@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
1 lines • 5.12 kB
Source Map (JSON)
{"version":3,"file":"lazyRouteComponent.cjs","names":["Dynamic","createResource","isModuleNotFoundError","isServer","AsyncRouteComponent","lazyRouteComponent","Record","T","importer","Promise","exportName","TKey","props","TProps","loadPromise","comp","error","load","undefined","then","res","window","lazyComp","preload","catch","err","Lazy","Error","sessionStorage","storageKey","message","getItem","setItem","location","reload","default","compResource","initialValue","ssrLoadFrom","_$createComponent","_$mergeProps","component"],"sources":["../../src/lazyRouteComponent.tsx"],"sourcesContent":["import { Dynamic } from 'solid-js/web'\nimport { createResource } 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 ;(lazyComp 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 const lazyComp = function Lazy(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 empty component while we wait for window to reload\n return {\n default: () => null,\n }\n }\n }\n }\n\n // Otherwise, just throw the error\n throw error\n }\n\n if (!comp) {\n const [compResource] = createResource(load, {\n initialValue: comp,\n ssrLoadFrom: 'initial',\n })\n return <Dynamic component={compResource()} {...props} />\n }\n\n return <Dynamic component={comp} {...props} />\n }\n\n ;(lazyComp as any).preload = load\n\n return lazyComp 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,SAAkBK,UAAUL,KAAAA;IAC/B;IACAH,OAAOK,IAAIV,cAAc;IACzB,OAAOK;GACT,CAAC,EACAS,OAAOC,QAAQ;IACdX,cAAcI,KAAAA;IACdF,QAAQS;GACV,CAAC;EACL;EAEA,OAAOX;CACT;CACA,MAAMQ,WAAW,SAASI,KAAKd,OAAY;EAGzC,IAAII,OAAO;GAMT,KAAA,GAAA,sBAAA,uBAA0BA,KAAK;QAO3BA,iBAAiBW,SACjB,OAAON,WAAW,eAClB,OAAOO,mBAAmB,aAC1B;KAKA,MAAMC,aAAa,0BAA0Bb,MAAMc;KACnD,IAAI,CAACF,eAAeG,QAAQF,UAAU,GAAG;MACvCD,e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