feat(generator): allow excluding exported pages using export:page hook (#7455)

This commit is contained in:
Ahad Birang 2020-06-04 00:05:28 +04:30 committed by GitHub
parent 1e4ce5055e
commit be726e42b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 2 deletions

View File

@ -359,10 +359,13 @@ export default class Generator {
}
// Call hook to let user update the path & html
const page = { route, path: fileName, html }
const page = { route, path: fileName, html, exclude: false }
await this.nuxt.callHook('generate:page', page)
await this.nuxt.callHook('export:page', { page })
await this.nuxt.callHook('export:page', { page, errors: pageErrors })
if (page.exclude) {
return false
}
page.path = path.join(this.distPath, page.path)
// Make sure the sub folders are created

View File

@ -36,6 +36,11 @@ describe('basic generate', () => {
nuxt.hook('generate:done', () => {
writeFileSync(changedFileName, '')
})
nuxt.hook('export:page', ({ page, errors }) => {
if (errors.length && page.route.includes('/skip-on-fail')) {
page.exclude = true
}
})
builder = new Builder(nuxt)
builder.build = jest.fn()
@ -249,6 +254,16 @@ describe('basic generate', () => {
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
})
test('Checke skipped files', () => {
expect(
existsSync(resolve(distDir, 'skip-on-fail/fail.html'))
).toBe(false)
expect(
existsSync(resolve(distDir, 'skip-on-fail/success.html'))
).toBe(true)
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await server.close()

View File

@ -32,6 +32,8 @@ export default {
'/users/1',
'/users/2',
'/тест雨',
'/skip-on-fail/success',
'/skip-on-fail/fail',
{ route: '/users/3', payload: { id: 3000 } }
],
interval: 200

View File

@ -0,0 +1,11 @@
<template>
<h1>404</h1>
</template>
<script>
export default {
asyncData ({ error }) {
error({ statusCode: 404, message: 'Post not found' })
}
}
</script>

View File

@ -0,0 +1,8 @@
<template>
<h1>200</h1>
</template>
<script>
export default {
}
</script>