fix: fallback should always be a string (#3792)

The SPA fallback option should always be a string, if the user configuration was specifed as boolean true it should have been replaced by the default string value (currently 404.html) in lib/common/options.js

Added test on empty string

Added missing test for spa-fallback.html
This commit is contained in:
Pim 2018-08-23 19:33:02 +02:00 committed by Sébastien Chopin
parent ba5df53042
commit 43f639b88e
2 changed files with 28 additions and 2 deletions

View File

@ -141,8 +141,8 @@ export default class Generator {
async afterGenerate() {
const { fallback } = this.options.generate
// Disable SPA fallback if value isn't true or a string
if (fallback !== true && typeof fallback !== 'string') return
// Disable SPA fallback if value isn't a non-empty string
if (typeof fallback !== 'string' || !fallback) return
const fallbackPath = path.join(this.distPath, fallback)

View File

@ -57,6 +57,23 @@ describe('fallback generate', () => {
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
})
test('nuxt re-generating with generate.fallback = \'\'', async () => {
nuxt.options.generate.fallback = ''
await expect(generator.generate({ build: false })).resolves.toBeTruthy()
})
test('empty string creates no fallback', async () => {
await expect(rp(url('/200.html'))).rejects.toMatchObject({
statusCode: 404,
response: {
body: expect.stringContaining('Cannot GET /200.html')
}
})
expect(existsSync(resolve(distDir, '200.html'))).toBe(false)
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
})
test('generate.fallback = true is transformed to /404.html', () => {
nuxt.options.generate.fallback = true
const options = Options.from(nuxt.options)
@ -71,6 +88,15 @@ describe('fallback generate', () => {
}
)
test('spa-fallback.html was created', async () => {
const html = await rp(url('/spa-fallback.html'))
expect(html.includes('<h1>Index page</h1>')).toBe(false)
expect(html.includes('data-server-rendered')).toBe(false)
expect(existsSync(resolve(distDir, 'spa-fallback.html'))).toBe(true)
expect(existsSync(resolve(distDir, '200.html'))).toBe(false)
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await server.close()