@tanstack/react-router
Version:
Modern and scalable routing for React applications
79 lines (78 loc) • 2.7 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const Match = require("./Match.cjs");
const ClientOnly = require("./ClientOnly.cjs");
function _interopNamespaceDefault(e) {
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
if (e) {
for (const k in e) {
if (k !== "default") {
const d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: () => e[k]
});
}
}
}
n.default = e;
return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
function isModuleNotFoundError(error) {
if (typeof (error == null ? void 0 : error.message) !== "string") return false;
return error.message.startsWith("Failed to fetch dynamically imported module") || error.message.startsWith("error loading dynamically imported module") || error.message.startsWith("Importing a module script failed");
}
function lazyRouteComponent(importer, exportName, ssr) {
let loadPromise;
let comp;
let error;
let reload;
const load = () => {
if (typeof document === "undefined" && (ssr == null ? void 0 : ssr()) === false) {
comp = () => null;
return Promise.resolve();
}
if (!loadPromise) {
loadPromise = importer().then((res) => {
loadPromise = void 0;
comp = res[exportName ?? "default"];
}).catch((err) => {
error = err;
if (isModuleNotFoundError(error)) {
if (error instanceof Error && typeof window !== "undefined" && typeof sessionStorage !== "undefined") {
const storageKey = `tanstack_router_reload:${error.message}`;
if (!sessionStorage.getItem(storageKey)) {
sessionStorage.setItem(storageKey, "1");
reload = true;
}
}
}
});
}
return loadPromise;
};
const lazyComp = function Lazy(props) {
if (reload) {
window.location.reload();
throw new Promise(() => {
});
}
if (error) {
throw error;
}
if (!comp) {
throw load();
}
if ((ssr == null ? void 0 : ssr()) === false) {
return /* @__PURE__ */ jsxRuntime.jsx(ClientOnly.ClientOnly, { fallback: /* @__PURE__ */ jsxRuntime.jsx(Match.Outlet, {}), children: React__namespace.createElement(comp, props) });
}
return React__namespace.createElement(comp, props);
};
lazyComp.preload = load;
return lazyComp;
}
exports.lazyRouteComponent = lazyRouteComponent;
//# sourceMappingURL=lazyRouteComponent.cjs.map
;