Skip to content

Commit

Permalink
- handle promise rejection - notify after reload check to avoid any e…
Browse files Browse the repository at this point in the history
…xtra pending HMR work
  • Loading branch information
jacob-ebey authored and defjosiah committed Jul 5, 2023
1 parent aba7ca6 commit 59552f4
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/remix-react/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ let hmrRouterReadyPromise = new Promise<Router>((resolve) => {
// body of a promise is executed immediately, so this can be resolved outside
// of the promise body
hmrRouterReadyResolve = resolve;
}).catch(() => {
// This is a noop catch handler to avoid unhandled promise rejection warnings
// in the console. The promise is never rejected.
return undefined;
});

if (import.meta && import.meta.hot) {
Expand All @@ -68,7 +72,15 @@ if (import.meta && import.meta.hot) {
assetsManifest: EntryContext["manifest"];
needsRevalidation: Set<string>;
}) => {
const router = await hmrRouterReadyPromise;
let router = await hmrRouterReadyPromise;
// This should never happen, but just in case...
if (!router) {
console.error(
"Failed to accept HMR update because the router was not ready."
);
return;
}

let routeIds = [
...new Set(
router.state.matches
Expand Down Expand Up @@ -190,8 +202,7 @@ export function RemixBrowser(_props: RemixBrowserProps): ReactElement {
window.__remixContext.future.v2_normalizeFormMethod,
},
});
hmrRouterReadyResolve?.(router);


// Hard reload if the path we tried to load is not the current path.
// This is usually the result of 2 rapid back/forward clicks from an
// external site into a Remix app, where we initially start the load for
Expand All @@ -208,6 +219,11 @@ export function RemixBrowser(_props: RemixBrowserProps): ReactElement {
console.error(errorMsg);
window.location.reload();
}

// Notify that the router is ready for HMR
if (hmrRouterReadyResolve) {
hmrRouterReadyResolve(router);
}
}

let [location, setLocation] = React.useState(router.state.location);
Expand Down

0 comments on commit 59552f4

Please sign in to comment.