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 6 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@727`
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 that the kvCache doesn't serve stale cache values from assets when there is no KV binding
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@727",
"enquirer": "^2.4.1",
"glob": "catalog:",
"ts-morph": "catalog:",
Expand Down
26 changes: 23 additions & 3 deletions packages/cloudflare/src/api/kvCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs/aws/types/overrides";
import type {
CachedFetchValue,
CacheValue,
IncrementalCache,
WithLastModified,
} from "@opennextjs/aws/types/overrides";
import { IgnorableError, RecoverableError } from "@opennextjs/aws/utils/error.js";

import { getCloudflareContext } from "./cloudflare-context.js";
Expand All @@ -20,7 +25,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 +48,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 @@ -61,7 +66,22 @@ class Cache implements IncrementalCache {
lastModified: (globalThis as { __BUILD_TIMESTAMP_MS__?: number }).__BUILD_TIMESTAMP_MS__,
};
}
if (!kv) {
// The cache can not be updated when there is no KV
// As we don't want to keep serving stale data for ever,
// we pretend the entry is not in cache
const entryValue = entry?.value as CachedFetchValue;
if (entryValue?.kind === "FETCH") {
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