Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): add experimental app:chunkError hook and reload strategy #19038

Merged
merged 10 commits into from
Feb 16, 2023

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented Feb 14, 2023

πŸ”— Linked issue

resolves #14594

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This PR adds a feature that can be enabled with the experimental.emitRouteChunkError option. This will catch chunk loading errors and calls the app:chunkError page. This can be handled with something like this:

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.hook('app:chunkError', ({ error }) => {
    console.log('caught chunk load error'))
  })
})

You can also set experimental.emitRouteChunkError to reload to enable an automatic hard reload when navigating if the new route triggers a chunk loading error.

Notes:

  • In webpack, a chunk error is not thrown when fetching payloads, although it does with vite. I think this is fine - errors fetching payload aren't really the kind of error we're trying to catch as payloads aren't hashed.
  • this approach does not suppress chunk errors. perhaps we can find a way for user to indicate that an error is 'handled' so we can then avoid throwing it.
  • the reload strategy does not handle all chunk errors, just ones on route navigation. For example, issues with prefetching chunks won't reload the page, though the error will still be thrown and it's possible to handle this intentionally

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codesandbox
Copy link

codesandbox bot commented Feb 14, 2023

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

const router = useRouter()

let chunkError: TypeError | null = null
nuxtApp.hook('app:chunkError', ({ error }) => { chunkError = error })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also catch this directly here:

Suggested change
nuxtApp.hook('app:chunkError', ({ error }) => { chunkError = error })
nuxtApp.hook('app:chunkError', ({ error }) => {
if (nuxtApp._processingMiddleware) {
// handle error here
}
})

I've not done that here in case the user has handled the error, but it's worth keeping in mind if someone has a better idea.

Copy link
Member

@clarkdo clarkdo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

packages/vite/src/client.ts Outdated Show resolved Hide resolved
@pi0
Copy link
Member

pi0 commented Feb 15, 2023

Nice work! For me it even looks good to be out of experimental flag testing on edge only.

One note about implementation it would be nice if the "reload" functionality extracted as a composable utility reloadNuxtApp({ error? }). It can be useful for outside integrations too such as handling app updates.

@danielroe
Copy link
Member Author

Great! 😊 I think we can include in next minor, along with extracting reload helper. πŸ‘ For now I'll keep it under flag so we can merge in a patch release.

@danielroe danielroe mentioned this pull request Feb 16, 2023
@danielroe danielroe changed the title feat(nuxt): add app:chunkError hook and experimental reload strategy feat(nuxt): add experimental app:chunkError hook and reload strategy Feb 16, 2023
@danielroe danielroe merged commit 96b09ea into main Feb 16, 2023
@danielroe danielroe deleted the feat/chunk-error branch February 16, 2023 12:44
@scofield-ua
Copy link

@danielroe so this is how I can set it in nuxt.config.ts:

export default defineNuxtConfig({
   experimental: {
      emitRouteChunkError: 'reload'    
   }
})

Is that correct?

@danielroe
Copy link
Member Author

@scofield-ua The API changed a little bit in #19086.

The jsdocs should give the possible configuration options for this. It is enabled by default in v3.3+.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto reload page when chunk assets are not found
4 participants