@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
1 lines • 5.37 kB
Source Map (JSON)
{"version":3,"file":"lazyRouteComponent.cjs","names":["Dynamic","lazy","isModuleNotFoundError","isServer","AsyncRouteComponent","lazyRouteComponent","Record","T","importer","Promise","exportName","TKey","props","TProps","loadPromise","comp","error","load","undefined","then","res","window","Lazy","preload","catch","err","render","_$createComponent","Loadable","Error","sessionStorage","storageKey","message","getItem","setItem","location","reload","_$mergeProps","component","process","env","NODE_ENV","String","default"],"sources":["../../src/lazyRouteComponent.tsx"],"sourcesContent":["import { Dynamic } from 'solid-js/web'\nimport { lazy } from 'solid-js'\nimport { isModuleNotFoundError } from '@tanstack/router-core'\nimport { isServer } from '@tanstack/router-core/isServer'\nimport type { AsyncRouteComponent } from './route'\n\nexport function lazyRouteComponent<\n T extends Record<string, any>,\n TKey extends keyof T = 'default',\n>(\n importer: () => Promise<T>,\n exportName?: TKey,\n): T[TKey] extends (props: infer TProps) => any\n ? AsyncRouteComponent<TProps>\n : never {\n let loadPromise: Promise<any> | undefined\n let comp: T[TKey] | T['default']\n let error: any\n\n const load = () => {\n if (!loadPromise) {\n error = undefined\n loadPromise = importer()\n .then((res) => {\n // Resolved clients have no preload work; SSR can reuse the import.\n if (!(isServer ?? typeof window === 'undefined')) {\n loadPromise = undefined\n ;(Lazy as any).preload = undefined\n }\n comp = res[exportName ?? 'default']\n return comp\n })\n .catch((err) => {\n loadPromise = undefined\n error = err\n })\n }\n\n return loadPromise\n }\n function Lazy(props: any) {\n if (comp || error) {\n return render(props)\n }\n\n return <Loadable {...props} />\n }\n\n // Both preload and lazy resolution end here without starting another load.\n function render(props: any) {\n // Now that we're out of preload and into actual render path,\n // throw the error if it was a module not found error during preload\n if (error) {\n // If the load fails due to module not found, it may mean a new version of\n // the build was deployed and the user's browser is still using an old version.\n // If this happens, the old version in the user's browser would have an outdated\n // URL to the lazy module.\n // In that case, we want to attempt one window refresh to get the latest.\n if (isModuleNotFoundError(error)) {\n // We don't want an error thrown from preload in this case, because\n // there's nothing we want to do about module not found during preload.\n // Record the error, recover the promise with a null return,\n // and we will attempt module not found resolution during the render path.\n\n if (\n error instanceof Error &&\n typeof window !== 'undefined' &&\n typeof sessionStorage !== 'undefined'\n ) {\n // Again, we want to reload one time on module not found error and not enter\n // a reload loop if there is some other issue besides an old deploy.\n // That's why we store our reload attempt in sessionStorage.\n // Use error.message as key because it contains the module path that failed.\n const storageKey = `tanstack_router_reload:${error.message}`\n if (!sessionStorage.getItem(storageKey)) {\n sessionStorage.setItem(storageKey, '1')\n window.location.reload()\n\n // Return no content while we wait for window to reload.\n return null\n }\n }\n }\n\n // Otherwise, just throw the error\n throw error\n }\n\n return <Dynamic component={comp} {...props} />\n }\n\n const Loadable = lazy(async () => {\n await load()\n if (process.env.NODE_ENV !== 'production' && !comp && !error) {\n throw new Error(\n `lazyRouteComponent: export \"${String(exportName ?? 'default')}\" not found`,\n )\n }\n return { default: render }\n })\n\n ;(Lazy as any).preload = load\n\n return Lazy as any\n}\n"],"mappings":";;;;;AAMA,SAAgBK,mBAIdG,UACAE,YAGQ;CACR,IAAII;CACJ,IAAIC;CACJ,IAAIC;CAEJ,MAAMC,aAAa;EACjB,IAAI,CAACH,aAAa;GAChBE,QAAQE,KAAAA;GACRJ,cAAcN,SAAS,EACpBW,MAAMC,QAAQ;IAEb,IAAI,EAAEjB,+BAAAA,YAAY,OAAOkB,WAAW,cAAc;KAChDP,cAAcI,KAAAA;KACb,KAAcK,UAAUL,KAAAA;IAC3B;IACAH,OAAOK,IAAIV,cAAc;IACzB,OAAOK;GACT,CAAC,EACAS,OAAOC,QAAQ;IACdX,cAAcI,KAAAA;IACdF,QAAQS;GACV,CAAC;EACL;EAEA,OAAOX;CACT;CACA,SAASQ,KAAKV,OAAY;EACxB,IAAIG,QAAQC,OACV,OAAOU,OAAOd,KAAK;EAGrB,QAAA,GAAA,aAAA,iBAAQgB,UAAahB,KAAK;CAC5B;CAGA,SAASc,OAAOd,OAAY;EAG1B,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