Unverified Commit ee605cb6 authored by Tobias Järvelöv's avatar Tobias Järvelöv Committed by GitHub

Wait for getFilesForRoute promise to fulfill before timeout in dev mode (#27395)

The fix in PR #25749 only works some of the time. The reason why it
doesn't work all of the time is because the `devBuildResolve` function
is called when the assets have been built, but this can happen before
the `getFilesForRoute` promise chain has completed, and thus the
`cancelled` variable could not yet have been updated.

To fix this we wait for the `getFilesForRoute` promise chain to be
fulfilled and then resolve the `devBuildPromise` promise afterwards.
This allows the `getFilesForRoute` promise chain to be fulfilled before
and the `cancelled` variable can be updated accordingly.

If an error is thrown somewhere in the `getFilesForRoute` promise chain,
i.e. due to a failed fetch request, then that will also resolve the
`devBuildPromise` and the error will bubble up to ultimately become a
`routeChangeError` which will reload the page.

With this fix the need to listen for HMR events has become obsolete as
regardless of when the HMR build/sync events complete we still want to
ensure that the `getFilesForRoute` promise chain has been fulfilled
before resolving the `devBuildPromise`.
Co-authored-by: 's avatarTobias Järvelöv <tobias.jarvelov@oderland.se>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 9cbc4aac
...@@ -152,35 +152,6 @@ function appendScript( ...@@ -152,35 +152,6 @@ function appendScript(
// We wait for pages to be built in dev before we start the route transition // We wait for pages to be built in dev before we start the route transition
// timeout to prevent an un-necessary hard navigation in development. // timeout to prevent an un-necessary hard navigation in development.
let devBuildPromise: Promise<void> | undefined let devBuildPromise: Promise<void> | undefined
let devBuildResolve: (() => void) | undefined
if (process.env.NODE_ENV === 'development') {
const { addMessageListener } = require('./dev/error-overlay/eventsource')
addMessageListener((event: any) => {
// This is the heartbeat event
if (event.data === '\uD83D\uDC93') {
return
}
const obj =
typeof event === 'string' ? { action: event } : JSON.parse(event.data)
switch (obj.action) {
case 'built':
case 'sync':
if (devBuildResolve) {
devBuildResolve()
devBuildResolve = undefined
}
break
default:
break
}
})
}
// Resolve a promise that times out after given amount of milliseconds. // Resolve a promise that times out after given amount of milliseconds.
function resolvePromiseWithTimeout<T>( function resolvePromiseWithTimeout<T>(
...@@ -356,28 +327,34 @@ export function createRouteLoader(assetPrefix: string): RouteLoader { ...@@ -356,28 +327,34 @@ export function createRouteLoader(assetPrefix: string): RouteLoader {
}, },
loadRoute(route: string, prefetch?: boolean) { loadRoute(route: string, prefetch?: boolean) {
return withFuture<RouteLoaderEntry>(route, routes, () => { 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],
}))
})
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
devBuildPromise = new Promise<void>((resolve) => { devBuildPromise = new Promise<void>((resolve) => {
devBuildResolve = resolve if (routeFilesPromise) {
return routeFilesPromise.finally(() => {
resolve()
})
}
}) })
} }
return resolvePromiseWithTimeout( return resolvePromiseWithTimeout(
getFilesForRoute(assetPrefix, route) routeFilesPromise,
.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, MS_MAX_IDLE_DELAY,
markAssetError(new Error(`Route did not complete loading: ${route}`)) markAssetError(new Error(`Route did not complete loading: ${route}`))
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment