@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
1 lines • 3.22 kB
Source Map (JSON)
{"version":3,"file":"lazyRouteComponent.cjs","names":[],"sources":["../../src/lazyRouteComponent.tsx"],"sourcesContent":["import { lazy } from 'solid-js'\nimport { isModuleNotFoundError } from '@tanstack/router-core'\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 const resolvedExport = (exportName as string | undefined) ?? 'default'\n let preloadPromise: Promise<void> | undefined\n\n // lazy()'s { export } option names which export of the resolved module is\n // the component — a call-site literal available on both runtimes, so the\n // module namespace passes through untouched ($$moduleUrl included, which\n // is how server-side lazy() resolves the route chunk's client assets) and\n // hydration claims the component synchronously from the preloaded module.\n const comp = lazy(\n () =>\n importer().catch((error) => {\n // If the load fails due to module not found, it may mean a new\n // version of the build was deployed and the user's browser is still\n // using an old version with an outdated URL to the lazy module. In\n // that case, attempt one window refresh to get the latest — gated\n // through sessionStorage so some other issue can't cause a reload\n // loop.\n if (\n isModuleNotFoundError(error) &&\n error instanceof Error &&\n typeof window !== 'undefined' &&\n typeof sessionStorage !== 'undefined'\n ) {\n const storageKey = `tanstack_router_reload:${error.message}`\n if (!sessionStorage.getItem(storageKey)) {\n sessionStorage.setItem(storageKey, '1')\n window.location.reload()\n\n // The page is reloading; render nothing in the meantime.\n return { [resolvedExport]: () => null } as unknown as T\n }\n }\n\n throw error\n }),\n { export: resolvedExport },\n )\n\n const load = comp.preload\n\n // TanStack's preload contract: a memoized Promise<void> that never\n // rejects. lazy() does not cache rejected module promises, so a failed\n // download is retried by the next preload or render.\n comp.preload = () => {\n if (!preloadPromise) {\n preloadPromise = load().then(\n () => {},\n () => {\n preloadPromise = undefined\n },\n )\n }\n return preloadPromise\n }\n\n return comp as any\n}\n"],"mappings":";;;AAIA,SAAgB,mBAId,UACA,YAGQ;CACR,MAAM,iBAAkB,cAAqC;CAC7D,IAAI;CAOJ,MAAM,QAAA,GAAA,SAAA,YAEF,SAAS,EAAE,OAAO,UAAU;EAO1B,KAAA,GAAA,sBAAA,uBACwB,KAAK,KAC3B,iBAAiB,SACjB,OAAO,WAAW,eAClB,OAAO,mBAAmB,aAC1B;GACA,MAAM,aAAa,0BAA0B,MAAM;GACnD,IAAI,CAAC,eAAe,QAAQ,UAAU,GAAG;IACvC,eAAe,QAAQ,YAAY,GAAG;IACtC,OAAO,SAAS,OAAO;IAGvB,OAAO,GAAG,uBAAuB,KAAK;GACxC;EACF;EAEA,MAAM;CACR,CAAC,GACH,EAAE,QAAQ,eAAe,CAC3B;CAEA,MAAM,OAAO,KAAK;CAKlB,KAAK,gBAAgB;EACnB,IAAI,CAAC,gBACH,iBAAiB,KAAK,EAAE,WAChB,CAAC,SACD;GACJ,iBAAiB,KAAA;EACnB,CACF;EAEF,OAAO;CACT;CAEA,OAAO;AACT"}