| 1 | /** |
| 2 | * A page left open across a deploy runs the old build. Its next client |
| 3 | * navigation can fail in ways that only mean "this tab is out of date": the |
| 4 | * router's manifest no longer matches the server's, a route module's old |
| 5 | * file is gone, or an address only the new build knows falls through to the |
| 6 | * old build's catch-all 404. Each of those is fixed by loading the address |
| 7 | * again as a whole page, not by an error page. |
| 8 | */ |
| 9 | |
| 10 | /** Messages that mean the tab's code is older than the server's. */ |
| 11 | const STALE_MESSAGES = [ |
| 12 | /manifest version mismatch/i, |
| 13 | /failed to fetch dynamically imported module/i, |
| 14 | /error loading dynamically imported module/i, |
| 15 | /importing a module script failed/i, |
| 16 | /unable to preload css/i, |
| 17 | ]; |
| 18 | |
| 19 | export type ErrorSeen = { |
| 20 | /** The error the root boundary caught. */ |
| 21 | error: unknown; |
| 22 | /** The 404's status, when the error is a response. */ |
| 23 | status?: number; |
| 24 | /** Whether the page got here by a client navigation, not a document load. */ |
| 25 | clientNavigation: boolean; |
| 26 | /** Whether the deepest route matched is the catch-all not-found route. */ |
| 27 | caughtByCatchAll: boolean; |
| 28 | }; |
| 29 | |
| 30 | /** Whether reloading the address as a whole page is the fix for this error. */ |
| 31 | export function reloadFixes({ error, status, clientNavigation, caughtByCatchAll }: ErrorSeen): boolean { |
| 32 | // A document load already ran the server's newest build. |
| 33 | if (!clientNavigation) return false; |
| 34 | // An address the old build does not know: the new one may. |
| 35 | if (status === 404) return caughtByCatchAll; |
| 36 | const message = error instanceof Error ? error.message : typeof error === "string" ? error : ""; |
| 37 | return STALE_MESSAGES.some((pattern) => pattern.test(message)); |
| 38 | } |
| 39 | |
| 40 | /** The session key that remembers which address was reloaded, so it is reloaded once. */ |
| 41 | export const RELOADED_KEY = "g1t-reloaded"; |