Skip to content

Implement persistent data cache option and update cache key logic #920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-walls-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": minor
---

Add an option to keep the data cache persistent between deployments
36 changes: 24 additions & 12 deletions packages/open-next/src/adapters/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type {
IncrementalCacheContext,
IncrementalCacheValue,
} from "types/cache";
import { getTagsFromValue, hasBeenRevalidated, writeTags } from "utils/cache";
import {
createCacheKey,
getTagsFromValue,
hasBeenRevalidated,
writeTags,
} from "utils/cache";
import { isBinaryContentType } from "../utils/binary";
import { debug, error, warn } from "./logger";

Expand Down Expand Up @@ -31,7 +36,7 @@ function isFetchCache(
// We need to use globalThis client here as this class can be defined at load time in next 12 but client is not available at load time
export default class Cache {
public async get(
key: string,
baseKey: string,
// fetchCache is for next 13.5 and above, kindHint is for next 14 and above and boolean is for earlier versions
options?:
| boolean
Expand All @@ -49,22 +54,24 @@ export default class Cache {

const softTags = typeof options === "object" ? options.softTags : [];
const tags = typeof options === "object" ? options.tags : [];
return isFetchCache(options)
? this.getFetchCache(key, softTags, tags)
: this.getIncrementalCache(key);
const isDataCache = isFetchCache(options);
return isDataCache
? this.getFetchCache(baseKey, softTags, tags)
: this.getIncrementalCache(baseKey);
}

async getFetchCache(key: string, softTags?: string[], tags?: string[]) {
debug("get fetch cache", { key, softTags, tags });
async getFetchCache(baseKey: string, softTags?: string[], tags?: string[]) {
debug("get fetch cache", { baseKey, softTags, tags });
try {
const key = createCacheKey(baseKey, true);
const cachedEntry = await globalThis.incrementalCache.get(key, "fetch");

if (cachedEntry?.value === undefined) return null;

const _tags = [...(tags ?? []), ...(softTags ?? [])];
const _lastModified = cachedEntry.lastModified ?? Date.now();
const _hasBeenRevalidated = await hasBeenRevalidated(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a comment explaining why the base key is used (inline + on the method)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-opening.
Could we expand on why this is not using the obj key?

key,
baseKey,
_tags,
cachedEntry,
);
Expand Down Expand Up @@ -105,8 +112,11 @@ export default class Cache {
}
}

async getIncrementalCache(key: string): Promise<CacheHandlerValue | null> {
async getIncrementalCache(
baseKey: string,
): Promise<CacheHandlerValue | null> {
try {
const key = createCacheKey(baseKey, false);
const cachedEntry = await globalThis.incrementalCache.get(key, "cache");

if (!cachedEntry?.value) {
Expand All @@ -119,7 +129,7 @@ export default class Cache {
const tags = getTagsFromValue(cacheData);
const _lastModified = cachedEntry.lastModified ?? Date.now();
const _hasBeenRevalidated = await hasBeenRevalidated(
key,
baseKey,
tags,
cachedEntry,
);
Expand Down Expand Up @@ -191,20 +201,22 @@ export default class Cache {
}

async set(
key: string,
baseKey: string,
data?: IncrementalCacheValue,
ctx?: IncrementalCacheContext,
): Promise<void> {
if (globalThis.openNextConfig.dangerous?.disableIncrementalCache) {
return;
}
const key = createCacheKey(baseKey, data?.kind === "FETCH");
// This one might not even be necessary anymore
// Better be safe than sorry
const detachedPromise = globalThis.__openNextAls
.getStore()
?.pendingPromiseRunner.withResolvers<void>();
try {
if (data === null || data === undefined) {
// only case where we delete the cache is for ISR/SSG cache
await globalThis.incrementalCache.delete(key);
} else {
const revalidate = this.extractRevalidateForSet(ctx);
Expand Down Expand Up @@ -302,7 +314,7 @@ export default class Cache {
}
}

await this.updateTagsOnSet(key, data, ctx);
await this.updateTagsOnSet(baseKey, data, ctx);
debug("Finished setting cache");
} catch (e) {
error("Failed to set cache", e);
Expand Down
28 changes: 26 additions & 2 deletions packages/open-next/src/adapters/composable-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,33 @@ import { fromReadableStream, toReadableStream } from "utils/stream";
import { debug } from "./logger";

const pendingWritePromiseMap = new Map<string, Promise<ComposableCacheEntry>>();
/**
* Get the cache key for a composable entry.
* 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.
* @param key The composable cache key
* @returns The composable cache key.
*/
function getComposableCacheKey(key: string): string {
try {
const shouldPrependBuildId =
globalThis.openNextConfig.dangerous?.persistentDataCache !== true;
if (shouldPrependBuildId) {
return key;
}
const [_buildId, ...rest] = JSON.parse(key);
return JSON.stringify([...rest]);
} catch (e) {
debug("Error while parsing composable cache key", e);
// If we fail to parse the key, we just return it as is
// This is not ideal, but we don't want to crash the application
return key;
}
}

export default {
async get(cacheKey: string) {
async get(key: string) {
try {
const cacheKey = getComposableCacheKey(key);
Copy link
Contributor

@vicb vicb Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to move that l45 and use key on l42 and 43?

edit:
Oh actually I think that's what we want as they might differ.
Should we add a comment warning that key should not be used?

edit2:
But I guess that I have the same question as the tag cache: why wouldn't we use the build?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it doesn't really matter, it's in memory anyway.
For the tag cache, technically we don't need the build_id as it is time based.
There is one case actually when it is useful to have the build_id, it's for more complex deployment (like blue/green).
I think what make sense is just to pass the CacheKey to the tag cache as well and let the implementation decides.
For the default one, I think we should just follow the persistentDataCache option
WDYT ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

// We first check if we have a pending write for this cache key
// If we do, we return the pending promise instead of fetching the cache
if (pendingWritePromiseMap.has(cacheKey)) {
Expand Down Expand Up @@ -55,7 +78,8 @@ export default {
}
},

async set(cacheKey: string, pendingEntry: Promise<ComposableCacheEntry>) {
async set(key: string, pendingEntry: Promise<ComposableCacheEntry>) {
const cacheKey = getComposableCacheKey(key);
pendingWritePromiseMap.set(cacheKey, pendingEntry);
const entry = await pendingEntry.finally(() => {
pendingWritePromiseMap.delete(cacheKey);
Expand Down
3 changes: 1 addition & 2 deletions packages/open-next/src/overrides/incrementalCache/fs-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import path from "node:path";
import type { IncrementalCache } from "types/overrides.js";
import { getMonorepoRelativePath } from "utils/normalize-path";

const buildId = process.env.NEXT_BUILD_ID;
const basePath = path.join(getMonorepoRelativePath(), `cache/${buildId}`);
const basePath = path.join(getMonorepoRelativePath(), "cache");

const getCacheKey = (key: string) => {
return path.join(basePath, `${key}.cache`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const awsFetch = (body: RequestInit["body"], type: "get" | "set" = "get") => {
};

const buildDynamoKey = (key: string) => {
const { NEXT_BUILD_ID } = process.env;
return `__meta_${NEXT_BUILD_ID}_${key}`;
return `__meta_${key}`;
};

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/open-next/src/overrides/incrementalCache/s3-lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ const awsFetch = async (key: string, options: RequestInit) => {
};

function buildS3Key(key: string, extension: Extension) {
const { CACHE_BUCKET_KEY_PREFIX, NEXT_BUILD_ID } = process.env;
const { CACHE_BUCKET_KEY_PREFIX } = process.env;
return path.posix.join(
CACHE_BUCKET_KEY_PREFIX ?? "",
extension === "fetch" ? "__fetch" : "",
NEXT_BUILD_ID ?? "",
extension === "fetch" ? key : `${key}.${extension}`,
);
}
Expand Down
9 changes: 2 additions & 7 deletions packages/open-next/src/overrides/incrementalCache/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ import type { IncrementalCache } from "types/overrides";
import { awsLogger } from "../../adapters/logger";
import { parseNumberFromEnv } from "../../adapters/util";

const {
CACHE_BUCKET_REGION,
CACHE_BUCKET_KEY_PREFIX,
NEXT_BUILD_ID,
CACHE_BUCKET_NAME,
} = process.env;
const { CACHE_BUCKET_REGION, CACHE_BUCKET_KEY_PREFIX, CACHE_BUCKET_NAME } =
process.env;

function parseS3ClientConfigFromEnv(): S3ClientConfig {
return {
Expand All @@ -34,7 +30,6 @@ function buildS3Key(key: string, extension: Extension) {
return path.posix.join(
CACHE_BUCKET_KEY_PREFIX ?? "",
extension === "fetch" ? "__fetch" : "",
NEXT_BUILD_ID ?? "",
extension === "fetch" ? key : `${key}.${extension}`,
);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/open-next/src/types/open-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ export interface DangerousOptions {
headersAndCookiesPriority?: (
event: InternalEvent,
) => "middleware" | "handler";

/**
* Persist data cache between deployments.
* Next.js claims that the data cache is persistent (not true for `use cache` and it depends on how you build/deploy otherwise).
* By default, every entry will be prepended with the BUILD_ID, when enabled it will not.
* This means that the data cache will be persistent between deployments.
* This is useful in a lot of cases, but be aware that it could cause issues, especially with `use cache` or `unstable_cache` (Some external change may not be reflected in the key, leading to stale data)
* @default false
*/
persistentDataCache?: boolean;
}

export type BaseOverride = {
Expand Down
16 changes: 16 additions & 0 deletions packages/open-next/src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,19 @@ export async function writeTags(
// Here we know that we have the correct type
await globalThis.tagCache.writeTags(tagsToWrite as any);
}

export function createCacheKey(key: string, isDataCache: boolean): string {
// We always prepend the build ID to the cache key for ISR/SSG cache entry
// For data cache, we only prepend the build ID if the persistentDataCache is not enabled
Comment on lines +90 to +91
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"prepend" do not sound right any more.

would be great to update the comments and move to their corresponding case.

const shouldPrependBuildId =
globalThis.openNextConfig.dangerous?.persistentDataCache !== true ||
!isDataCache;
if (shouldPrependBuildId) {
// If we don't have a build ID, we just return the key as is
if (!process.env.NEXT_BUILD_ID) {
return key;
}
return `${process.env.NEXT_BUILD_ID}/${key}`;
}
return key;
}
6 changes: 5 additions & 1 deletion packages/tests-unit/tests/adapters/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { vi } from "vitest";

declare global {
var openNextConfig: {
dangerous: { disableIncrementalCache?: boolean; disableTagCache?: boolean };
dangerous: {
disableIncrementalCache?: boolean;
disableTagCache?: boolean;
persistentDataCache?: boolean;
};
};
var isNextAfter15: boolean;
}
Expand Down
62 changes: 62 additions & 0 deletions packages/tests-unit/tests/utils/cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { createCacheKey } from "@opennextjs/aws/utils/cache.js";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";

describe("createCacheKey", () => {
const originalEnv = process.env;
const originalGlobalThis = globalThis as any;

beforeEach(() => {
vi.resetModules();
process.env = { ...originalEnv };

// Mock globalThis.openNextConfig
if (!globalThis.openNextConfig) {
globalThis.openNextConfig = {
dangerous: {},
};
}
});

afterEach(() => {
process.env = originalEnv;
globalThis.openNextConfig = originalGlobalThis.openNextConfig;
});

test("prepends build ID for non-data cache entries", () => {
process.env.NEXT_BUILD_ID = "test-build-id";
const key = "test-key";

const result = createCacheKey(key, false);

expect(result).toBe("test-build-id/test-key");
});

test("prepends build ID for data cache when persistentDataCache is not enabled", () => {
process.env.NEXT_BUILD_ID = "test-build-id";
globalThis.openNextConfig.dangerous.persistentDataCache = false;
const key = "test-key";

const result = createCacheKey(key, true);

expect(result).toBe("test-build-id/test-key");
});

test("does not prepend build ID for data cache when persistentDataCache is enabled", () => {
process.env.NEXT_BUILD_ID = "test-build-id";
globalThis.openNextConfig.dangerous.persistentDataCache = true;
const key = "test-key";

const result = createCacheKey(key, true);

expect(result).toBe("test-key");
});

test("handles missing build ID", () => {
process.env.NEXT_BUILD_ID = undefined;
const key = "test-key";

const result = createCacheKey(key, false);

expect(result).toBe("test-key");
});
});
Loading