Skip to content

Commit fc83cb7

Browse files
authored
feat(gatsby): catch webpack chunk loading errors and completely reload the app (#33032)
Fix #18866 The error happens when a user loads a page and while they have it open, a new version of the site is deployed. Then when they try to navigate and the Gatsby app tries to load a js chunk, the chunk might no longer exist on the server and so the app crashes.
1 parent f8f1666 commit fc83cb7

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

packages/gatsby/cache-dir/navigation.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,20 @@ function maybeRedirect(pathname) {
2121
}
2222
}
2323

24+
// Catch unhandled chunk loading errors and force a restart of the app.
25+
let nextRoute = ``
26+
27+
window.addEventListener(`unhandledrejection`, event => {
28+
if (/loading chunk \d* failed./i.test(event.reason)) {
29+
if (nextRoute) {
30+
window.location.pathname = nextRoute
31+
}
32+
}
33+
})
34+
2435
const onPreRouteUpdate = (location, prevLocation) => {
2536
if (!maybeRedirect(location.pathname)) {
37+
nextRoute = location.pathname
2638
apiRunner(`onPreRouteUpdate`, { location, prevLocation })
2739
}
2840
}

0 commit comments

Comments
 (0)