{"version":3,"file":"lazyRouteComponent.cjs","names":[],"sources":["../../src/lazyRouteComponent.tsx"],"sourcesContent":["import * as React from 'react'\nimport { isModuleNotFoundError } from '@tanstack/router-core'\nimport { isServer } from '@tanstack/router-core/isServer'\nimport { reactUse } from './utils'\nimport type { AsyncRouteComponent } from './route'\n\n/**\n * Wrap a dynamic import to create a route component that supports\n * `.preload()` and friendly reload-on-module-missing behavior.\n *\n * @param importer Function returning a module promise\n * @param exportName Named export to use (default: `default`)\n * @returns A lazy route component compatible with TanStack Router\n * @link https://tanstack.com/router/latest/docs/framework/react/api/router/lazyRouteComponentFunction\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          // Keep browser preload behavior unchanged; SSR can reuse the import.\n          if (!(isServer ?? typeof window === 'undefined')) {\n            loadPromise = undefined\n          }\n          comp = res[exportName ?? 'default']\n        })\n        .catch((err) => {\n          loadPromise = undefined\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, the rest is handled during the render path.\n          error = err\n        })\n    }\n\n    return loadPromise\n  }\n\n  const lazyComp = function Lazy(props: any) {\n    if (error) {\n      // A missing module can mean that a newer deployment replaced the URL.\n      // Reload only for the error that is still current at render time, so a\n      // successful retry cannot leave a stale reload request armed.\n      if (\n        isModuleNotFoundError(error) &&\n        !(isServer ?? 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          // Suspend forever while the document reloads.\n          throw new Promise(() => {})\n        }\n      }\n      throw error\n    }\n\n    if (!comp) {\n      if (reactUse) {\n        reactUse(load())\n      } else {\n        throw load()\n      }\n    }\n\n    return React.createElement(comp, props)\n  }\n\n  ;(lazyComp as any).preload = load\n\n  return lazyComp as any\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAeA,SAAgB,mBAId,UACA,YAGQ;CACR,IAAI;CACJ,IAAI;CACJ,IAAI;CAEJ,MAAM,aAAa;EACjB,IAAI,CAAC,aAAa;GAChB,QAAQ,KAAA;GACR,cAAc,SAAS,EACpB,MAAM,QAAQ;IAEb,IAAI,EAAE,+BAAA,YAAY,OAAO,WAAW,cAClC,cAAc,KAAA;IAEhB,OAAO,IAAI,cAAc;GAC3B,CAAC,EACA,OAAO,QAAQ;IACd,cAAc,KAAA;IAId,QAAQ;GACV,CAAC;EACL;EAEA,OAAO;CACT;CAEA,MAAM,WAAW,SAAS,KAAK,OAAY;EACzC,IAAI,OAAO;GAIT,KAAA,GAAA,sBAAA,uBACwB,KAAK,KAC3B,EAAE,+BAAA,YAAY,OAAO,WAAW,gBAChC,OAAO,mBAAmB,aAC1B;IACA,MAAM,aAAa,0BAA0B,MAAM;IACnD,IAAI,CAAC,eAAe,QAAQ,UAAU,GAAG;KACvC,eAAe,QAAQ,YAAY,GAAG;KACtC,OAAO,SAAS,OAAO;KAEvB,MAAM,IAAI,cAAc,CAAC,CAAC;IAC5B;GACF;GACA,MAAM;EACR;EAEA,IAAI,CAAC,MACH,IAAI,cAAA,UACF,cAAA,SAAS,KAAK,CAAC;OAEf,MAAM,KAAK;EAIf,OAAO,MAAM,cAAc,MAAM,KAAK;CACxC;CAEC,SAAkB,UAAU;CAE7B,OAAO;AACT"}