question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Catch and handle ChunkLoadError

See original GitHub issue

Preliminary Checks

Description

We continue to see unhandled ChunkLoadError reports in our Sentry logs, as previously tracked in #18866.

Reproduction Link

N/A

Steps to Reproduce

N/A

Expected Result

Gatsby should catch ChunkLoadError and then examine and handle the error.

Actual Result

I continue to see ChunkLoadError unhandled exceptions in our Sentry logs.

@KyleAMathews added a workaround in #33032, where the page reloads on ChunkLoadError, but issues remain:

  • The error still bubbles to the app, and then to our Sentry logs.
  • If a chunk really was missing, for example due to a Webpack or CDN issue, the application would enter into an infinite loop. In a worst-case scenario this could DDoS an already-broken server or host.
    • This can be reproduced by using DevTools to block requests for a single .js chunk and refreshing. The application starts aggressively refreshing.

I think the current workaround was written the way it is because @KyleAMathews believed this error is uncatchable: https://github.com/gatsbyjs/gatsby/issues/18866#issuecomment-845561804

But I’m reasonably confident that I wouldn’t be seeing this in my Sentry logs if that were true. If our error boundary can catch it, certainly a Gatsby error boundary could do the same.

With some guidance I’d be happy to take a shot at a PR. Specifically I’d love any ideas on avoiding the refresh loop - I think it would be better to crash on a second consecutive error - and on whether it would be possible to write tests that cover this.

Environment

Gatsby latest.

Config Flags

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
PeterDekkerscommented, Dec 2, 2021
  • For this issue: Check if page.error.name === "ChunkLoadError", if yes, issue window.location.reload() instead of throwing.

@aaronadamsCA, if the chunk is actually missing then wouldn’t this turn into a reload loop?

1reaction
graysonhickscommented, Dec 3, 2021

@PeterDekkers Yea, I think he mentioned that as the worst-case scenario in the description. Definitely something that would need to be solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix ChunkLoadError in your ReactJS application
Do you or your users get a ChunkLoadError after your deploy updates? ... you can apply the same logic (and put it in...
Read more >
How to Solve the Chunk Load Error in JavaScript
Whenever there's an error observed in dynamically fetching helper JavaScript files known as Chunks, a ChunkLoad Error is thrown.
Read more >
React + Webpack: ChunkLoadError: Loading chunk X failed.
React + Webpack: ChunkLoadError: Loading chunk X failed. ... suggest to try/catch the error, retry a few times and then crash silently.
Read more >
How to solve Chunk Load Error in Create-React-app project?
When one chunk will be missing in the index.html, the error boundary will catch the error and run the function "retryPageLoading()".
Read more >
Fixing ChunkLoadError
I solved this issue by adding a try-catch around the navigation and page import logic so that if a page chunk request fails,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found