Skip to content

Commit

Permalink
Fix unhandled rejection on route change in dev (vercel#31554)
Browse files Browse the repository at this point in the history
This ensures we properly catch the rejection on route change failure in development so the dev overlay doesn't flash with the unhandled rejection. A test case isn't able to be added for this specific scenario as the unhandled rejection is fired right before the page is navigated so we can't consistently check if the rejection was unhandled or not. 

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes: vercel#28560
  • Loading branch information
ijjk authored and natew committed Feb 16, 2022
1 parent 563b2ac commit 7b71ec2
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions packages/next/client/route-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,34 +376,30 @@ export function createRouteLoader(assetPrefix: string): RouteLoader {
},
loadRoute(route: string, prefetch?: boolean) {
return withFuture<RouteLoaderEntry>(route, routes, () => {
const routeFilesPromise = getFilesForRoute(assetPrefix, route)
.then(({ scripts, css }) => {
return Promise.all([
entrypoints.has(route)
? []
: Promise.all(scripts.map(maybeExecuteScript)),
Promise.all(css.map(fetchStyleSheet)),
] as const)
})
.then((res) => {
return this.whenEntrypoint(route).then((entrypoint) => ({
entrypoint,
styles: res[1],
}))
})
let devBuildPromiseResolve: () => void

if (process.env.NODE_ENV === 'development') {
devBuildPromise = new Promise<void>((resolve) => {
if (routeFilesPromise) {
return routeFilesPromise.finally(() => {
resolve()
})
}
devBuildPromiseResolve = resolve
})
}

return resolvePromiseWithTimeout(
routeFilesPromise,
getFilesForRoute(assetPrefix, route)
.then(({ scripts, css }) => {
return Promise.all([
entrypoints.has(route)
? []
: Promise.all(scripts.map(maybeExecuteScript)),
Promise.all(css.map(fetchStyleSheet)),
] as const)
})
.then((res) => {
return this.whenEntrypoint(route).then((entrypoint) => ({
entrypoint,
styles: res[1],
}))
}),
MS_MAX_IDLE_DELAY,
markAssetError(new Error(`Route did not complete loading: ${route}`))
)
Expand All @@ -421,6 +417,7 @@ export function createRouteLoader(assetPrefix: string): RouteLoader {
}
return { error: err }
})
.finally(() => devBuildPromiseResolve?.())
})
},
prefetch(route: string): Promise<void> {
Expand Down

0 comments on commit 7b71ec2

Please sign in to comment.