|
1 | 1 | import type { ComposableCacheEntry, ComposableCacheHandler } from "types/cache"; |
2 | 2 | import type { CacheKey } from "types/overrides"; |
3 | | -import { writeTags } from "utils/cache"; |
| 3 | +import { createCacheKey, writeTags } from "utils/cache"; |
4 | 4 | import { fromReadableStream, toReadableStream } from "utils/stream"; |
5 | 5 | import { debug, warn } from "./logger"; |
6 | 6 |
|
7 | 7 | const pendingWritePromiseMap = new Map<string, Promise<ComposableCacheEntry>>(); |
8 | | -/** |
9 | | - * Get the cache key for a composable entry. |
10 | | - * Composable cache keys are a special cases as they are a stringified version of a tuple composed of a representation of the BUILD_ID and the actual key. |
11 | | - * @param key The composable cache key |
12 | | - * @returns The composable cache key. |
13 | | - */ |
14 | | -function getComposableCacheKey(key: string): CacheKey<"composable"> { |
15 | | - try { |
16 | | - const shouldPrependBuildId = |
17 | | - globalThis.openNextConfig.dangerous?.persistentDataCache !== true; |
18 | | - const [_buildId, ...rest] = JSON.parse(key); |
19 | | - return { |
20 | | - cacheType: "composable", |
21 | | - buildId: shouldPrependBuildId ? _buildId : undefined, |
22 | | - baseKey: JSON.stringify(rest), |
23 | | - } as CacheKey<"composable">; |
24 | | - } catch (e) { |
25 | | - warn("Error while parsing composable cache key", e); |
26 | | - // If we fail to parse the key, we just return it as is |
27 | | - // This is not ideal, but we don't want to crash the application |
28 | | - return { |
29 | | - cacheType: "composable", |
30 | | - buildId: process.env.NEXT_BUILD_ID ?? "undefined-build-id", |
31 | | - baseKey: key, |
32 | | - }; |
33 | | - } |
34 | | -} |
35 | 8 |
|
36 | 9 | export default { |
37 | 10 | async get(key: string) { |
38 | 11 | try { |
39 | | - const cacheKey = getComposableCacheKey(key); |
| 12 | + const cacheKey = createCacheKey({ key, type: "composable" }); |
40 | 13 | // We first check if we have a pending write for this cache key |
41 | 14 | // If we do, we return the pending promise instead of fetching the cache |
42 | 15 | if (pendingWritePromiseMap.has(cacheKey.baseKey)) { |
@@ -82,7 +55,7 @@ export default { |
82 | 55 | }, |
83 | 56 |
|
84 | 57 | async set(key: string, pendingEntry: Promise<ComposableCacheEntry>) { |
85 | | - const cacheKey = getComposableCacheKey(key); |
| 58 | + const cacheKey = createCacheKey({ key, type: "composable" }); |
86 | 59 | pendingWritePromiseMap.set(cacheKey.baseKey, pendingEntry); |
87 | 60 | const entry = await pendingEntry.finally(() => { |
88 | 61 | pendingWritePromiseMap.delete(cacheKey.baseKey); |
|
0 commit comments