Skip to content

Commit d342844

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

File tree

13 files changed

+211
-110
lines changed

13 files changed

+211
-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: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 initialized = false;
21+
protected kv: KVNamespace | undefined;
22+
protected assets: Fetcher | undefined;
23+
24+
async get<IsFetch extends boolean = false>(
25+
key: string,
26+
isFetch?: IsFetch
27+
): Promise<WithLastModified<CacheValue<IsFetch>>> {
28+
if (!this.initialized) {
29+
await this.init();
30+
}
31+
32+
if (!(this.kv || this.assets)) {
33+
throw new IgnorableError(`No KVNamespace nor Fetcher`);
34+
}
35+
36+
this.debug(`Get ${key}`);
37+
38+
try {
39+
this.debug(`- From KV`);
40+
const kvKey = this.getKVKey(key, isFetch ? "fetch" : "cache");
41+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
42+
let value: any = await this.kv?.get(kvKey, "json");
43+
if (!value && this.assets) {
44+
const url = this.getAssetUrl(key);
45+
const response = await this.assets.fetch(url);
46+
this.debug(`- From Assets`);
47+
if (response.ok) {
48+
value = await response.json();
49+
}
50+
}
51+
if (value) {
52+
this.debug(`-> hit`);
53+
return { value };
54+
}
55+
} catch {
56+
throw new RecoverableError(`Failed to get cache [${key}]`);
57+
}
58+
59+
this.debug(`-> miss`);
60+
throw new RecoverableError(`Not found [${key}]`);
61+
}
62+
63+
async set<IsFetch extends boolean = false>(
64+
key: string,
65+
value: CacheValue<IsFetch>,
66+
isFetch?: IsFetch
67+
): Promise<void> {
68+
if (!this.initialized) {
69+
await this.init();
70+
}
71+
if (!this.kv) {
72+
throw new IgnorableError(`No KVNamespace`);
73+
}
74+
this.debug(`Set ${key}`);
75+
try {
76+
const kvKey = this.getKVKey(key, isFetch ? "fetch" : "cache");
77+
// TODO: add TTL to avoid cache growing too big ?
78+
await this.kv.put(kvKey, JSON.stringify(value));
79+
} catch {
80+
throw new RecoverableError(`Failed to set cache [${key}]`);
81+
}
82+
}
83+
84+
async delete(key: string): Promise<void> {
85+
if (!this.initialized) {
86+
await this.init();
87+
}
88+
if (!this.kv) {
89+
throw new IgnorableError(`No KVNamespace`);
90+
}
91+
this.debug(`Delete ${key}`);
92+
try {
93+
const kvKey = this.getKVKey(key, "cache");
94+
await this.kv.delete(kvKey);
95+
} catch (e) {
96+
throw new RecoverableError(`Failed to delete cache [${key}]`);
97+
}
98+
}
99+
100+
protected getKVKey(key: string, extension: Extension): string {
101+
return `${this.getBuildId()}/${key}.${extension}`;
102+
}
103+
104+
protected getAssetUrl(key: string): string {
105+
return `http://assets.local/${CACHE_ASSET_DIR}/${this.getBuildId()}/${key}.cache`.replace(/\/\//g, "/");
106+
}
107+
108+
protected debug(...args: unknown[]) {
109+
if (process.env.NEXT_PRIVATE_DEBUG_CACHE) {
110+
console.log(`[Cache ${this.name}] `, ...args);
111+
}
112+
}
113+
114+
protected getBuildId() {
115+
return process.env.NEXT_BUILD_ID ?? "no-build-id";
116+
}
117+
118+
protected async init() {
119+
const env = (await getCloudflareContext()).env;
120+
this.kv = env.NEXT_CACHE_WORKERS_KV;
121+
this.assets = env.ASSETS;
122+
this.initialized = true;
123+
}
124+
}
125+
126+
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)