Skip to content
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

Do not serve stale values from the assets when there is no KV #344

Merged
merged 7 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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/fluffy-taxis-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

bump `@opennextjs/aws` dependency to `https://pkg.pr.new/@opennextjs/aws@726`
5 changes: 5 additions & 0 deletions .changeset/spotty-baboons-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
vicb marked this conversation as resolved.
Show resolved Hide resolved
"@opennextjs/cloudflare": patch
---

Fix: make sure `kvCache` discards expired entries
2 changes: 1 addition & 1 deletion packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"dependencies": {
"@ast-grep/napi": "^0.34.1",
"@dotenvx/dotenvx": "catalog:",
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@724",
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@726",
"enquirer": "^2.4.1",
"glob": "catalog:",
"ts-morph": "catalog:",
Expand Down
32 changes: 30 additions & 2 deletions packages/cloudflare/src/api/kvCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ export const CACHE_ASSET_DIR = "cnd-cgi/_next_cache";

export const STATUS_DELETED = 1;

// https://github.com/vercel/next.js/blob/9a1cd356/packages/next/src/server/response-cache/types.ts#L26-L38
export type CachedFetchValue = {
kind: "FETCH";
data: {
headers: { [k: string]: string };
body: string;
url: string;
status?: number;
// field used by older versions of Next.js (see: https://github.com/vercel/next.js/blob/fda1ecc/packages/next/src/server/response-cache/types.ts#L23)
tags?: string[];
};
// tags are only present with file-system-cache
// fetch cache stores tags outside of cache entry
tags?: string[];
revalidate: number;
};

/**
* Open Next cache based on cloudflare KV and Assets.
*
Expand All @@ -20,7 +37,7 @@ class Cache implements IncrementalCache {
async get<IsFetch extends boolean = false>(
key: string,
isFetch?: IsFetch
): Promise<WithLastModified<CacheValue<IsFetch>>> {
): Promise<WithLastModified<CacheValue<IsFetch>> | null> {
const cfEnv = getCloudflareContext().env;
const kv = cfEnv.NEXT_CACHE_WORKERS_KV;
const assets = cfEnv.ASSETS;
Expand All @@ -43,7 +60,7 @@ class Cache implements IncrementalCache {
const kvKey = this.getKVKey(key, isFetch);
entry = await kv.get(kvKey, "json");
if (entry?.status === STATUS_DELETED) {
return {};
return null;
}
}

Expand All @@ -62,6 +79,17 @@ class Cache implements IncrementalCache {
};
}
}

const entryValue = entry?.value as CachedFetchValue | undefined;
dario-piotrowicz marked this conversation as resolved.
Show resolved Hide resolved
if (entryValue?.kind === "FETCH") {
dario-piotrowicz marked this conversation as resolved.
Show resolved Hide resolved
const expires = entryValue.data.headers?.expires;
const expiresTime = new Date(expires as string).getTime();
if (!isNaN(expiresTime) && expiresTime <= Date.now()) {
this.debug(`found expired entry (expire time: ${expires})`);
return null;
}
}

this.debug(entry ? `-> hit` : `-> miss`);
return { value: entry?.value, lastModified: entry?.lastModified };
} catch {
Expand Down
Loading