@lolyjs/core 0.2.0-alpha.2 → 0.2.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENCE.md +9 -0
  2. package/README.md +1074 -761
  3. package/dist/{bootstrap-BiCQmSkx.d.mts → bootstrap-BfGTMUkj.d.mts} +19 -0
  4. package/dist/{bootstrap-BiCQmSkx.d.ts → bootstrap-BfGTMUkj.d.ts} +19 -0
  5. package/dist/cli.cjs +16997 -4416
  6. package/dist/cli.cjs.map +1 -1
  7. package/dist/cli.js +17007 -4416
  8. package/dist/cli.js.map +1 -1
  9. package/dist/index.cjs +14731 -1652
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.mts +295 -57
  12. package/dist/index.d.ts +295 -57
  13. package/dist/index.js +17190 -4102
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.types-DMOO-uvF.d.mts +221 -0
  16. package/dist/index.types-DMOO-uvF.d.ts +221 -0
  17. package/dist/react/cache.cjs +107 -32
  18. package/dist/react/cache.cjs.map +1 -1
  19. package/dist/react/cache.d.mts +27 -21
  20. package/dist/react/cache.d.ts +27 -21
  21. package/dist/react/cache.js +107 -32
  22. package/dist/react/cache.js.map +1 -1
  23. package/dist/react/components.cjs +10 -8
  24. package/dist/react/components.cjs.map +1 -1
  25. package/dist/react/components.js +10 -8
  26. package/dist/react/components.js.map +1 -1
  27. package/dist/react/hooks.cjs +208 -26
  28. package/dist/react/hooks.cjs.map +1 -1
  29. package/dist/react/hooks.d.mts +75 -15
  30. package/dist/react/hooks.d.ts +75 -15
  31. package/dist/react/hooks.js +208 -26
  32. package/dist/react/hooks.js.map +1 -1
  33. package/dist/react/sockets.cjs +13 -6
  34. package/dist/react/sockets.cjs.map +1 -1
  35. package/dist/react/sockets.js +13 -6
  36. package/dist/react/sockets.js.map +1 -1
  37. package/dist/react/themes.cjs +61 -18
  38. package/dist/react/themes.cjs.map +1 -1
  39. package/dist/react/themes.js +63 -20
  40. package/dist/react/themes.js.map +1 -1
  41. package/dist/runtime.cjs +544 -111
  42. package/dist/runtime.cjs.map +1 -1
  43. package/dist/runtime.d.mts +2 -2
  44. package/dist/runtime.d.ts +2 -2
  45. package/dist/runtime.js +540 -107
  46. package/dist/runtime.js.map +1 -1
  47. package/package.json +49 -4
@@ -1 +1 @@
1
- {"version":3,"sources":["../../modules/react/cache/index.ts","../../modules/react/cache/client-data-cache/index.ts"],"sourcesContent":["export * from \"./client-data-cache\";","type RouteData = {\n ok: boolean;\n status: number;\n json: any;\n};\n\ntype CacheEntry =\n | { status: \"pending\"; promise: Promise<RouteData> }\n | { status: \"fulfilled\"; value: RouteData }\n | { status: \"rejected\"; error: any };\n\n// Use window to guarantee a single shared cache instance\n// across all bundles/modules\nconst CACHE_KEY = \"__FW_DATA_CACHE__\";\n\n// Maximum number of entries in the cache (LRU)\nconst MAX_CACHE_SIZE = 100;\n\ntype CacheStore = {\n data: Map<string, CacheEntry>;\n index: Map<string, Set<string>>; // pathBase -> Set of keys\n lru: string[]; // Ordered list: most recent at end, oldest at start\n};\n\nfunction getCacheStore(): CacheStore {\n if (typeof window !== \"undefined\") {\n if (!(window as any)[CACHE_KEY]) {\n (window as any)[CACHE_KEY] = {\n data: new Map<string, CacheEntry>(),\n index: new Map<string, Set<string>>(),\n lru: [],\n };\n }\n return (window as any)[CACHE_KEY];\n }\n // Fallback for SSR (though this shouldn't be used on the client)\n return {\n data: new Map<string, CacheEntry>(),\n index: new Map<string, Set<string>>(),\n lru: [],\n };\n}\n\nconst cacheStore = getCacheStore();\nconst dataCache = cacheStore.data;\nconst pathIndex = cacheStore.index;\nconst lru = cacheStore.lru;\n\n// Helper functions for cache management\n\n/**\n * Extract base path from a cache key (removes query params)\n */\nfunction extractPathBase(key: string): string {\n return key.split(\"?\")[0];\n}\n\n/**\n * Add key to path index\n */\nfunction addToIndex(key: string): void {\n const pathBase = extractPathBase(key);\n if (!pathIndex.has(pathBase)) {\n pathIndex.set(pathBase, new Set());\n }\n pathIndex.get(pathBase)!.add(key);\n}\n\n/**\n * Remove key from path index\n */\nfunction removeFromIndex(key: string): void {\n const pathBase = extractPathBase(key);\n const keys = pathIndex.get(pathBase);\n if (keys) {\n keys.delete(key);\n if (keys.size === 0) {\n pathIndex.delete(pathBase);\n }\n }\n}\n\n/**\n * Update LRU order - move key to end (most recent)\n */\nfunction updateLRU(key: string): void {\n const index = lru.indexOf(key);\n if (index !== -1) {\n lru.splice(index, 1);\n }\n lru.push(key);\n}\n\n/**\n * Remove oldest entries if cache exceeds MAX_CACHE_SIZE\n */\nfunction evictOldest(): void {\n while (lru.length >= MAX_CACHE_SIZE && lru.length > 0) {\n const oldestKey = lru.shift()!;\n dataCache.delete(oldestKey);\n removeFromIndex(oldestKey);\n }\n}\n\n/**\n * Set cache entry and maintain indexes\n */\nfunction setCacheEntry(key: string, entry: CacheEntry): void {\n const existingEntry = dataCache.get(key);\n const wasFulfilled = existingEntry?.status === \"fulfilled\";\n \n dataCache.set(key, entry);\n \n // Only track fulfilled entries in LRU and index (not pending/rejected)\n if (entry.status === \"fulfilled\") {\n // Add to index if it wasn't already fulfilled (new entry or transition from pending/rejected)\n if (!wasFulfilled) {\n addToIndex(key);\n }\n updateLRU(key);\n evictOldest();\n } else if (wasFulfilled) {\n // If entry was fulfilled and now isn't (transitioning to pending/rejected), remove from index\n removeFromIndex(key);\n }\n}\n\n/**\n * Delete cache entry and clean up indexes\n */\nfunction deleteCacheEntry(key: string): void {\n if (dataCache.has(key)) {\n dataCache.delete(key);\n removeFromIndex(key);\n const lruIndex = lru.indexOf(key);\n if (lruIndex !== -1) {\n lru.splice(lruIndex, 1);\n }\n }\n}\n\nfunction buildDataUrl(url: string): string {\n return url + (url.includes(\"?\") ? \"&\" : \"?\") + \"__fw_data=1\";\n}\n\nasync function fetchRouteDataOnce(url: string): Promise<RouteData> {\n const dataUrl = buildDataUrl(url);\n\n const res = await fetch(dataUrl, {\n headers: {\n \"x-fw-data\": \"1\",\n Accept: \"application/json\",\n },\n });\n\n let json: any = {};\n\n try {\n const text = await res.text();\n if (text) {\n json = JSON.parse(text);\n }\n } catch (parseError) {\n console.error(\n \"[client][cache] Failed to parse response as JSON:\",\n parseError\n );\n }\n\n const result: RouteData = {\n ok: res.ok,\n status: res.status,\n json,\n };\n\n return result;\n}\n\n/**\n * Revalidates route data by removing it from the cache.\n * The next time you navigate to this route, fresh data will be fetched from the server.\n * This is a client-side function and does not require a server-side revalidation.\n *\n * @param path - The route path to revalidate (e.g., '/posts/1' or '/posts/1?page=2')\n * If query params are not included, revalidates all variants of that route.\n *\n * @example\n * ```ts\n * // After saving something to the DB, revalidate the route\n * await saveToDatabase(data);\n * revalidatePath('/posts');\n * \n * // Revalidate a specific route with query params\n * revalidatePath('/posts?page=2');\n * ```\n */\nexport function revalidatePath(path: string): void {\n // Normalize the base path (without query params)\n const normalizedPath = path.split(\"?\")[0];\n const hasQueryParams = path.includes(\"?\");\n \n // Get all keys for this path base from index (O(1) lookup)\n const keysForPath = pathIndex.get(normalizedPath);\n \n if (!keysForPath || keysForPath.size === 0) {\n return; // No entries to revalidate\n }\n \n // If the path includes specific query params, extract them\n let specificQueryParams: string | undefined;\n if (hasQueryParams) {\n const queryPart = path.split(\"?\")[1];\n // Sort query params for consistent comparison\n specificQueryParams = queryPart\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n }\n \n // Iterate only over keys for this path (much smaller set)\n const keysToDelete: string[] = [];\n for (const key of keysForPath) {\n // If specific query params were specified, check if they match\n if (hasQueryParams && specificQueryParams) {\n const [, keyQuery = \"\"] = key.split(\"?\");\n const keyQueryParams = keyQuery\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n \n if (keyQueryParams === specificQueryParams) {\n keysToDelete.push(key);\n }\n } else {\n // If no specific query params, revalidate all variants\n keysToDelete.push(key);\n }\n }\n \n // Delete matching entries\n keysToDelete.forEach((key) => {\n deleteCacheEntry(key);\n });\n}\n\n/**\n * Revalidates and refreshes the current page data.\n * Similar to Next.js's `router.refresh()`.\n * \n * This function:\n * 1. Removes the current route from cache\n * 2. Fetches fresh data from the server\n * 3. Updates window.__FW_DATA__ with the new data\n * 4. Dispatches a 'fw-data-refresh' event for components to listen to\n * \n * @returns Promise that resolves with the fresh route data\n * \n * @example\n * ```ts\n * // Refresh current page data after a mutation\n * await revalidate();\n * ```\n */\nexport async function revalidate(): Promise<RouteData> {\n if (typeof window === \"undefined\") {\n throw new Error(\"revalidate() can only be called on the client\");\n }\n\n const pathname = window.location.pathname + window.location.search;\n \n // Revalidate the path (remove from cache)\n revalidatePath(pathname);\n \n // Fetch fresh data\n const freshData = await getRouteData(pathname, { revalidate: true });\n \n // Update window.__FW_DATA__ if it exists\n if ((window as any).__FW_DATA__ && freshData.ok && freshData.json) {\n const currentData = (window as any).__FW_DATA__;\n (window as any).__FW_DATA__ = {\n ...currentData,\n pathname: pathname.split(\"?\")[0],\n params: freshData.json.params || currentData.params || {},\n props: freshData.json.props || currentData.props || {},\n metadata: freshData.json.metadata ?? currentData.metadata ?? null,\n notFound: freshData.json.notFound ?? false,\n error: freshData.json.error ?? false,\n };\n \n // Dispatch event for components to listen to\n window.dispatchEvent(new CustomEvent(\"fw-data-refresh\", {\n detail: { data: freshData },\n }));\n }\n \n return freshData;\n}\n\n/**\n * @deprecated Use `revalidatePath()` instead. This function is kept for backwards compatibility.\n */\nexport function revalidateRouteData(url: string): void {\n revalidatePath(url);\n}\n\nexport function prefetchRouteData(url: string): void {\n const key = buildDataUrl(url);\n\n const cached = dataCache.get(key);\n\n if (cached && cached.status !== \"rejected\") {\n // Update LRU if it exists and is fulfilled\n if (cached.status === \"fulfilled\") {\n updateLRU(key);\n }\n return;\n }\n\n const promise = fetchRouteDataOnce(url)\n .then((value) => {\n setCacheEntry(key, { status: \"fulfilled\", value });\n return value;\n })\n .catch((error) => {\n console.error(\"[client][cache] Error prefetching route data:\", error);\n dataCache.set(key, { status: \"rejected\", error });\n throw error;\n });\n\n dataCache.set(key, { status: \"pending\", promise });\n}\n\nexport type GetRouteDataOptions = {\n /**\n * If true, forces revalidation of route data,\n * ignoring the cache and fetching fresh data from the server.\n * Similar to Next.js's `router.refresh()` behavior.\n */\n revalidate?: boolean;\n};\n\nexport async function getRouteData(\n url: string,\n options?: GetRouteDataOptions\n): Promise<RouteData> {\n const key = buildDataUrl(url);\n\n // If revalidation is requested, remove the entry from cache\n if (options?.revalidate) {\n deleteCacheEntry(key);\n }\n\n const entry = dataCache.get(key);\n\n if (entry) {\n if (entry.status === \"fulfilled\") {\n // Update LRU: mark as recently used\n updateLRU(key);\n return entry.value;\n }\n if (entry.status === \"pending\") {\n return entry.promise;\n }\n }\n\n // No entry in cache, fetch it\n const promise = fetchRouteDataOnce(url)\n .then((value) => {\n setCacheEntry(key, { status: \"fulfilled\", value });\n return value;\n })\n .catch((error) => {\n console.error(\"[client][cache] Error fetching route data:\", error);\n dataCache.set(key, { status: \"rejected\", error });\n throw error;\n });\n\n dataCache.set(key, { status: \"pending\", promise });\n return promise;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACaA,IAAM,YAAY;AAGlB,IAAM,iBAAiB;AAQvB,SAAS,gBAA4B;AACnC,MAAI,OAAO,WAAW,aAAa;AACjC,QAAI,CAAE,OAAe,SAAS,GAAG;AAC/B,MAAC,OAAe,SAAS,IAAI;AAAA,QAC3B,MAAM,oBAAI,IAAwB;AAAA,QAClC,OAAO,oBAAI,IAAyB;AAAA,QACpC,KAAK,CAAC;AAAA,MACR;AAAA,IACF;AACA,WAAQ,OAAe,SAAS;AAAA,EAClC;AAEA,SAAO;AAAA,IACL,MAAM,oBAAI,IAAwB;AAAA,IAClC,OAAO,oBAAI,IAAyB;AAAA,IACpC,KAAK,CAAC;AAAA,EACR;AACF;AAEA,IAAM,aAAa,cA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AAQ,CAAC;AACjD,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../modules/react/cache/index.ts","../../modules/react/cache/client-data-cache/index.ts"],"sourcesContent":["export * from \"./client-data-cache\";","import type { PageMetadata } from \"@router/index\";\n\n/**\n * Response data structure from server for route data requests\n */\nexport type RouteDataResponse = {\n /** Combined props (layout + page) - kept for backward compatibility */\n props?: Record<string, unknown>;\n /** Layout props (from layout.server.hook.ts) - only present when layout hooks were executed */\n layoutProps?: Record<string, unknown>;\n /** Page props (from page.server.hook.ts) - always present in data requests */\n pageProps?: Record<string, unknown>;\n metadata?: PageMetadata | null;\n theme?: string;\n redirect?: { destination: string; permanent?: boolean };\n notFound?: boolean;\n error?: boolean;\n message?: string;\n params?: Record<string, string>;\n};\n\ntype RouteData = {\n ok: boolean;\n status: number;\n json: RouteDataResponse;\n};\n\ntype CacheEntry =\n | { status: \"pending\"; promise: Promise<RouteData> }\n | { status: \"fulfilled\"; value: RouteData }\n | { status: \"rejected\"; error: any };\n\n// Use window to guarantee a single shared cache instance\n// across all bundles/modules\nconst CACHE_KEY = \"__FW_DATA_CACHE__\";\n\n// Maximum number of entries in the cache (LRU)\nconst MAX_CACHE_SIZE = 100;\n\ntype CacheStore = {\n data: Map<string, CacheEntry>;\n index: Map<string, Set<string>>; // pathBase -> Set of keys\n lru: string[]; // Ordered list: most recent at end, oldest at start\n};\n\nfunction getCacheStore(): CacheStore {\n if (typeof window !== \"undefined\") {\n if (!(window as any)[CACHE_KEY]) {\n (window as any)[CACHE_KEY] = {\n data: new Map<string, CacheEntry>(),\n index: new Map<string, Set<string>>(),\n lru: [],\n };\n }\n return (window as any)[CACHE_KEY];\n }\n // Fallback for SSR (though this shouldn't be used on the client)\n return {\n data: new Map<string, CacheEntry>(),\n index: new Map<string, Set<string>>(),\n lru: [],\n };\n}\n\nconst cacheStore = getCacheStore();\nconst dataCache = cacheStore.data;\nconst pathIndex = cacheStore.index;\nconst lru = cacheStore.lru;\n\n// Helper functions for cache management\n\n/**\n * Extract base path from a cache key (removes query params)\n */\nfunction extractPathBase(key: string): string {\n return key.split(\"?\")[0];\n}\n\n/**\n * Add key to path index\n */\nfunction addToIndex(key: string): void {\n const pathBase = extractPathBase(key);\n if (!pathIndex.has(pathBase)) {\n pathIndex.set(pathBase, new Set());\n }\n pathIndex.get(pathBase)!.add(key);\n}\n\n/**\n * Remove key from path index\n */\nfunction removeFromIndex(key: string): void {\n const pathBase = extractPathBase(key);\n const keys = pathIndex.get(pathBase);\n if (keys) {\n keys.delete(key);\n if (keys.size === 0) {\n pathIndex.delete(pathBase);\n }\n }\n}\n\n/**\n * Update LRU order - move key to end (most recent)\n */\nfunction updateLRU(key: string): void {\n const index = lru.indexOf(key);\n if (index !== -1) {\n lru.splice(index, 1);\n }\n lru.push(key);\n}\n\n/**\n * Remove oldest entries if cache exceeds MAX_CACHE_SIZE\n */\nfunction evictOldest(): void {\n while (lru.length >= MAX_CACHE_SIZE && lru.length > 0) {\n const oldestKey = lru.shift()!;\n dataCache.delete(oldestKey);\n removeFromIndex(oldestKey);\n }\n}\n\n/**\n * Set cache entry and maintain indexes\n */\nfunction setCacheEntry(key: string, entry: CacheEntry): void {\n const existingEntry = dataCache.get(key);\n const wasFulfilled = existingEntry?.status === \"fulfilled\";\n \n dataCache.set(key, entry);\n \n // Only track fulfilled entries in LRU and index (not pending/rejected)\n if (entry.status === \"fulfilled\") {\n // Add to index if it wasn't already fulfilled (new entry or transition from pending/rejected)\n if (!wasFulfilled) {\n addToIndex(key);\n }\n updateLRU(key);\n evictOldest();\n } else if (wasFulfilled) {\n // If entry was fulfilled and now isn't (transitioning to pending/rejected), remove from index\n removeFromIndex(key);\n }\n}\n\n/**\n * Delete cache entry and clean up indexes\n */\nfunction deleteCacheEntry(key: string): void {\n if (dataCache.has(key)) {\n dataCache.delete(key);\n removeFromIndex(key);\n const lruIndex = lru.indexOf(key);\n if (lruIndex !== -1) {\n lru.splice(lruIndex, 1);\n }\n }\n}\n\nfunction buildDataUrl(url: string): string {\n return url + (url.includes(\"?\") ? \"&\" : \"?\") + \"__fw_data=1\";\n}\n\nasync function fetchRouteDataOnce(\n url: string,\n skipLayoutHooks: boolean = true\n): Promise<RouteData> {\n const dataUrl = buildDataUrl(url);\n\n const headers: Record<string, string> = {\n \"x-fw-data\": \"1\",\n Accept: \"application/json\",\n };\n\n // Send header to skip layout hooks execution in SPA navigation\n // Only skip if skipLayoutHooks is true (normal SPA navigation)\n // If false (revalidate), don't send header to force execution of all hooks\n if (skipLayoutHooks) {\n headers[\"x-skip-layout-hooks\"] = \"true\";\n }\n\n const res = await fetch(dataUrl, { headers });\n\n let json: any = {};\n\n try {\n const text = await res.text();\n if (text) {\n json = JSON.parse(text);\n }\n } catch (parseError) {\n console.error(\n \"[client][cache] Failed to parse response as JSON:\",\n parseError\n );\n }\n\n const result: RouteData = {\n ok: res.ok,\n status: res.status,\n json,\n };\n\n return result;\n}\n\n/**\n * Revalidates route data by removing it from the cache.\n * The next time you navigate to this route, fresh data will be fetched from the server.\n * This is a client-side function and does not require a server-side revalidation.\n *\n * @param path - The route path to revalidate (e.g., '/posts/1' or '/posts/1?page=2')\n * If query params are not included, revalidates all variants of that route.\n *\n * @example\n * ```ts\n * // After saving something to the DB, revalidate the route\n * await saveToDatabase(data);\n * revalidatePath('/posts');\n * \n * // Revalidate a specific route with query params\n * revalidatePath('/posts?page=2');\n * ```\n */\nexport function revalidatePath(path: string, skipAutoRevalidate: boolean = false): void {\n // Normalize the base path (without query params)\n const normalizedPath = path.split(\"?\")[0];\n const hasQueryParams = path.includes(\"?\");\n \n // Get all keys for this path base from index (O(1) lookup)\n const keysForPath = pathIndex.get(normalizedPath);\n \n if (!keysForPath || keysForPath.size === 0) {\n return; // No entries to revalidate\n }\n \n // If the path includes specific query params, extract them\n let specificQueryParams: string | undefined;\n if (hasQueryParams) {\n const queryPart = path.split(\"?\")[1];\n // Sort query params for consistent comparison\n specificQueryParams = queryPart\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n }\n \n // Iterate only over keys for this path (much smaller set)\n const keysToDelete: string[] = [];\n for (const key of keysForPath) {\n // If specific query params were specified, check if they match\n if (hasQueryParams && specificQueryParams) {\n const [, keyQuery = \"\"] = key.split(\"?\");\n const keyQueryParams = keyQuery\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n \n if (keyQueryParams === specificQueryParams) {\n keysToDelete.push(key);\n }\n } else {\n // If no specific query params, revalidate all variants\n keysToDelete.push(key);\n }\n }\n \n // Delete matching entries\n keysToDelete.forEach((key) => {\n deleteCacheEntry(key);\n });\n \n // If the revalidated path matches the current route, automatically refresh data\n // UNLESS skipAutoRevalidate is true (to prevent recursive calls from revalidate())\n if (!skipAutoRevalidate && typeof window !== \"undefined\") {\n const currentPathname = window.location.pathname;\n const currentSearch = window.location.search;\n const matchesCurrentPath = normalizedPath === currentPathname;\n \n if (matchesCurrentPath) {\n if (hasQueryParams && specificQueryParams) {\n const currentQueryParams = currentSearch\n .replace(\"?\", \"\")\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n \n if (currentQueryParams === specificQueryParams) {\n revalidate().catch((err) => {\n console.error(\n \"[client][cache] Error revalidating current route:\",\n err\n );\n });\n }\n } else {\n revalidate().catch((err) => {\n console.error(\n \"[client][cache] Error revalidating current route:\",\n err\n );\n });\n }\n }\n }\n}\n\n/**\n * Revalidates and refreshes the current page data.\n * Similar to Next.js's `router.refresh()`.\n * \n * This function:\n * 1. Removes the current route from cache\n * 2. Fetches fresh data from the server\n * 3. Updates window.__FW_DATA__ with the new data\n * 4. Dispatches a 'fw-data-refresh' event for components to listen to\n * \n * @returns Promise that resolves with the fresh route data\n * \n * @example\n * ```ts\n * // Refresh current page data after a mutation\n * await revalidate();\n * ```\n */\n// Flag to prevent recursive calls to revalidate()\nlet isRevalidating = false;\n\nexport async function revalidate(): Promise<RouteData> {\n if (typeof window === \"undefined\") {\n throw new Error(\"revalidate() can only be called on the client\");\n }\n\n // Prevent multiple simultaneous revalidations\n if (isRevalidating) {\n // Wait for the current revalidation to complete\n const key = buildDataUrl(window.location.pathname + window.location.search);\n const entry = dataCache.get(key);\n if (entry && entry.status === \"pending\") {\n return entry.promise;\n }\n // If no pending entry, something went wrong, allow the call\n }\n\n isRevalidating = true;\n try {\n const pathname = window.location.pathname + window.location.search;\n \n // Revalidate the path (remove from cache)\n // Pass a flag to prevent revalidatePath from calling revalidate() again (recursive call)\n revalidatePath(pathname, true); // true = skip auto-revalidate\n \n // Fetch fresh data\n const freshData = await getRouteData(pathname, { revalidate: true });\n \n // Update window.__FW_DATA__ if it exists\n if ((window as any).__FW_DATA__ && freshData.ok && freshData.json) {\n const currentData = (window as any).__FW_DATA__;\n \n // Update preserved layout props if new ones were returned\n if (freshData.json.layoutProps !== undefined && freshData.json.layoutProps !== null) {\n (window as any).__FW_LAYOUT_PROPS__ = freshData.json.layoutProps;\n }\n \n // Combine layout props (new or preserved) + page props\n let combinedProps = currentData.props || {};\n if (freshData.json.layoutProps !== undefined && freshData.json.layoutProps !== null) {\n // Use new layout props\n combinedProps = {\n ...freshData.json.layoutProps,\n ...(freshData.json.pageProps ?? freshData.json.props ?? {}),\n };\n } else if (freshData.json.pageProps !== undefined) {\n // Use preserved layout props + new page props\n const preservedLayoutProps = (window as any).__FW_LAYOUT_PROPS__ || {};\n combinedProps = {\n ...preservedLayoutProps,\n ...freshData.json.pageProps,\n };\n } else if (freshData.json.props) {\n // Fallback to combined props\n combinedProps = freshData.json.props;\n }\n \n (window as any).__FW_DATA__ = {\n ...currentData,\n pathname: pathname.split(\"?\")[0],\n params: freshData.json.params || currentData.params || {},\n props: combinedProps,\n metadata: freshData.json.metadata ?? currentData.metadata ?? null,\n notFound: freshData.json.notFound ?? false,\n error: freshData.json.error ?? false,\n };\n \n // Dispatch event for components to listen to\n window.dispatchEvent(new CustomEvent(\"fw-data-refresh\", {\n detail: { data: freshData },\n }));\n }\n \n return freshData;\n } finally {\n isRevalidating = false;\n }\n}\n\n/**\n * @deprecated Use `revalidatePath()` instead. This function is kept for backwards compatibility.\n */\nexport function revalidateRouteData(url: string): void {\n revalidatePath(url);\n}\n\nexport function prefetchRouteData(url: string): void {\n const key = buildDataUrl(url);\n\n const cached = dataCache.get(key);\n\n if (cached && cached.status !== \"rejected\") {\n // Update LRU if it exists and is fulfilled\n if (cached.status === \"fulfilled\") {\n updateLRU(key);\n }\n return;\n }\n\n // Prefetch uses skipLayoutHooks: true (normal navigation behavior)\n const promise = fetchRouteDataOnce(url, true)\n .then((value) => {\n setCacheEntry(key, { status: \"fulfilled\", value });\n return value;\n })\n .catch((error) => {\n console.error(\"[client][cache] Error prefetching route data:\", error);\n dataCache.set(key, { status: \"rejected\", error });\n throw error;\n });\n\n dataCache.set(key, { status: \"pending\", promise });\n}\n\nexport type GetRouteDataOptions = {\n /**\n * If true, forces revalidation of route data,\n * ignoring the cache and fetching fresh data from the server.\n * Similar to Next.js's `router.refresh()` behavior.\n */\n revalidate?: boolean;\n};\n\nexport async function getRouteData(\n url: string,\n options?: GetRouteDataOptions\n): Promise<RouteData> {\n const key = buildDataUrl(url);\n\n // If revalidation is requested, remove the entry from cache\n // This ensures we don't reuse pending or fulfilled entries\n if (options?.revalidate) {\n deleteCacheEntry(key);\n }\n\n const entry = dataCache.get(key);\n\n if (entry && !options?.revalidate) {\n // Only use cached entry if not revalidating\n if (entry.status === \"fulfilled\") {\n // Update LRU: mark as recently used\n updateLRU(key);\n return entry.value;\n }\n if (entry.status === \"pending\") {\n // Return existing pending promise to avoid duplicate requests\n return entry.promise;\n }\n }\n\n // No entry in cache (or revalidating), fetch it\n // skipLayoutHooks: true for normal SPA navigation, false when revalidating\n const skipLayoutHooks = !options?.revalidate;\n \n // Check again if an entry was added while we were processing (race condition)\n const currentEntry = dataCache.get(key);\n if (currentEntry && !options?.revalidate) {\n if (currentEntry.status === \"fulfilled\") {\n updateLRU(key);\n return currentEntry.value;\n }\n if (currentEntry.status === \"pending\") {\n return currentEntry.promise;\n }\n }\n \n // Create a new promise for this fetch\n const promise = fetchRouteDataOnce(url, skipLayoutHooks)\n .then((value) => {\n // Only set cache entry if this is still the current fetch for this key\n // This prevents race conditions where multiple revalidations happen simultaneously\n const entryAfterFetch = dataCache.get(key);\n if (!entryAfterFetch || entryAfterFetch.status === \"pending\") {\n setCacheEntry(key, { status: \"fulfilled\", value });\n }\n return value;\n })\n .catch((error) => {\n console.error(\"[client][cache] Error fetching route data:\", error);\n const entryAfterFetch = dataCache.get(key);\n if (!entryAfterFetch || entryAfterFetch.status === \"pending\") {\n dataCache.set(key, { status: \"rejected\", error });\n }\n throw error;\n });\n\n // Set pending entry - if revalidating, we already deleted it, so this is safe\n dataCache.set(key, { status: \"pending\", promise });\n \n return promise;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkCA,IAAM,YAAY;AAGlB,IAAM,iBAAiB;AAQvB,SAAS,gBAA4B;AACnC,MAAI,OAAO,WAAW,aAAa;AACjC,QAAI,CAAE,OAAe,SAAS,GAAG;AAC/B,MAAC,OAAe,SAAS,IAAI;AAAA,QAC3B,MAAM,oBAAI,IAAwB;AAAA,QAClC,OAAO,oBAAI,IAAyB;AAAA,QACpC,KAAK,CAAC;AAAA,MACR;AAAA,IACF;AACA,WAAQ,OAAe,SAAS;AAAA,EAClC;AAEA,SAAO;AAAA,IACL,MAAM,oBAAI,IAAwB;AAAA,IAClC,OAAO,oBAAI,IAAyB;AAAA,IACpC,KAAK,CAAC;AAAA,EACR;AACF;AAEA,IAAM,aAAa,cAAc;AACjC,IAAM,YAAY,WAAW;AAC7B,IAAM,YAAY,WAAW;AAC7B,IAAM,MAAM,WAAW;AAOvB,SAAS,gBAAgB,KAAqB;AAC5C,SAAO,IAAI,MAAM,GAAG,EAAE,CAAC;AACzB;AAKA,SAAS,WAAW,K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AAO,aAAa;AAAA,IACtB;AACA,QAAI,aAAa,WAAW,WAAW;AACrC,aAAO,aAAa;AAAA,IACtB;AAAA,EACF;AAGA,QAAM,UAAU,mBAAmB,KAAK,eAAe,EACpD,KAAK,CAAC,UAAU;AAGf,UAAM,kBAAkB,UAAU,IAAI,GAAG;AACzC,QAAI,CAAC,mBAAmB,gBAAgB,WAAW,WAAW;AAC5D,oBAAc,KAAK,EAAE,QAAQ,aAAa,MAAM,CAAC;AAAA,IACnD;AACA,WAAO;AAAA,EACT,CAAC,EACA,MAAM,CAAC,UAAU;AAChB,YAAQ,MAAM,8CAA8C,KAAK;AACjE,UAAM,kBAAkB,UAAU,IAAI,GAAG;AACzC,QAAI,CAAC,mBAAmB,gBAAgB,WAAW,WAAW;AAC5D,gBAAU,IAAI,KAAK,EAAE,QAAQ,YAAY,MAAM,CAAC;AAAA,IAClD;AACA,UAAM;AAAA,EACR,CAAC;AAGH,YAAU,IAAI,KAAK,EAAE,QAAQ,WAAW,QAAQ,CAAC;AAEjD,SAAO;AACT;","names":[]}
@@ -1,7 +1,31 @@
1
+ import { P as PageMetadata } from '../index.types-DMOO-uvF.mjs';
2
+ import 'express';
3
+
4
+ /**
5
+ * Response data structure from server for route data requests
6
+ */
7
+ type RouteDataResponse = {
8
+ /** Combined props (layout + page) - kept for backward compatibility */
9
+ props?: Record<string, unknown>;
10
+ /** Layout props (from layout.server.hook.ts) - only present when layout hooks were executed */
11
+ layoutProps?: Record<string, unknown>;
12
+ /** Page props (from page.server.hook.ts) - always present in data requests */
13
+ pageProps?: Record<string, unknown>;
14
+ metadata?: PageMetadata | null;
15
+ theme?: string;
16
+ redirect?: {
17
+ destination: string;
18
+ permanent?: boolean;
19
+ };
20
+ notFound?: boolean;
21
+ error?: boolean;
22
+ message?: string;
23
+ params?: Record<string, string>;
24
+ };
1
25
  type RouteData = {
2
26
  ok: boolean;
3
27
  status: number;
4
- json: any;
28
+ json: RouteDataResponse;
5
29
  };
6
30
  /**
7
31
  * Revalidates route data by removing it from the cache.
@@ -21,25 +45,7 @@ type RouteData = {
21
45
  * revalidatePath('/posts?page=2');
22
46
  * ```
23
47
  */
24
- declare function revalidatePath(path: string): void;
25
- /**
26
- * Revalidates and refreshes the current page data.
27
- * Similar to Next.js's `router.refresh()`.
28
- *
29
- * This function:
30
- * 1. Removes the current route from cache
31
- * 2. Fetches fresh data from the server
32
- * 3. Updates window.__FW_DATA__ with the new data
33
- * 4. Dispatches a 'fw-data-refresh' event for components to listen to
34
- *
35
- * @returns Promise that resolves with the fresh route data
36
- *
37
- * @example
38
- * ```ts
39
- * // Refresh current page data after a mutation
40
- * await revalidate();
41
- * ```
42
- */
48
+ declare function revalidatePath(path: string, skipAutoRevalidate?: boolean): void;
43
49
  declare function revalidate(): Promise<RouteData>;
44
50
  /**
45
51
  * @deprecated Use `revalidatePath()` instead. This function is kept for backwards compatibility.
@@ -56,4 +62,4 @@ type GetRouteDataOptions = {
56
62
  };
57
63
  declare function getRouteData(url: string, options?: GetRouteDataOptions): Promise<RouteData>;
58
64
 
59
- export { type GetRouteDataOptions, getRouteData, prefetchRouteData, revalidate, revalidatePath, revalidateRouteData };
65
+ export { type GetRouteDataOptions, type RouteDataResponse, getRouteData, prefetchRouteData, revalidate, revalidatePath, revalidateRouteData };
@@ -1,7 +1,31 @@
1
+ import { P as PageMetadata } from '../index.types-DMOO-uvF.js';
2
+ import 'express';
3
+
4
+ /**
5
+ * Response data structure from server for route data requests
6
+ */
7
+ type RouteDataResponse = {
8
+ /** Combined props (layout + page) - kept for backward compatibility */
9
+ props?: Record<string, unknown>;
10
+ /** Layout props (from layout.server.hook.ts) - only present when layout hooks were executed */
11
+ layoutProps?: Record<string, unknown>;
12
+ /** Page props (from page.server.hook.ts) - always present in data requests */
13
+ pageProps?: Record<string, unknown>;
14
+ metadata?: PageMetadata | null;
15
+ theme?: string;
16
+ redirect?: {
17
+ destination: string;
18
+ permanent?: boolean;
19
+ };
20
+ notFound?: boolean;
21
+ error?: boolean;
22
+ message?: string;
23
+ params?: Record<string, string>;
24
+ };
1
25
  type RouteData = {
2
26
  ok: boolean;
3
27
  status: number;
4
- json: any;
28
+ json: RouteDataResponse;
5
29
  };
6
30
  /**
7
31
  * Revalidates route data by removing it from the cache.
@@ -21,25 +45,7 @@ type RouteData = {
21
45
  * revalidatePath('/posts?page=2');
22
46
  * ```
23
47
  */
24
- declare function revalidatePath(path: string): void;
25
- /**
26
- * Revalidates and refreshes the current page data.
27
- * Similar to Next.js's `router.refresh()`.
28
- *
29
- * This function:
30
- * 1. Removes the current route from cache
31
- * 2. Fetches fresh data from the server
32
- * 3. Updates window.__FW_DATA__ with the new data
33
- * 4. Dispatches a 'fw-data-refresh' event for components to listen to
34
- *
35
- * @returns Promise that resolves with the fresh route data
36
- *
37
- * @example
38
- * ```ts
39
- * // Refresh current page data after a mutation
40
- * await revalidate();
41
- * ```
42
- */
48
+ declare function revalidatePath(path: string, skipAutoRevalidate?: boolean): void;
43
49
  declare function revalidate(): Promise<RouteData>;
44
50
  /**
45
51
  * @deprecated Use `revalidatePath()` instead. This function is kept for backwards compatibility.
@@ -56,4 +62,4 @@ type GetRouteDataOptions = {
56
62
  };
57
63
  declare function getRouteData(url: string, options?: GetRouteDataOptions): Promise<RouteData>;
58
64
 
59
- export { type GetRouteDataOptions, getRouteData, prefetchRouteData, revalidate, revalidatePath, revalidateRouteData };
65
+ export { type GetRouteDataOptions, type RouteDataResponse, getRouteData, prefetchRouteData, revalidate, revalidatePath, revalidateRouteData };
@@ -83,14 +83,16 @@ function deleteCacheEntry(key) {
83
83
  function buildDataUrl(url) {
84
84
  return url + (url.includes("?") ? "&" : "?") + "__fw_data=1";
85
85
  }
86
- async function fetchRouteDataOnce(url) {
86
+ async function fetchRouteDataOnce(url, skipLayoutHooks = true) {
87
87
  const dataUrl = buildDataUrl(url);
88
- const res = await fetch(dataUrl, {
89
- headers: {
90
- "x-fw-data": "1",
91
- Accept: "application/json"
92
- }
93
- });
88
+ const headers = {
89
+ "x-fw-data": "1",
90
+ Accept: "application/json"
91
+ };
92
+ if (skipLayoutHooks) {
93
+ headers["x-skip-layout-hooks"] = "true";
94
+ }
95
+ const res = await fetch(dataUrl, { headers });
94
96
  let json = {};
95
97
  try {
96
98
  const text = await res.text();
@@ -110,7 +112,7 @@ async function fetchRouteDataOnce(url) {
110
112
  };
111
113
  return result;
112
114
  }
113
- function revalidatePath(path) {
115
+ function revalidatePath(path, skipAutoRevalidate = false) {
114
116
  const normalizedPath = path.split("?")[0];
115
117
  const hasQueryParams = path.includes("?");
116
118
  const keysForPath = pathIndex.get(normalizedPath);
@@ -137,30 +139,86 @@ function revalidatePath(path) {
137
139
  keysToDelete.forEach((key) => {
138
140
  deleteCacheEntry(key);
139
141
  });
142
+ if (!skipAutoRevalidate && typeof window !== "undefined") {
143
+ const currentPathname = window.location.pathname;
144
+ const currentSearch = window.location.search;
145
+ const matchesCurrentPath = normalizedPath === currentPathname;
146
+ if (matchesCurrentPath) {
147
+ if (hasQueryParams && specificQueryParams) {
148
+ const currentQueryParams = currentSearch.replace("?", "").split("&").filter((p) => !p.startsWith("__fw_data=")).sort().join("&");
149
+ if (currentQueryParams === specificQueryParams) {
150
+ revalidate().catch((err) => {
151
+ console.error(
152
+ "[client][cache] Error revalidating current route:",
153
+ err
154
+ );
155
+ });
156
+ }
157
+ } else {
158
+ revalidate().catch((err) => {
159
+ console.error(
160
+ "[client][cache] Error revalidating current route:",
161
+ err
162
+ );
163
+ });
164
+ }
165
+ }
166
+ }
140
167
  }
168
+ var isRevalidating = false;
141
169
  async function revalidate() {
142
170
  if (typeof window === "undefined") {
143
171
  throw new Error("revalidate() can only be called on the client");
144
172
  }
145
- const pathname = window.location.pathname + window.location.search;
146
- revalidatePath(pathname);
147
- const freshData = await getRouteData(pathname, { revalidate: true });
148
- if (window.__FW_DATA__ && freshData.ok && freshData.json) {
149
- const currentData = window.__FW_DATA__;
150
- window.__FW_DATA__ = {
151
- ...currentData,
152
- pathname: pathname.split("?")[0],
153
- params: freshData.json.params || currentData.params || {},
154
- props: freshData.json.props || currentData.props || {},
155
- metadata: freshData.json.metadata ?? currentData.metadata ?? null,
156
- notFound: freshData.json.notFound ?? false,
157
- error: freshData.json.error ?? false
158
- };
159
- window.dispatchEvent(new CustomEvent("fw-data-refresh", {
160
- detail: { data: freshData }
161
- }));
162
- }
163
- return freshData;
173
+ if (isRevalidating) {
174
+ const key = buildDataUrl(window.location.pathname + window.location.search);
175
+ const entry = dataCache.get(key);
176
+ if (entry && entry.status === "pending") {
177
+ return entry.promise;
178
+ }
179
+ }
180
+ isRevalidating = true;
181
+ try {
182
+ const pathname = window.location.pathname + window.location.search;
183
+ revalidatePath(pathname, true);
184
+ const freshData = await getRouteData(pathname, { revalidate: true });
185
+ if (window.__FW_DATA__ && freshData.ok && freshData.json) {
186
+ const currentData = window.__FW_DATA__;
187
+ if (freshData.json.layoutProps !== void 0 && freshData.json.layoutProps !== null) {
188
+ window.__FW_LAYOUT_PROPS__ = freshData.json.layoutProps;
189
+ }
190
+ let combinedProps = currentData.props || {};
191
+ if (freshData.json.layoutProps !== void 0 && freshData.json.layoutProps !== null) {
192
+ combinedProps = {
193
+ ...freshData.json.layoutProps,
194
+ ...freshData.json.pageProps ?? freshData.json.props ?? {}
195
+ };
196
+ } else if (freshData.json.pageProps !== void 0) {
197
+ const preservedLayoutProps = window.__FW_LAYOUT_PROPS__ || {};
198
+ combinedProps = {
199
+ ...preservedLayoutProps,
200
+ ...freshData.json.pageProps
201
+ };
202
+ } else if (freshData.json.props) {
203
+ combinedProps = freshData.json.props;
204
+ }
205
+ window.__FW_DATA__ = {
206
+ ...currentData,
207
+ pathname: pathname.split("?")[0],
208
+ params: freshData.json.params || currentData.params || {},
209
+ props: combinedProps,
210
+ metadata: freshData.json.metadata ?? currentData.metadata ?? null,
211
+ notFound: freshData.json.notFound ?? false,
212
+ error: freshData.json.error ?? false
213
+ };
214
+ window.dispatchEvent(new CustomEvent("fw-data-refresh", {
215
+ detail: { data: freshData }
216
+ }));
217
+ }
218
+ return freshData;
219
+ } finally {
220
+ isRevalidating = false;
221
+ }
164
222
  }
165
223
  function revalidateRouteData(url) {
166
224
  revalidatePath(url);
@@ -174,7 +232,7 @@ function prefetchRouteData(url) {
174
232
  }
175
233
  return;
176
234
  }
177
- const promise = fetchRouteDataOnce(url).then((value) => {
235
+ const promise = fetchRouteDataOnce(url, true).then((value) => {
178
236
  setCacheEntry(key, { status: "fulfilled", value });
179
237
  return value;
180
238
  }).catch((error) => {
@@ -190,7 +248,7 @@ async function getRouteData(url, options) {
190
248
  deleteCacheEntry(key);
191
249
  }
192
250
  const entry = dataCache.get(key);
193
- if (entry) {
251
+ if (entry && !options?.revalidate) {
194
252
  if (entry.status === "fulfilled") {
195
253
  updateLRU(key);
196
254
  return entry.value;
@@ -199,12 +257,29 @@ async function getRouteData(url, options) {
199
257
  return entry.promise;
200
258
  }
201
259
  }
202
- const promise = fetchRouteDataOnce(url).then((value) => {
203
- setCacheEntry(key, { status: "fulfilled", value });
260
+ const skipLayoutHooks = !options?.revalidate;
261
+ const currentEntry = dataCache.get(key);
262
+ if (currentEntry && !options?.revalidate) {
263
+ if (currentEntry.status === "fulfilled") {
264
+ updateLRU(key);
265
+ return currentEntry.value;
266
+ }
267
+ if (currentEntry.status === "pending") {
268
+ return currentEntry.promise;
269
+ }
270
+ }
271
+ const promise = fetchRouteDataOnce(url, skipLayoutHooks).then((value) => {
272
+ const entryAfterFetch = dataCache.get(key);
273
+ if (!entryAfterFetch || entryAfterFetch.status === "pending") {
274
+ setCacheEntry(key, { status: "fulfilled", value });
275
+ }
204
276
  return value;
205
277
  }).catch((error) => {
206
278
  console.error("[client][cache] Error fetching route data:", error);
207
- dataCache.set(key, { status: "rejected", error });
279
+ const entryAfterFetch = dataCache.get(key);
280
+ if (!entryAfterFetch || entryAfterFetch.status === "pending") {
281
+ dataCache.set(key, { status: "rejected", error });
282
+ }
208
283
  throw error;
209
284
  });
210
285
  dataCache.set(key, { status: "pending", promise });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../modules/react/cache/client-data-cache/index.ts"],"sourcesContent":["type RouteData = {\n ok: boolean;\n status: number;\n json: any;\n};\n\ntype CacheEntry =\n | { status: \"pending\"; promise: Promise<RouteData> }\n | { status: \"fulfilled\"; value: RouteData }\n | { status: \"rejected\"; error: any };\n\n// Use window to guarantee a single shared cache instance\n// across all bundles/modules\nconst CACHE_KEY = \"__FW_DATA_CACHE__\";\n\n// Maximum number of entries in the cache (LRU)\nconst MAX_CACHE_SIZE = 100;\n\ntype CacheStore = {\n data: Map<string, CacheEntry>;\n index: Map<string, Set<string>>; // pathBase -> Set of keys\n lru: string[]; // Ordered list: most recent at end, oldest at start\n};\n\nfunction getCacheStore(): CacheStore {\n if (typeof window !== \"undefined\") {\n if (!(window as any)[CACHE_KEY]) {\n (window as any)[CACHE_KEY] = {\n data: new Map<string, CacheEntry>(),\n index: new Map<string, Set<string>>(),\n lru: [],\n };\n }\n return (window as any)[CACHE_KEY];\n }\n // Fallback for SSR (though this shouldn't be used on the client)\n return {\n data: new Map<string, CacheEntry>(),\n index: new Map<string, Set<string>>(),\n lru: [],\n };\n}\n\nconst cacheStore = getCacheStore();\nconst dataCache = cacheStore.data;\nconst pathIndex = cacheStore.index;\nconst lru = cacheStore.lru;\n\n// Helper functions for cache management\n\n/**\n * Extract base path from a cache key (removes query params)\n */\nfunction extractPathBase(key: string): string {\n return key.split(\"?\")[0];\n}\n\n/**\n * Add key to path index\n */\nfunction addToIndex(key: string): void {\n const pathBase = extractPathBase(key);\n if (!pathIndex.has(pathBase)) {\n pathIndex.set(pathBase, new Set());\n }\n pathIndex.get(pathBase)!.add(key);\n}\n\n/**\n * Remove key from path index\n */\nfunction removeFromIndex(key: string): void {\n const pathBase = extractPathBase(key);\n const keys = pathIndex.get(pathBase);\n if (keys) {\n keys.delete(key);\n if (keys.size === 0) {\n pathIndex.delete(pathBase);\n }\n }\n}\n\n/**\n * Update LRU order - move key to end (most recent)\n */\nfunction updateLRU(key: string): void {\n const index = lru.indexOf(key);\n if (index !== -1) {\n lru.splice(index, 1);\n }\n lru.push(key);\n}\n\n/**\n * Remove oldest entries if cache exceeds MAX_CACHE_SIZE\n */\nfunction evictOldest(): void {\n while (lru.length >= MAX_CACHE_SIZE && lru.length > 0) {\n const oldestKey = lru.shift()!;\n dataCache.delete(oldestKey);\n removeFromIndex(oldestKey);\n }\n}\n\n/**\n * Set cache entry and maintain indexes\n */\nfunction setCacheEntry(key: string, entry: CacheEntry): void {\n const existingEntry = dataCache.get(key);\n const wasFulfilled = existingEntry?.status === \"fulfilled\";\n \n dataCache.set(key, entry);\n \n // Only track fulfilled entries in LRU and index (not pending/rejected)\n if (entry.status === \"fulfilled\") {\n // Add to index if it wasn't already fulfilled (new entry or transition from pending/rejected)\n if (!wasFulfilled) {\n addToIndex(key);\n }\n updateLRU(key);\n evictOldest();\n } else if (wasFulfilled) {\n // If entry was fulfilled and now isn't (transitioning to pending/rejected), remove from index\n removeFromIndex(key);\n }\n}\n\n/**\n * Delete cache entry and clean up indexes\n */\nfunction deleteCacheEntry(key: string): void {\n if (dataCache.has(key)) {\n dataCache.delete(key);\n removeFromIndex(key);\n const lruIndex = lru.indexOf(key);\n if (lruIndex !== -1) {\n lru.splice(lruIndex, 1);\n }\n }\n}\n\nfunction buildDataUrl(url: string): string {\n return url + (url.includes(\"?\") ? \"&\" : \"?\") + \"__fw_data=1\";\n}\n\nasync function fetchRouteDataOnce(url: string): Promise<RouteData> {\n const dataUrl = buildDataUrl(url);\n\n const res = await fetch(dataUrl, {\n headers: {\n \"x-fw-data\": \"1\",\n Accept: \"application/json\",\n },\n });\n\n let json: any = {};\n\n try {\n const text = await res.text();\n if (text) {\n json = JSON.parse(text);\n }\n } catch (parseError) {\n console.error(\n \"[client][cache] Failed to parse response as JSON:\",\n parseError\n );\n }\n\n const result: RouteData = {\n ok: res.ok,\n status: res.status,\n json,\n };\n\n return result;\n}\n\n/**\n * Revalidates route data by removing it from the cache.\n * The next time you navigate to this route, fresh data will be fetched from the server.\n * This is a client-side function and does not require a server-side revalidation.\n *\n * @param path - The route path to revalidate (e.g., '/posts/1' or '/posts/1?page=2')\n * If query params are not included, revalidates all variants of that route.\n *\n * @example\n * ```ts\n * // After saving something to the DB, revalidate the route\n * await saveToDatabase(data);\n * revalidatePath('/posts');\n * \n * // Revalidate a specific route with query params\n * revalidatePath('/posts?page=2');\n * ```\n */\nexport function revalidatePath(path: string): void {\n // Normalize the base path (without query params)\n const normalizedPath = path.split(\"?\")[0];\n const hasQueryParams = path.includes(\"?\");\n \n // Get all keys for this path base from index (O(1) lookup)\n const keysForPath = pathIndex.get(normalizedPath);\n \n if (!keysForPath || keysForPath.size === 0) {\n return; // No entries to revalidate\n }\n \n // If the path includes specific query params, extract them\n let specificQueryParams: string | undefined;\n if (hasQueryParams) {\n const queryPart = path.split(\"?\")[1];\n // Sort query params for consistent comparison\n specificQueryParams = queryPart\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n }\n \n // Iterate only over keys for this path (much smaller set)\n const keysToDelete: string[] = [];\n for (const key of keysForPath) {\n // If specific query params were specified, check if they match\n if (hasQueryParams && specificQueryParams) {\n const [, keyQuery = \"\"] = key.split(\"?\");\n const keyQueryParams = keyQuery\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n \n if (keyQueryParams === specificQueryParams) {\n keysToDelete.push(key);\n }\n } else {\n // If no specific query params, revalidate all variants\n keysToDelete.push(key);\n }\n }\n \n // Delete matching entries\n keysToDelete.forEach((key) => {\n deleteCacheEntry(key);\n });\n}\n\n/**\n * Revalidates and refreshes the current page data.\n * Similar to Next.js's `router.refresh()`.\n * \n * This function:\n * 1. Removes the current route from cache\n * 2. Fetches fresh data from the server\n * 3. Updates window.__FW_DATA__ with the new data\n * 4. Dispatches a 'fw-data-refresh' event for components to listen to\n * \n * @returns Promise that resolves with the fresh route data\n * \n * @example\n * ```ts\n * // Refresh current page data after a mutation\n * await revalidate();\n * ```\n */\nexport async function revalidate(): Promise<RouteData> {\n if (typeof window === \"undefined\") {\n throw new Error(\"revalidate() can only be called on the client\");\n }\n\n const pathname = window.location.pathname + window.location.search;\n \n // Revalidate the path (remove from cache)\n revalidatePath(pathname);\n \n // Fetch fresh data\n const freshData = await getRouteData(pathname, { revalidate: true });\n \n // Update window.__FW_DATA__ if it exists\n if ((window as any).__FW_DATA__ && freshData.ok && freshData.json) {\n const currentData = (window as any).__FW_DATA__;\n (window as any).__FW_DATA__ = {\n ...currentData,\n pathname: pathname.split(\"?\")[0],\n params: freshData.json.params || currentData.params || {},\n props: freshData.json.props || currentData.props || {},\n metadata: freshData.json.metadata ?? currentData.metadata ?? null,\n notFound: freshData.json.notFound ?? false,\n error: freshData.json.error ?? false,\n };\n \n // Dispatch event for components to listen to\n window.dispatchEvent(new CustomEvent(\"fw-data-refresh\", {\n detail: { data: freshData },\n }));\n }\n \n return freshData;\n}\n\n/**\n * @deprecated Use `revalidatePath()` instead. This function is kept for backwards compatibility.\n */\nexport function revalidateRouteData(url: string): void {\n revalidatePath(url);\n}\n\nexport function prefetchRouteData(url: string): void {\n const key = buildDataUrl(url);\n\n const cached = dataCache.get(key);\n\n if (cached && cached.status !== \"rejected\") {\n // Update LRU if it exists and is fulfilled\n if (cached.status === \"fulfilled\") {\n updateLRU(key);\n }\n return;\n }\n\n const promise = fetchRouteDataOnce(url)\n .then((value) => {\n setCacheEntry(key, { status: \"fulfilled\", value });\n return value;\n })\n .catch((error) => {\n console.error(\"[client][cache] Error prefetching route data:\", error);\n dataCache.set(key, { status: \"rejected\", error });\n throw error;\n });\n\n dataCache.set(key, { status: \"pending\", promise });\n}\n\nexport type GetRouteDataOptions = {\n /**\n * If true, forces revalidation of route data,\n * ignoring the cache and fetching fresh data from the server.\n * Similar to Next.js's `router.refresh()` behavior.\n */\n revalidate?: boolean;\n};\n\nexport async function getRouteData(\n url: string,\n options?: GetRouteDataOptions\n): Promise<RouteData> {\n const key = buildDataUrl(url);\n\n // If revalidation is requested, remove the entry from cache\n if (options?.revalidate) {\n deleteCacheEntry(key);\n }\n\n const entry = dataCache.get(key);\n\n if (entry) {\n if (entry.status === \"fulfilled\") {\n // Update LRU: mark as recently used\n updateLRU(key);\n return entry.value;\n }\n if (entry.status === \"pending\") {\n return entry.promise;\n }\n }\n\n // No entry in cache, fetch it\n const promise = fetchRouteDataOnce(url)\n .then((value) => {\n setCacheEntry(key, { status: \"fulfilled\", value });\n return value;\n })\n .catch((error) => {\n console.error(\"[client][cache] Error fetching route data:\", error);\n dataCache.set(key, { status: \"rejected\", error });\n throw error;\n });\n\n dataCache.set(key, { status: \"pending\", promise });\n return promise;\n}\n"],"mappings":";AAaA,IAAM,YAAY;AAGlB,IAAM,iBAAiB;AAQvB,SAAS,gBAA4B;AACnC,MAAI,OAAO,WAAW,aAAa;AACjC,QAAI,CAAE,OAAe,SAAS,GAAG;AAC/B,MAAC,OAAe,SAAS,IAAI;AAAA,QAC3B,MAAM,oBAAI,IAAwB;AAAA,QAClC,OAAO,oBAAI,IAAyB;AAAA,QACpC,KAAK,CAAC;AAAA,MACR;AAAA,IACF;AACA,WAAQ,OAAe,SAAS;AAAA,EAClC;AAEA,SAAO;AAAA,IACL,MAAM,oBAAI,IAAwB;AAAA,IAClC,OAAO,oBAAI,IAAyB;AAAA,IACpC,KAAK,CAAC;AAAA,EACR;AACF;AAEA,IAAM,aAAa,cAAc;AACjC,IAAM,YAAY,WAAW;AAC7B,IAAM,YAAY,WAAW;AAC7B,IAAM,MAAM,WAAW;AAOvB,SAAS,gBAAgB,KAAqB;AAC5C,SAAO,IAAI,MAAM,GAAG,EAAE,CAAC;AACzB;AAKA,SAAS,WAAW,KAAmB;AACrC,QAAM,WAAW,gBAAgB,GAAG;AACpC,MAAI,CAAC,UAAU,IAAI,QAAQ,GAAG;AAC5B,cAAU,IAAI,UAAU,oBAAI,IAAI,CAAC;AAAA,EACnC;AACA,YAAU,IAAI,QAAQ,E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,aAAa,MAAM,CAAC;AACjD,WAAO;AAAA,EACT,CAAC,EACA,MAAM,CAAC,UAAU;AAChB,YAAQ,MAAM,iDAAiD,KAAK;AACpE,cAAU,IAAI,KAAK,EAAE,QAAQ,YAAY,MAAM,CAAC;AAChD,UAAM;AAAA,EACR,CAAC;AAEH,YAAU,IAAI,KAAK,EAAE,QAAQ,WAAW,QAAQ,CAAC;AACnD;AAWA,eAAsB,aACpB,KACA,SACoB;AACpB,QAAM,MAAM,aAAa,GAAG;AAG5B,MAAI,SAAS,YAAY;AACvB,qBAAiB,GAAG;AAAA,EACtB;AAEA,QAAM,QAAQ,UAAU,IAAI,GAAG;AAE/B,MAAI,OAAO;AACT,QAAI,MAAM,WAAW,aAAa;AAEhC,gBAAU,GAAG;AACb,aAAO,MAAM;AAAA,IACf;AACA,QAAI,MAAM,WAAW,WAAW;AAC9B,aAAO,MAAM;AAAA,IACf;AAAA,EACF;AAGA,QAAM,UAAU,mBAAmB,GAAG,EACnC,KAAK,CAAC,UAAU;AACf,kBAAc,KAAK,EAAE,QAAQ,aAAa,MAAM,CAAC;AACjD,WAAO;AAAA,EACT,CAAC,EACA,MAAM,CAAC,UAAU;AAChB,YAAQ,MAAM,8CAA8C,KAAK;AACjE,cAAU,IAAI,KAAK,EAAE,QAAQ,YAAY,MAAM,CAAC;AAChD,UAAM;AAAA,EACR,CAAC;AAEH,YAAU,IAAI,KAAK,EAAE,QAAQ,WAAW,QAAQ,CAAC;AACjD,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../modules/react/cache/client-data-cache/index.ts"],"sourcesContent":["import type { PageMetadata } from \"@router/index\";\n\n/**\n * Response data structure from server for route data requests\n */\nexport type RouteDataResponse = {\n /** Combined props (layout + page) - kept for backward compatibility */\n props?: Record<string, unknown>;\n /** Layout props (from layout.server.hook.ts) - only present when layout hooks were executed */\n layoutProps?: Record<string, unknown>;\n /** Page props (from page.server.hook.ts) - always present in data requests */\n pageProps?: Record<string, unknown>;\n metadata?: PageMetadata | null;\n theme?: string;\n redirect?: { destination: string; permanent?: boolean };\n notFound?: boolean;\n error?: boolean;\n message?: string;\n params?: Record<string, string>;\n};\n\ntype RouteData = {\n ok: boolean;\n status: number;\n json: RouteDataResponse;\n};\n\ntype CacheEntry =\n | { status: \"pending\"; promise: Promise<RouteData> }\n | { status: \"fulfilled\"; value: RouteData }\n | { status: \"rejected\"; error: any };\n\n// Use window to guarantee a single shared cache instance\n// across all bundles/modules\nconst CACHE_KEY = \"__FW_DATA_CACHE__\";\n\n// Maximum number of entries in the cache (LRU)\nconst MAX_CACHE_SIZE = 100;\n\ntype CacheStore = {\n data: Map<string, CacheEntry>;\n index: Map<string, Set<string>>; // pathBase -> Set of keys\n lru: string[]; // Ordered list: most recent at end, oldest at start\n};\n\nfunction getCacheStore(): CacheStore {\n if (typeof window !== \"undefined\") {\n if (!(window as any)[CACHE_KEY]) {\n (window as any)[CACHE_KEY] = {\n data: new Map<string, CacheEntry>(),\n index: new Map<string, Set<string>>(),\n lru: [],\n };\n }\n return (window as any)[CACHE_KEY];\n }\n // Fallback for SSR (though this shouldn't be used on the client)\n return {\n data: new Map<string, CacheEntry>(),\n index: new Map<string, Set<string>>(),\n lru: [],\n };\n}\n\nconst cacheStore = getCacheStore();\nconst dataCache = cacheStore.data;\nconst pathIndex = cacheStore.index;\nconst lru = cacheStore.lru;\n\n// Helper functions for cache management\n\n/**\n * Extract base path from a cache key (removes query params)\n */\nfunction extractPathBase(key: string): string {\n return key.split(\"?\")[0];\n}\n\n/**\n * Add key to path index\n */\nfunction addToIndex(key: string): void {\n const pathBase = extractPathBase(key);\n if (!pathIndex.has(pathBase)) {\n pathIndex.set(pathBase, new Set());\n }\n pathIndex.get(pathBase)!.add(key);\n}\n\n/**\n * Remove key from path index\n */\nfunction removeFromIndex(key: string): void {\n const pathBase = extractPathBase(key);\n const keys = pathIndex.get(pathBase);\n if (keys) {\n keys.delete(key);\n if (keys.size === 0) {\n pathIndex.delete(pathBase);\n }\n }\n}\n\n/**\n * Update LRU order - move key to end (most recent)\n */\nfunction updateLRU(key: string): void {\n const index = lru.indexOf(key);\n if (index !== -1) {\n lru.splice(index, 1);\n }\n lru.push(key);\n}\n\n/**\n * Remove oldest entries if cache exceeds MAX_CACHE_SIZE\n */\nfunction evictOldest(): void {\n while (lru.length >= MAX_CACHE_SIZE && lru.length > 0) {\n const oldestKey = lru.shift()!;\n dataCache.delete(oldestKey);\n removeFromIndex(oldestKey);\n }\n}\n\n/**\n * Set cache entry and maintain indexes\n */\nfunction setCacheEntry(key: string, entry: CacheEntry): void {\n const existingEntry = dataCache.get(key);\n const wasFulfilled = existingEntry?.status === \"fulfilled\";\n \n dataCache.set(key, entry);\n \n // Only track fulfilled entries in LRU and index (not pending/rejected)\n if (entry.status === \"fulfilled\") {\n // Add to index if it wasn't already fulfilled (new entry or transition from pending/rejected)\n if (!wasFulfilled) {\n addToIndex(key);\n }\n updateLRU(key);\n evictOldest();\n } else if (wasFulfilled) {\n // If entry was fulfilled and now isn't (transitioning to pending/rejected), remove from index\n removeFromIndex(key);\n }\n}\n\n/**\n * Delete cache entry and clean up indexes\n */\nfunction deleteCacheEntry(key: string): void {\n if (dataCache.has(key)) {\n dataCache.delete(key);\n removeFromIndex(key);\n const lruIndex = lru.indexOf(key);\n if (lruIndex !== -1) {\n lru.splice(lruIndex, 1);\n }\n }\n}\n\nfunction buildDataUrl(url: string): string {\n return url + (url.includes(\"?\") ? \"&\" : \"?\") + \"__fw_data=1\";\n}\n\nasync function fetchRouteDataOnce(\n url: string,\n skipLayoutHooks: boolean = true\n): Promise<RouteData> {\n const dataUrl = buildDataUrl(url);\n\n const headers: Record<string, string> = {\n \"x-fw-data\": \"1\",\n Accept: \"application/json\",\n };\n\n // Send header to skip layout hooks execution in SPA navigation\n // Only skip if skipLayoutHooks is true (normal SPA navigation)\n // If false (revalidate), don't send header to force execution of all hooks\n if (skipLayoutHooks) {\n headers[\"x-skip-layout-hooks\"] = \"true\";\n }\n\n const res = await fetch(dataUrl, { headers });\n\n let json: any = {};\n\n try {\n const text = await res.text();\n if (text) {\n json = JSON.parse(text);\n }\n } catch (parseError) {\n console.error(\n \"[client][cache] Failed to parse response as JSON:\",\n parseError\n );\n }\n\n const result: RouteData = {\n ok: res.ok,\n status: res.status,\n json,\n };\n\n return result;\n}\n\n/**\n * Revalidates route data by removing it from the cache.\n * The next time you navigate to this route, fresh data will be fetched from the server.\n * This is a client-side function and does not require a server-side revalidation.\n *\n * @param path - The route path to revalidate (e.g., '/posts/1' or '/posts/1?page=2')\n * If query params are not included, revalidates all variants of that route.\n *\n * @example\n * ```ts\n * // After saving something to the DB, revalidate the route\n * await saveToDatabase(data);\n * revalidatePath('/posts');\n * \n * // Revalidate a specific route with query params\n * revalidatePath('/posts?page=2');\n * ```\n */\nexport function revalidatePath(path: string, skipAutoRevalidate: boolean = false): void {\n // Normalize the base path (without query params)\n const normalizedPath = path.split(\"?\")[0];\n const hasQueryParams = path.includes(\"?\");\n \n // Get all keys for this path base from index (O(1) lookup)\n const keysForPath = pathIndex.get(normalizedPath);\n \n if (!keysForPath || keysForPath.size === 0) {\n return; // No entries to revalidate\n }\n \n // If the path includes specific query params, extract them\n let specificQueryParams: string | undefined;\n if (hasQueryParams) {\n const queryPart = path.split(\"?\")[1];\n // Sort query params for consistent comparison\n specificQueryParams = queryPart\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n }\n \n // Iterate only over keys for this path (much smaller set)\n const keysToDelete: string[] = [];\n for (const key of keysForPath) {\n // If specific query params were specified, check if they match\n if (hasQueryParams && specificQueryParams) {\n const [, keyQuery = \"\"] = key.split(\"?\");\n const keyQueryParams = keyQuery\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n \n if (keyQueryParams === specificQueryParams) {\n keysToDelete.push(key);\n }\n } else {\n // If no specific query params, revalidate all variants\n keysToDelete.push(key);\n }\n }\n \n // Delete matching entries\n keysToDelete.forEach((key) => {\n deleteCacheEntry(key);\n });\n \n // If the revalidated path matches the current route, automatically refresh data\n // UNLESS skipAutoRevalidate is true (to prevent recursive calls from revalidate())\n if (!skipAutoRevalidate && typeof window !== \"undefined\") {\n const currentPathname = window.location.pathname;\n const currentSearch = window.location.search;\n const matchesCurrentPath = normalizedPath === currentPathname;\n \n if (matchesCurrentPath) {\n if (hasQueryParams && specificQueryParams) {\n const currentQueryParams = currentSearch\n .replace(\"?\", \"\")\n .split(\"&\")\n .filter((p) => !p.startsWith(\"__fw_data=\"))\n .sort()\n .join(\"&\");\n \n if (currentQueryParams === specificQueryParams) {\n revalidate().catch((err) => {\n console.error(\n \"[client][cache] Error revalidating current route:\",\n err\n );\n });\n }\n } else {\n revalidate().catch((err) => {\n console.error(\n \"[client][cache] Error revalidating current route:\",\n err\n );\n });\n }\n }\n }\n}\n\n/**\n * Revalidates and refreshes the current page data.\n * Similar to Next.js's `router.refresh()`.\n * \n * This function:\n * 1. Removes the current route from cache\n * 2. Fetches fresh data from the server\n * 3. Updates window.__FW_DATA__ with the new data\n * 4. Dispatches a 'fw-data-refresh' event for components to listen to\n * \n * @returns Promise that resolves with the fresh route data\n * \n * @example\n * ```ts\n * // Refresh current page data after a mutation\n * await revalidate();\n * ```\n */\n// Flag to prevent recursive calls to revalidate()\nlet isRevalidating = false;\n\nexport async function revalidate(): Promise<RouteData> {\n if (typeof window === \"undefined\") {\n throw new Error(\"revalidate() can only be called on the client\");\n }\n\n // Prevent multiple simultaneous revalidations\n if (isRevalidating) {\n // Wait for the current revalidation to complete\n const key = buildDataUrl(window.location.pathname + window.location.search);\n const entry = dataCache.get(key);\n if (entry && entry.status === \"pending\") {\n return entry.promise;\n }\n // If no pending entry, something went wrong, allow the call\n }\n\n isRevalidating = true;\n try {\n const pathname = window.location.pathname + window.location.search;\n \n // Revalidate the path (remove from cache)\n // Pass a flag to prevent revalidatePath from calling revalidate() again (recursive call)\n revalidatePath(pathname, true); // true = skip auto-revalidate\n \n // Fetch fresh data\n const freshData = await getRouteData(pathname, { revalidate: true });\n \n // Update window.__FW_DATA__ if it exists\n if ((window as any).__FW_DATA__ && freshData.ok && freshData.json) {\n const currentData = (window as any).__FW_DATA__;\n \n // Update preserved layout props if new ones were returned\n if (freshData.json.layoutProps !== undefined && freshData.json.layoutProps !== null) {\n (window as any).__FW_LAYOUT_PROPS__ = freshData.json.layoutProps;\n }\n \n // Combine layout props (new or preserved) + page props\n let combinedProps = currentData.props || {};\n if (freshData.json.layoutProps !== undefined && freshData.json.layoutProps !== null) {\n // Use new layout props\n combinedProps = {\n ...freshData.json.layoutProps,\n ...(freshData.json.pageProps ?? freshData.json.props ?? {}),\n };\n } else if (freshData.json.pageProps !== undefined) {\n // Use preserved layout props + new page props\n const preservedLayoutProps = (window as any).__FW_LAYOUT_PROPS__ || {};\n combinedProps = {\n ...preservedLayoutProps,\n ...freshData.json.pageProps,\n };\n } else if (freshData.json.props) {\n // Fallback to combined props\n combinedProps = freshData.json.props;\n }\n \n (window as any).__FW_DATA__ = {\n ...currentData,\n pathname: pathname.split(\"?\")[0],\n params: freshData.json.params || currentData.params || {},\n props: combinedProps,\n metadata: freshData.json.metadata ?? currentData.metadata ?? null,\n notFound: freshData.json.notFound ?? false,\n error: freshData.json.error ?? false,\n };\n \n // Dispatch event for components to listen to\n window.dispatchEvent(new CustomEvent(\"fw-data-refresh\", {\n detail: { data: freshData },\n }));\n }\n \n return freshData;\n } finally {\n isRevalidating = false;\n }\n}\n\n/**\n * @deprecated Use `revalidatePath()` instead. This function is kept for backwards compatibility.\n */\nexport function revalidateRouteData(url: string): void {\n revalidatePath(url);\n}\n\nexport function prefetchRouteData(url: string): void {\n const key = buildDataUrl(url);\n\n const cached = dataCache.get(key);\n\n if (cached && cached.status !== \"rejected\") {\n // Update LRU if it exists and is fulfilled\n if (cached.status === \"fulfilled\") {\n updateLRU(key);\n }\n return;\n }\n\n // Prefetch uses skipLayoutHooks: true (normal navigation behavior)\n const promise = fetchRouteDataOnce(url, true)\n .then((value) => {\n setCacheEntry(key, { status: \"fulfilled\", value });\n return value;\n })\n .catch((error) => {\n console.error(\"[client][cache] Error prefetching route data:\", error);\n dataCache.set(key, { status: \"rejected\", error });\n throw error;\n });\n\n dataCache.set(key, { status: \"pending\", promise });\n}\n\nexport type GetRouteDataOptions = {\n /**\n * If true, forces revalidation of route data,\n * ignoring the cache and fetching fresh data from the server.\n * Similar to Next.js's `router.refresh()` behavior.\n */\n revalidate?: boolean;\n};\n\nexport async function getRouteData(\n url: string,\n options?: GetRouteDataOptions\n): Promise<RouteData> {\n const key = buildDataUrl(url);\n\n // If revalidation is requested, remove the entry from cache\n // This ensures we don't reuse pending or fulfilled entries\n if (options?.revalidate) {\n deleteCacheEntry(key);\n }\n\n const entry = dataCache.get(key);\n\n if (entry && !options?.revalidate) {\n // Only use cached entry if not revalidating\n if (entry.status === \"fulfilled\") {\n // Update LRU: mark as recently used\n updateLRU(key);\n return entry.value;\n }\n if (entry.status === \"pending\") {\n // Return existing pending promise to avoid duplicate requests\n return entry.promise;\n }\n }\n\n // No entry in cache (or revalidating), fetch it\n // skipLayoutHooks: true for normal SPA navigation, false when revalidating\n const skipLayoutHooks = !options?.revalidate;\n \n // Check again if an entry was added while we were processing (race condition)\n const currentEntry = dataCache.get(key);\n if (currentEntry && !options?.revalidate) {\n if (currentEntry.status === \"fulfilled\") {\n updateLRU(key);\n return currentEntry.value;\n }\n if (currentEntry.status === \"pending\") {\n return currentEntry.promise;\n }\n }\n \n // Create a new promise for this fetch\n const promise = fetchRouteDataOnce(url, skipLayoutHooks)\n .then((value) => {\n // Only set cache entry if this is still the current fetch for this key\n // This prevents race conditions where multiple revalidations happen simultaneously\n const entryAfterFetch = dataCache.get(key);\n if (!entryAfterFetch || entryAfterFetch.status === \"pending\") {\n setCacheEntry(key, { status: \"fulfilled\", value });\n }\n return value;\n })\n .catch((error) => {\n console.error(\"[client][cache] Error fetching route data:\", error);\n const entryAfterFetch = dataCache.get(key);\n if (!entryAfterFetch || entryAfterFetch.status === \"pending\") {\n dataCache.set(key, { status: \"rejected\", error });\n }\n throw error;\n });\n\n // Set pending entry - if revalidating, we already deleted it, so this is safe\n dataCache.set(key, { status: \"pending\", promise });\n \n return promise;\n}\n"],"mappings":";AAkCA,IAAM,YAAY;AAGlB,IAAM,iBAAiB;AAQvB,SAAS,gBAA4B;AACnC,MAAI,OAAO,WAAW,aAAa;AACjC,QAAI,CAAE,OAAe,SAAS,GAAG;AAC/B,MAAC,OAAe,SAAS,IAAI;AAAA,QAC3B,MAAM,oBAAI,IAAwB;AAAA,QAClC,OAAO,oBAAI,IAAyB;AAAA,QACpC,KAAK,CAAC;AAAA,MACR;AAAA,IACF;AACA,WAAQ,OAAe,SAAS;AAAA,EAClC;AAEA,SAAO;AAAA,IACL,MAAM,oBAAI,IAAwB;AAAA,IAClC,OAAO,oBAAI,IAAyB;AAAA,IACpC,KAAK,CAAC;AAAA,EACR;AACF;AAEA,IAAM,aAAa,cAAc;AACjC,IAAM,YAAY,WAAW;AAC7B,IAAM,YAAY,WAAW;AAC7B,IAAM,MAAM,WAAW;AAOvB,SAAS,gBAAgB,KAAqB;AAC5C,SAAO,IAAI,MAAM,GAAG,EAAE,CAAC;AACzB;AAKA,SAAS,WAAW,KAAmB;AACrC,QAAM,WAAW,gBAAgB,GAAG;AACpC,MAAI,CAAC,UAAU,IAAI,QAAQ,GAAG;AAC5B,cAAU,IAAI,UAAU,oBAAI,IAAI,CAAC;AAAA,EACnC;AACA,YAAU,IAAI,QAAQ,EAAG,IAAI,GAAG;AAClC;AAKA,SAAS,gBAAgB,KAAmB;AAC1C,QAAM,WAAW,gBAAgB,GAAG;AACpC,QAAM,OAAO,UAAU,IAAI,QAAQ;AACnC,MAAI,MAAM;AACR,SAAK,OAAO,GAAG;AACf,QAAI,KAAK,SAAS,GAAG;AACnB,gBAAU,OAAO,QAAQ;AAAA,IAC3B;AAAA,EACF;AACF;AAKA,SAAS,UAAU,KAAmB;AACpC,QAAM,QAAQ,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BAAgB,WAAW,WAAW;AAC5D,gBAAU,IAAI,KAAK,EAAE,QAAQ,YAAY,MAAM,CAAC;AAAA,IAClD;AACA,UAAM;AAAA,EACR,CAAC;AAGH,YAAU,IAAI,KAAK,EAAE,QAAQ,WAAW,QAAQ,CAAC;AAEjD,SAAO;AACT;","names":[]}
@@ -130,14 +130,16 @@ function setCacheEntry(key, entry) {
130
130
  function buildDataUrl(url) {
131
131
  return url + (url.includes("?") ? "&" : "?") + "__fw_data=1";
132
132
  }
133
- async function fetchRouteDataOnce(url) {
133
+ async function fetchRouteDataOnce(url, skipLayoutHooks = true) {
134
134
  const dataUrl = buildDataUrl(url);
135
- const res = await fetch(dataUrl, {
136
- headers: {
137
- "x-fw-data": "1",
138
- Accept: "application/json"
139
- }
140
- });
135
+ const headers = {
136
+ "x-fw-data": "1",
137
+ Accept: "application/json"
138
+ };
139
+ if (skipLayoutHooks) {
140
+ headers["x-skip-layout-hooks"] = "true";
141
+ }
142
+ const res = await fetch(dataUrl, { headers });
141
143
  let json = {};
142
144
  try {
143
145
  const text = await res.text();
@@ -166,7 +168,7 @@ function prefetchRouteData(url) {
166
168
  }
167
169
  return;
168
170
  }
169
- const promise = fetchRouteDataOnce(url).then((value) => {
171
+ const promise = fetchRouteDataOnce(url, true).then((value) => {
170
172
  setCacheEntry(key, { status: "fulfilled", value });
171
173
  return value;
172
174
  }).catch((error) => {