UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

1 lines 4.66 kB
{"version":3,"file":"lazyRouteComponent.cjs","names":["Dynamic","createResource","isModuleNotFoundError","AsyncRouteComponent","lazyRouteComponent","Record","T","importer","Promise","exportName","TKey","props","TProps","loadPromise","comp","error","load","then","res","undefined","catch","err","lazyComp","Lazy","Error","window","sessionStorage","storageKey","message","getItem","setItem","location","reload","default","compResource","initialValue","ssrLoadFrom","_$createComponent","_$mergeProps","component","preload"],"sources":["../../src/lazyRouteComponent.tsx"],"sourcesContent":["import { Dynamic } from 'solid-js/web'\nimport { createResource } 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 let loadPromise: Promise<any> | undefined\n let comp: T[TKey] | T['default']\n let error: any\n\n const load = () => {\n if (!loadPromise) {\n loadPromise = importer()\n .then((res) => {\n loadPromise = undefined\n comp = res[exportName ?? 'default']\n return comp\n })\n .catch((err) => {\n error = err\n })\n }\n\n return loadPromise\n }\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":";;;;AAKA,SAAgBI,mBAIdG,UACAE,YAGQ;CACR,IAAII;CACJ,IAAIC;CACJ,IAAIC;CAEJ,MAAMC,aAAa;EACjB,IAAI,CAACH,aACHA,cAAcN,SAAS,EACpBU,MAAMC,QAAQ;GACbL,cAAcM,KAAAA;GACdL,OAAOI,IAAIT,cAAc;GACzB,OAAOK;EACT,CAAC,EACAM,OAAOC,QAAQ;GACdN,QAAQM;EACV,CAAC;EAGL,OAAOR;CACT;CAEA,MAAMS,WAAW,SAASC,KAAKZ,OAAY;EAGzC,IAAII,OAAO;GAMT,KAAA,GAAA,sBAAA,uBAA0BA,KAAK;QAO3BA,iBAAiBS,SACjB,OAAOC,WAAW,eAClB,OAAOC,mBAAmB,aAC1B;KAKA,MAAMC,aAAa,0BAA0BZ,MAAMa;KACnD,IAAI,CAACF,eAAeG,QAAQF,UAAU,GAAG;MACvCD,eAAeI,QAAQH,YAAY,GAAG;MACtCF,OAAOM,SAASC,OAAO;MAGvB,OAAO,EACLC,eAAe,KACjB;KACF;IACF;;GAIF,MAAMlB;EACR;EAEA,IAAI,CAACD,MAAM;GACT,MAAM,CAACoB,iBAAAA,GAAAA,SAAAA,gBAA+BlB,MAAM;IAC1CmB,cAAcrB;IACdsB,aAAa;GACf,CAAC;GACD,QAAA,GAAA,aAAA,iBAAQpC,aAAAA,UAAAA,GAAAA,aAAAA,YAAO,EAAA,IAACuC,YAAS;IAAA,OAAEL,aAAa;GAAC,EAAA,GAAMvB,KAAK,CAAA;EACtD;EAEA,QAAA,GAAA,aAAA,iBAAQX,aAAAA,UAAAA,GAAAA,aAAAA,YAAO,EAACuC,WAAWzB,KAAI,GAAMH,KAAK,CAAA;CAC5C;CAEC,SAAkB6B,UAAUxB;CAE7B,OAAOM;AACT"}