Skip to content

Commit 84d3634

Browse files
author
Nicolas Dorseuil
committed
review fix
1 parent bc29e07 commit 84d3634

File tree

5 files changed

+23
-35
lines changed

5 files changed

+23
-35
lines changed

.changeset/slow-walls-allow.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
Add an option to keep the data cache persistent between deployments.
66

7-
BREAKING CHANGE: Incremental cache keys are now an object of type `CacheKey` instead of a string. The new type includes properties like `baseKey`, `buildId`, and `cacheType`. Build_id is automatically provided according to the cache type and the `dangerous.persistentDataCache` option. Up to the Incremental Cache implementation to use it as they see fit.
7+
BREAKING CHANGE: Incremental cache keys are now an object of type `CacheKey` instead of a string. The new type includes properties like `baseKey`, `buildId`, and `cacheType`. Build_id is automatically provided according to the cache type and the `dangerous.persistentDataCache` option. Up to the Incremental Cache implementation to use it as they see fit.
8+
**Custom Incremental cache will need to be updated**

packages/open-next/src/adapters/composable-cache.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
11
import type { ComposableCacheEntry, ComposableCacheHandler } from "types/cache";
22
import type { CacheKey } from "types/overrides";
3-
import { writeTags } from "utils/cache";
3+
import { createCacheKey, writeTags } from "utils/cache";
44
import { fromReadableStream, toReadableStream } from "utils/stream";
55
import { debug, warn } from "./logger";
66

77
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-
}
358

369
export default {
3710
async get(key: string) {
3811
try {
39-
const cacheKey = getComposableCacheKey(key);
12+
const cacheKey = createCacheKey({ key, type: "composable" });
4013
// We first check if we have a pending write for this cache key
4114
// If we do, we return the pending promise instead of fetching the cache
4215
if (pendingWritePromiseMap.has(cacheKey.baseKey)) {
@@ -82,7 +55,7 @@ export default {
8255
},
8356

8457
async set(key: string, pendingEntry: Promise<ComposableCacheEntry>) {
85-
const cacheKey = getComposableCacheKey(key);
58+
const cacheKey = createCacheKey({ key, type: "composable" });
8659
pendingWritePromiseMap.set(cacheKey.baseKey, pendingEntry);
8760
const entry = await pendingEntry.finally(() => {
8861
pendingWritePromiseMap.delete(cacheKey.baseKey);

packages/open-next/src/overrides/incrementalCache/fs-dev.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "node:fs/promises";
22
import path from "node:path";
33

4-
import type { IncrementalCache } from "types/overrides.js";
4+
import type { CacheKey, IncrementalCache } from "types/overrides.js";
55
import { getMonorepoRelativePath } from "utils/normalize-path";
66

77
const basePath = path.join(getMonorepoRelativePath(), "cache");
@@ -12,7 +12,9 @@ const getCacheKey = (key: string) => {
1212

1313
const cache: IncrementalCache = {
1414
name: "fs-dev",
15-
get: async ({ baseKey }) => {
15+
get: async (cacheKey: CacheKey) => {
16+
// This cache is always shared across build (the build id is not used)
17+
const { baseKey } = cacheKey;
1618
const fileData = await fs.readFile(getCacheKey(baseKey), "utf-8");
1719
const data = JSON.parse(fileData);
1820
const { mtime } = await fs.stat(getCacheKey(baseKey));

packages/open-next/src/overrides/incrementalCache/multi-tier-ddb-s3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const awsFetch = (body: RequestInit["body"], type: "get" | "set" = "get") => {
4242
};
4343

4444
const buildDynamoKey = (key: CacheKey<CacheEntryType>) => {
45-
return `__meta_${key.buildId ? `${key.buildId}_` : ""}${key.baseKey}`;
45+
return `__meta_${key.buildId ?? ""}_${key.baseKey}`;
4646
};
4747

4848
/**

packages/open-next/src/utils/cache.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
OriginalTagCacheWriteInput,
66
WithLastModified,
77
} from "types/overrides";
8-
import { debug } from "../adapters/logger";
8+
import { debug, warn } from "../adapters/logger";
99

1010
export async function hasBeenRevalidated(
1111
key: string,
@@ -100,6 +100,18 @@ export function createCacheKey<CacheType extends CacheEntryType>({
100100
baseKey: key,
101101
} as CacheKey<CacheType>;
102102
}
103+
let baseKey = key;
104+
if (type === "composable") {
105+
try {
106+
const [_buildId, ...rest] = JSON.parse(key);
107+
baseKey = JSON.stringify(rest);
108+
} catch (e) {
109+
warn("Error while parsing composable cache key", e);
110+
// If we fail to parse the key, we just return it as is
111+
// This is not ideal, but we don't want to crash the application
112+
baseKey = key;
113+
}
114+
}
103115
if (shouldPrependBuildId) {
104116
return {
105117
cacheType: type,

0 commit comments

Comments
 (0)