Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

charts-loader.ts

74 lines2.9 KB

1/*
2 * Loads the chart module, with retries that work in real browsers.
3 *
4 * Browsers remember a module URL whose fetch failed, and fail every later
5 * import() of it at once. So a retry asks for the same chunk under a new
6 * URL, with a `?retry=` query. The URL comes from the import error's message
7 * (Chromium and Firefox name it), and it's only used if it really is the
8 * chart chunk: same origin, same directory as this module (the build's
9 * assets, or the source folder in dev) and named like the chunk. Anything
10 * else, or a message without a URL, retries with the plain import.
11 */
12​
13/** "Failed to fetch dynamically imported module: <url>" (Chromium), "error loading dynamically imported module: <url>" (Firefox). */
14const IMPORT_FAILURE = /dynamically imported module:\s*(\S+)/i;
15​
16/** The chart chunk's file name: `charts-<hash>.js` once built, `charts.tsx` in dev. */
17const CHART_CHUNK = /^charts(-[\w-]+)?\.(js|tsx)$/;
18​
19/**
20 * The chart chunk a failed import names, without its query, or `null` if
21 * the error doesn't name one: `moduleUrl` is this module's own URL
22 * (`import.meta.url`), `origin` the page's.
23 */
24export function chartChunkUrlFrom(
25 error: unknown,
26 moduleUrl: string,
27 origin: string,
28): string | null {
29 const message = error instanceof Error ? error.message : String(error);
30 const named = IMPORT_FAILURE.exec(message)?.[1];
31 if (!named || !URL.canParse(named) || !URL.canParse(moduleUrl)) return null;
32 const url = new URL(named);
33 const here = new URL(moduleUrl);
34 if (url.origin !== origin || here.origin !== origin) return null;
35 const directory = here.pathname.slice(0, here.pathname.lastIndexOf("/") + 1);
36 if (!url.pathname.startsWith(directory)) return null;
37 if (!CHART_CHUNK.test(url.pathname.slice(directory.length))) return null;
38 return url.origin + url.pathname;
40​
41export interface ChartsLoaderOptions<TModule> {
42 /** The normal import, `() => import("./charts")`. */
43 importModule: () => Promise<TModule>;
44 /** An import of an exact URL, for retries under a new URL. */
45 importUrl: (url: string) => Promise<TModule>;
46 /** The chunk URL a failed import names (see `chartChunkUrlFrom`), or `null`. */
47 chunkUrlFrom: (error: unknown) => string | null;
49​
50/**
51 * A `load()` that imports the module once, however many charts ask at the
52 * same time. A failed import is forgotten, so the next `load()` tries again:
53 * under a new URL if the failure named the chart chunk, otherwise the
54 * normal way.
55 */
56export function createChartsLoader<TModule>({
57 importModule,
58 importUrl,
59 chunkUrlFrom,
60}: ChartsLoaderOptions<TModule>): () => Promise<TModule> {
61 let pending: Promise<TModule> | null = null;
62 let failedUrl: string | null = null;
63 let retries = 0;
64 return () => {
65 pending ??= (failedUrl ? importUrl(`${failedUrl}?retry=${++retries}`) : importModule()).catch(
66 (error: unknown) => {
67 pending = null;
68 failedUrl ??= chunkUrlFrom(error);
69 throw error;
70 },
71 );
72 return pending;
73 };