Skip to content

Commit bd99bac

Browse files
committed
feat: kv cache
1 parent c51b8b3 commit bd99bac

File tree

13 files changed

+214
-110
lines changed

13 files changed

+214
-110
lines changed

examples/vercel-blog-starter/open-next.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next";
2+
import cache from "@opennextjs/cloudflare/kvCache";
23

34
const config: OpenNextConfig = {
45
default: {
56
override: {
67
wrapper: "cloudflare-node",
78
converter: "edge",
8-
// Unused implementation
9-
incrementalCache: "dummy",
9+
incrementalCache: async () => cache,
10+
// Unused implementations
1011
tagCache: "dummy",
1112
queue: "dummy",
1213
},

packages/cloudflare/env.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
declare global {
22
namespace NodeJS {
33
interface ProcessEnv {
4-
ASSETS: Fetcher;
54
__NEXT_PRIVATE_STANDALONE_CONFIG?: string;
65
SKIP_NEXT_APP_BUILD?: string;
76
NEXT_PRIVATE_DEBUG_CACHE?: string;
8-
__OPENNEXT_KV_BINDING_NAME: string;
97
OPEN_NEXT_ORIGIN: string;
108
NODE_ENV?: string;
11-
__OPENNEXT_PROCESSED_ENV?: string;
9+
// Whether process.env has been populated (on first request).
10+
__PROCESS_ENV_POPULATED?: string;
1211
}
1312
}
1413
}

packages/cloudflare/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
".": {
2323
"import": "./dist/api/index.js",
2424
"types": "./dist/api/index.d.ts"
25+
},
26+
"./*": {
27+
"import": "./dist/api/*.js",
28+
"types": "./dist/api/*.d.ts"
2529
}
2630
},
2731
"files": [

packages/cloudflare/src/api/get-cloudflare-context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import "server-only";
2-
31
declare global {
4-
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
5-
interface CloudflareEnv {}
2+
interface CloudflareEnv {
3+
NEXT_CACHE_WORKERS_KV?: KVNamespace;
4+
ASSETS?: Fetcher;
5+
}
66
}
77

88
export type CloudflareContext<
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
import type { KVNamespace } from "@cloudflare/workers-types";
3+
import type { Extension } from "@opennextjs/aws/types/cache";
4+
import type { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs/aws/types/overrides";
5+
import { IgnorableError, RecoverableError } from "@opennextjs/aws/utils/error.js";
6+
7+
import { getCloudflareContext } from "./get-cloudflare-context.js";
8+
9+
export const CACHE_ASSET_DIR = "cnd-cgi/_next_cache";
10+
11+
/**
12+
* Open Next cache based on cloudflare KV and Assets.
13+
*
14+
* Note: The class is instantiated outside of the request context.
15+
* The cloudflare context and process.env are not initialzed yet
16+
* when the constructor is called.
17+
*/
18+
class Cache implements IncrementalCache {
19+
readonly name = "cloudflare-kv";
20+
protected optKv: { value?: KVNamespace | undefined } = {};
21+
protected optAssets: { value?: Fetcher | undefined } = {};
22+
23+
async get<IsFetch extends boolean = false>(
24+
key: string,
25+
isFetch?: IsFetch
26+
): Promise<WithLastModified<CacheValue<IsFetch>>> {
27+
const [kv, assets] = await Promise.all([this.getKv(), this.getAssets()]);
28+
29+
if (!(kv || assets)) {
30+
throw new IgnorableError(`No KVNamespace nor Fetcher`);
31+
}
32+
33+
this.debug(`Get ${key}`);
34+
35+
try {
36+
this.debug(`- From KV`);
37+
const kvKey = this.getKVKey(key, isFetch ? "fetch" : "cache");
38+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
39+
let value: any = await kv?.get(kvKey, "json");
40+
if (!value && assets) {
41+
const url = this.getAssetUrl(key);
42+
const response = await assets.fetch(url);
43+
this.debug(`- From Assets`);
44+
if (response.ok) {
45+
value = await response.json();
46+
}
47+
}
48+
if (value) {
49+
this.debug(`-> hit`);
50+
return { value };
51+
}
52+
} catch {
53+
throw new RecoverableError(`Failed to get cache [${key}]`);
54+
}
55+
56+
this.debug(`-> miss`);
57+
throw new RecoverableError(`Not found [${key}]`);
58+
}
59+
60+
async set<IsFetch extends boolean = false>(
61+
key: string,
62+
value: CacheValue<IsFetch>,
63+
isFetch?: IsFetch
64+
): Promise<void> {
65+
const kv = await this.getKv();
66+
if (!kv) {
67+
throw new IgnorableError(`No KVNamespace`);
68+
}
69+
this.debug(`Set ${key}`);
70+
try {
71+
const kvKey = this.getKVKey(key, isFetch ? "fetch" : "cache");
72+
// TODO: add TTL to avoid cache growing too big ?
73+
await kv.put(kvKey, JSON.stringify(value));
74+
} catch {
75+
throw new RecoverableError(`Failed to set cache [${key}]`);
76+
}
77+
}
78+
79+
async delete(key: string): Promise<void> {
80+
const kv = await this.getKv();
81+
if (!kv) {
82+
throw new IgnorableError(`No KVNamespace`);
83+
}
84+
this.debug(`Delete ${key}`);
85+
try {
86+
const kvKey = this.getKVKey(key, "cache");
87+
await kv.delete(kvKey);
88+
} catch (e) {
89+
throw new RecoverableError(`Failed to delete cache [${key}]`);
90+
}
91+
}
92+
93+
protected getKVKey(key: string, extension: Extension): string {
94+
return `${this.getBuildId()}/${key}.${extension}`;
95+
}
96+
97+
protected getAssetUrl(key: string): string {
98+
return `http://assets.local/${CACHE_ASSET_DIR}/${this.getBuildId()}/${key}.cache`.replace(/\/\//g, "/");
99+
}
100+
101+
protected debug(...args: unknown[]) {
102+
if (process.env.NEXT_PRIVATE_DEBUG_CACHE) {
103+
console.log(`[Cache ${this.name}] `, ...args);
104+
}
105+
}
106+
107+
protected getBuildId() {
108+
return process.env.NEXT_BUILD_ID ?? "no-build-id";
109+
}
110+
111+
protected async getKv(): Promise<KVNamespace | undefined> {
112+
if (!("value" in this.optKv)) {
113+
const env = (await getCloudflareContext()).env;
114+
this.optKv.value = env.NEXT_CACHE_WORKERS_KV;
115+
}
116+
return this.optKv.value;
117+
}
118+
119+
protected async getAssets(): Promise<Fetcher | undefined> {
120+
if (!("value" in this.optAssets)) {
121+
const env = (await getCloudflareContext()).env;
122+
this.optAssets.value = env.ASSETS;
123+
}
124+
return this.optAssets.value;
125+
}
126+
}
127+
128+
export default new Cache();

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { build, Plugin } from "esbuild";
88

99
import { Config } from "../config.js";
1010
import * as patches from "./patches/index.js";
11-
import { copyPrerenderedRoutes } from "./utils/index.js";
1211

1312
/** The dist directory of the Cloudflare adapter package */
1413
const packageDistDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "../..");
@@ -17,9 +16,6 @@ const packageDistDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "
1716
* Bundle the Open Next server.
1817
*/
1918
export async function bundleServer(config: Config, openNextOptions: BuildOptions): Promise<void> {
20-
// Copy over prerendered assets (e.g. SSG routes)
21-
copyPrerenderedRoutes(config);
22-
2319
patches.copyPackageCliFiles(packageDistDir, config, openNextOptions);
2420

2521
const nextConfigStr =

packages/cloudflare/src/cli/build/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { dirname, join } from "node:path";
55
import { buildNextjsApp, setStandaloneBuildMode } from "@opennextjs/aws/build/buildNextApp.js";
66
import { compileCache } from "@opennextjs/aws/build/compileCache.js";
77
import { compileOpenNextConfig } from "@opennextjs/aws/build/compileConfig.js";
8-
import { createStaticAssets } from "@opennextjs/aws/build/createAssets.js";
8+
import { createCacheAssets, createStaticAssets } from "@opennextjs/aws/build/createAssets.js";
99
import { createMiddleware } from "@opennextjs/aws/build/createMiddleware.js";
1010
import * as buildHelper from "@opennextjs/aws/build/helper.js";
1111
import { printHeader, showWarningOnWindows } from "@opennextjs/aws/build/utils.js";
@@ -16,6 +16,7 @@ import type { ProjectOptions } from "../config.js";
1616
import { containsDotNextDir, getConfig } from "../config.js";
1717
import { bundleServer } from "./bundle-server.js";
1818
import { compileEnvFiles } from "./open-next/compile-env-files.js";
19+
import { copyCacheAssets } from "./open-next/copyCacheAssets.js";
1920
import { createServerBundle } from "./open-next/createServerBundle.js";
2021

2122
/**
@@ -80,6 +81,11 @@ export async function build(projectOpts: ProjectOptions): Promise<void> {
8081

8182
createStaticAssets(options);
8283

84+
if (config.dangerous?.disableIncrementalCache !== true) {
85+
createCacheAssets(options);
86+
copyCacheAssets(options);
87+
}
88+
8389
await createServerBundle(options);
8490

8591
// TODO: drop this copy.
@@ -103,10 +109,11 @@ function ensureCloudflareConfig(config: OpenNextConfig) {
103109
const requirements = {
104110
dftUseCloudflareWrapper: config.default?.override?.wrapper === "cloudflare-node",
105111
dftUseEdgeConverter: config.default?.override?.converter === "edge",
106-
dftUseDummyCache:
107-
config.default?.override?.incrementalCache === "dummy" &&
108-
config.default?.override?.tagCache === "dummy" &&
109-
config.default?.override?.queue === "dummy",
112+
dftMaybeUseCache:
113+
config.default?.override?.incrementalCache === "dummy" ||
114+
typeof config.default?.override?.incrementalCache === "function",
115+
dftUseDummyTagCache:
116+
config.default?.override?.tagCache === "dummy" && config.default?.override?.queue === "dummy",
110117
disableCacheInterception: config.dangerous?.enableCacheInterception !== true,
111118
mwIsMiddlewareExternal: config.middleware?.external == true,
112119
mwUseCloudflareWrapper: config.middleware?.override?.wrapper === "cloudflare-edge",
@@ -121,7 +128,7 @@ function ensureCloudflareConfig(config: OpenNextConfig) {
121128
override: {
122129
wrapper: "cloudflare-node",
123130
converter: "edge",
124-
incrementalCache: "dummy",
131+
incrementalCache: "dummy" | function,
125132
tagCache: "dummy",
126133
queue: "dummy",
127134
},
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { cpSync, mkdirSync } from "node:fs";
2+
import { join } from "node:path";
3+
4+
import * as buildHelper from "@opennextjs/aws/build/helper.js";
5+
6+
import { CACHE_ASSET_DIR } from "../../../api/kvCache.js";
7+
8+
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);
13+
mkdirSync(dstPath, { recursive: true });
14+
cpSync(srcPath, dstPath, { recursive: true });
15+
}

packages/cloudflare/src/cli/build/utils/copy-prerendered-routes.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from "./copy-prerendered-routes.js";
21
export * from "./extract-project-env-vars.js";
32
export * from "./normalize-path.js";
43
export * from "./ts-parse-file.js";

0 commit comments

Comments
 (0)