Skip to content

Commit 83942cd

Browse files
committed
fixup! fetch cache
1 parent 0bd6a2a commit 83942cd

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

packages/cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"@types/mock-fs": "catalog:"
7070
},
7171
"dependencies": {
72-
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@678",
72+
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@684",
7373
"ts-morph": "catalog:",
7474
"@dotenvx/dotenvx": "catalog:"
7575
},

packages/cloudflare/src/api/kvCache.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
import type { KVNamespace } from "@cloudflare/workers-types";
3-
import type { Extension } from "@opennextjs/aws/types/cache";
43
import type { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs/aws/types/overrides";
54
import { IgnorableError, RecoverableError } from "@opennextjs/aws/utils/error.js";
65

@@ -39,7 +38,7 @@ class Cache implements IncrementalCache {
3938

4039
try {
4140
this.debug(`- From KV`);
42-
const kvKey = this.getKVKey(key, isFetch ? "fetch" : "cache");
41+
const kvKey = this.getKVKey(key, isFetch);
4342

4443
let entry = (await this.kv?.get(kvKey, "json")) as {
4544
value?: CacheValue<IsFetch>;
@@ -50,7 +49,7 @@ class Cache implements IncrementalCache {
5049
return {};
5150
}
5251
if (!entry && this.assets) {
53-
const url = this.getAssetUrl(key);
52+
const url = this.getAssetUrl(key, isFetch);
5453
const response = await this.assets.fetch(url);
5554
this.debug(`- From Assets`);
5655
if (response.ok) {
@@ -84,7 +83,7 @@ class Cache implements IncrementalCache {
8483
}
8584
this.debug(`Set ${key}`);
8685
try {
87-
const kvKey = this.getKVKey(key, isFetch ? "fetch" : "cache");
86+
const kvKey = this.getKVKey(key, isFetch);
8887
// Note: We can not set a TTL as we might fallback to assets,
8988
// still removing old data (old BUILD_ID) could help avoiding
9089
// the cache growing too big.
@@ -109,20 +108,22 @@ class Cache implements IncrementalCache {
109108
}
110109
this.debug(`Delete ${key}`);
111110
try {
112-
const kvKey = this.getKVKey(key, "cache");
111+
const kvKey = this.getKVKey(key, /* isFetch= */ false);
113112
// Do not delete the key as we will then fallback to the assets.
114113
await this.kv.put(kvKey, JSON.stringify({ status: STATUS_DELETED }));
115114
} catch (e) {
116115
throw new RecoverableError(`Failed to delete cache [${key}]`);
117116
}
118117
}
119118

120-
protected getKVKey(key: string, extension: Extension): string {
121-
return `${this.getBuildId()}/${key}.${extension}`;
119+
protected getKVKey(key: string, isFetch?: boolean): string {
120+
return `${this.getBuildId()}/${key}.${isFetch ? "fetch" : "cache"}`;
122121
}
123122

124-
protected getAssetUrl(key: string): string {
125-
return `http://assets.local/${CACHE_ASSET_DIR}/${this.getBuildId()}/${key}.cache`.replace(/\/\//g, "/");
123+
protected getAssetUrl(key: string, isFetch?: boolean): string {
124+
return isFetch
125+
? `http://assets.local/${CACHE_ASSET_DIR}/__fetch/${this.getBuildId()}/${key}`
126+
: `http://assets.local/${CACHE_ASSET_DIR}/${this.getBuildId()}/${key}.cache`;
126127
}
127128

128129
protected debug(...args: unknown[]) {

packages/cloudflare/src/cli/build/open-next/copyCacheAssets.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import * as buildHelper from "@opennextjs/aws/build/helper.js";
66
import { CACHE_ASSET_DIR } from "../../../api/kvCache.js";
77

88
export function copyCacheAssets(options: buildHelper.BuildOptions) {
9-
const { appBuildOutputPath, outputDir } = options;
10-
const buildId = buildHelper.getBuildId(appBuildOutputPath);
11-
const srcPath = join(outputDir, "cache", buildId);
12-
const dstPath = join(outputDir, "assets", CACHE_ASSET_DIR, buildId);
9+
const { outputDir } = options;
10+
const srcPath = join(outputDir, "cache");
11+
const dstPath = join(outputDir, "assets", CACHE_ASSET_DIR);
1312
mkdirSync(dstPath, { recursive: true });
1413
cpSync(srcPath, dstPath, { recursive: true });
1514
}

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)