| 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 | /** |
| 41 | * The key React Router gives the location a tab's document loaded at. It is |
| 42 | * the history entry's own key, and not "default": ScrollRestoration's inline |
| 43 | * script gives the entry a key before the app starts, so a key alone cannot |
| 44 | * tell a document load from a client navigation. Read when this module first |
| 45 | * runs, which is before the app hydrates; undefined on the server. |
| 46 | */ |
| 47 | const DOCUMENT_KEY: string | undefined = |
| 48 | typeof window === "undefined" ? undefined : ((window.history.state as { key?: string } | null)?.key ?? "default"); |
| 49 | |
| 50 | /** |
| 51 | * Whether the page at this location key got there by a client navigation. |
| 52 | * The server, and the render that hydrates the server's page, always say |
| 53 | * no, so both render the same error page. |
| 54 | */ |
| 55 | export function clientNavigated(key: string, documentKey: string | undefined = DOCUMENT_KEY): boolean { |
| 56 | return documentKey !== undefined && key !== documentKey; |
| 57 | } |
| 58 | |
| 59 | /** The session key that remembers which address was reloaded, so it is reloaded once. */ |
| 60 | export const RELOADED_KEY = "g1t-reloaded"; |
| 61 | |
| 62 | /** Whether this address was already loaded again once, so another try would not help. */ |
| 63 | export function reloadedBefore(href: string, storage: Pick<Storage, "getItem"> | undefined = sessionStorageOrNone()): boolean { |
| 64 | try { |
| 65 | return storage?.getItem(RELOADED_KEY) === href; |
| 66 | } catch { |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** How long the page waits for the reload to start before it shows the error instead. */ |
| 72 | export const RELOAD_GIVE_UP_MS = 5000; |
| 73 | |
| 74 | function sessionStorageOrNone(): Storage | undefined { |
| 75 | try { |
| 76 | return typeof window === "undefined" ? undefined : window.sessionStorage; |
| 77 | } catch { |
| 78 | return undefined; |
| 79 | } |
| 80 | } |