Skip to content

Commit

Permalink
Merge pull request #19 from BKWLD/no-fail-ssg-on-404
Browse files Browse the repository at this point in the history
Don’t exit with error on 404s during SSG
  • Loading branch information
weotch authored Jun 14, 2022
2 parents f4d79de + 22bc24f commit 7dc45aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
36 changes: 30 additions & 6 deletions modules/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
import { isGenerating } from '../helpers/env'
export default function() {

// Exit immediately when a route fails
this.nuxt.hook('generate:routeFailed', ({ errors }) => {
errors.forEach(error => console.error(error))
process.exit(1)
})

// Support falling back to a resolvable file on Netlify if a route didn't
// exist when build was run. We only want this to run when _not_ using
// generate so we return true 404s.
Expand All @@ -25,4 +19,34 @@ export default function() {

// Sub folders, set to false to remove the trailing slashes
this.options.generate.subFolders = false

// Immediately exit for route level errors, like Axios errors. The errors
// array looks like:
// {
// type: 'unhandled',
// route: '/products/cam-wall-mounting-kit/19731607617609',
// error: // An actual exception object
// }
this.nuxt.hook('generate:routeFailed', ({ errors }) => {
errors.forEach(error => console.error(error))
process.exit(1)
})

// Abort SSG if any of the errors weren't 404s with an "ENOENT" error. The
// 404 errors don't fire the generate:routeFailed hook for some reason. The
// errors array contains objects that look like:
// {
// type: 'handled',
// route: '/products/play-time-fabric-sock',
// error: {
// statusCode: 404,
// message: 'Page not found'
// }
// }
this.nuxt.hook('generate:done', (generator, errors) => {
if (errors.filter((error) => {
return error?.error?.statusCode != 404
}).length) process.exit(2)
})

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dev": "nuxt dev demo",
"build": "nuxt build demo",
"start": "nuxt start demo",
"generate": "nuxt generate --fail-on-error --target=static demo"
"generate": "nuxt generate --target=static demo"
},
"dependencies": {
"@bkwld/credits": "^0.3.0",
Expand Down

0 comments on commit 7dc45aa

Please sign in to comment.