{"version":3,"file":"lazyRouteComponent.cjs","names":["isModuleNotFoundError","Dynamic","createMemo","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 { isModuleNotFoundError } from '@tanstack/router-core'\nimport { Dynamic } from '@solidjs/web'\nimport { createMemo } from 'solid-js'\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 = createMemo(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,MAAMoB,gBAAAA,GAAAA,SAAAA,YAA0BlB,MAAM;IACpCmB,cAAcrB;IACdsB,aAAa;IACd,CAAC;AACF,WAAA,GAAA,aAAA,iBAAQnC,aAAAA,UAAAA,GAAAA,aAAAA,YAAO,EAAA,IAACsC,YAAS;AAAA,WAAEL,cAAc;MAAA,EAAMvB,MAAK,CAAA;;AAGtD,UAAA,GAAA,aAAA,iBAAQV,aAAAA,UAAAA,GAAAA,aAAAA,YAAO,EAACsC,WAAWzB,MAAI,EAAMH,MAAK,CAAA;;AAG1CW,UAAiBkB,UAAUxB;AAE7B,QAAOM"}