UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

1 lines 4.62 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;AACjB,MAAI,CAACH,YACHA,eAAcN,UAAU,CACrBU,MAAMC,QAAQ;AACbL,iBAAcM,KAAAA;AACdL,UAAOI,IAAIT,cAAc;AACzB,UAAOK;IACP,CACDM,OAAOC,QAAQ;AACdN,WAAQM;IACR;AAGN,SAAOR;;CAGT,MAAMS,WAAW,SAASC,KAAKZ,OAAY;AAGzC,MAAII,OAAO;AAMT,QAAA,GAAA,sBAAA,uBAA0BA,MAAM;QAO5BA,iBAAiBS,SACjB,OAAOC,WAAW,eAClB,OAAOC,mBAAmB,aAC1B;KAKA,MAAMC,aAAa,0BAA0BZ,MAAMa;AACnD,SAAI,CAACF,eAAeG,QAAQF,WAAW,EAAE;AACvCD,qBAAeI,QAAQH,YAAY,IAAI;AACvCF,aAAOM,SAASC,QAAQ;AAGxB,aAAO,EACLC,eAAe,MAChB;;;;AAMP,SAAMlB;;AAGR,MAAI,CAACD,MAAM;GACT,MAAM,CAACoB,iBAAAA,GAAAA,SAAAA,gBAA+BlB,MAAM;IAC1CmB,cAAcrB;IACdsB,aAAa;IACd,CAAC;AACF,WAAA,GAAA,aAAA,iBAAQpC,aAAAA,UAAAA,GAAAA,aAAAA,YAAO,EAAA,IAACuC,YAAS;AAAA,WAAEL,cAAc;MAAA,EAAMvB,MAAK,CAAA;;AAGtD,UAAA,GAAA,aAAA,iBAAQX,aAAAA,UAAAA,GAAAA,aAAAA,YAAO,EAACuC,WAAWzB,MAAI,EAAMH,MAAK,CAAA;;AAG1CW,UAAiBkB,UAAUxB;AAE7B,QAAOM"}