"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.tsx var src_exports = {}; __export(src_exports, { cache: () => cache, cacheClientLoader: () => cacheClientLoader, configureGlobalCache: () => configureGlobalCache, createCacheAdapter: () => createCacheAdapter, decacheClientLoader: () => decacheClientLoader, invalidateCache: () => invalidateCache, useCacheInvalidator: () => useCacheInvalidator, useCachedLoaderData: () => useCachedLoaderData, useSwrData: () => useSwrData }); module.exports = __toCommonJS(src_exports); var import_react = require("@remix-run/react"); var import_react2 = require("react"); var import_react3 = __toESM(require("react"), 1); var map = /* @__PURE__ */ new Map(); var cache = { getItem: async (key) => map.get(key), setItem: async (key, val) => map.set(key, val), removeItem: async (key) => map.delete(key) }; var augmentStorageAdapter = (storage) => { return { getItem: async (key) => { try { const item = JSON.parse(storage.getItem(key) || ""); return item; } catch (e) { return storage.getItem(key); } }, setItem: async (key, val) => storage.setItem(key, JSON.stringify(val)), removeItem: async (key) => storage.removeItem(key) }; }; var createCacheAdapter = (adapter) => { if (typeof document === "undefined") return { adapter: void 0 }; const adapterInstance = adapter(); if (adapterInstance instanceof Storage) { return { adapter: augmentStorageAdapter(adapterInstance) }; } return { adapter: adapter() }; }; var configureGlobalCache = (newCacheInstance) => { if (typeof document === "undefined") return; const newCache = newCacheInstance(); if (newCache instanceof Storage) { cache = augmentStorageAdapter(newCache); return; } if (newCache) { cache = newCache; } }; var decacheClientLoader = async ({ request, serverAction }, { key = constructKey(request), adapter = cache }) => { const data = await serverAction(); await adapter.removeItem(key); return data; }; var cacheClientLoader = async ({ request, serverLoader }, { type = "swr", key = constructKey(request), adapter = cache } = { type: "swr", key: constructKey(request), adapter: cache }) => { const existingData = await adapter.getItem(key); if (type === "normal" && existingData) { return { ...existingData, serverData: existingData, deferredServerData: void 0, key }; } const data = existingData ? existingData : await serverLoader(); await adapter.setItem(key, data); const deferredServerData = existingData ? serverLoader() : void 0; return { ...data != null ? data : existingData, serverData: data, deferredServerData, key }; }; function useCachedLoaderData({ adapter = cache } = { adapter: cache }) { const loaderData = (0, import_react.useLoaderData)(); const navigate = (0, import_react.useNavigate)(); const [freshData, setFreshData] = (0, import_react2.useState)({ ..."serverData" in loaderData ? loaderData.serverData : loaderData }); (0, import_react2.useEffect)(() => { let isMounted = true; if (loaderData.deferredServerData) { loaderData.deferredServerData.then((newData) => { if (isMounted) { adapter.setItem(loaderData.key, newData); setFreshData(newData); } }).catch((e) => { const res = e instanceof Response ? e : void 0; if (res && res.status === 302) { const to = res.headers.get("Location"); to && navigate(to); } else { throw e; } }); } return () => { isMounted = false; }; }, [loaderData]); (0, import_react2.useEffect)(() => { if (loaderData.serverData && JSON.stringify(loaderData.serverData) !== JSON.stringify(freshData)) { setFreshData(loaderData.serverData); } }, [loaderData == null ? void 0 : loaderData.serverData]); return { ...loaderData, ...freshData, cacheKey: loaderData.key, invalidate: () => invalidateCache(loaderData.key) }; } var constructKey = (request) => { const url = new URL(request.url); return url.pathname + url.search + url.hash; }; var invalidateCache = async (key) => { const keys = Array.isArray(key) ? key : [key]; for (const k of keys) { await cache.removeItem(k); } }; var useCacheInvalidator = () => ({ invalidateCache }); function useSwrData({ serverData, deferredServerData, ...args }) { return function SWR({ children }) { return /* @__PURE__ */ import_react3.default.createElement(import_react3.default.Fragment, null, deferredServerData ? /* @__PURE__ */ import_react3.default.createElement(import_react3.default.Suspense, { fallback: children(serverData) }, /* @__PURE__ */ import_react3.default.createElement(import_react.Await, { resolve: deferredServerData }, children)) : children(serverData != null ? serverData : args)); }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { cache, cacheClientLoader, configureGlobalCache, createCacheAdapter, decacheClientLoader, invalidateCache, useCacheInvalidator, useCachedLoaderData, useSwrData });