Skip to content

Commit d13412b

Browse files
committed
output an sql file instead
1 parent 5623685 commit d13412b

File tree

8 files changed

+47
-85
lines changed

8 files changed

+47
-85
lines changed

examples/e2e/app-router/open-next.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
2-
import memoryQueue from "@opennextjs/cloudflare/memory-queue";
3-
import incrementalCache from "@opennextjs/cloudflare/kv-cache";
42
import tagCache from "@opennextjs/cloudflare/d1-tag-cache";
3+
import incrementalCache from "@opennextjs/cloudflare/kv-cache";
4+
import memoryQueue from "@opennextjs/cloudflare/memory-queue";
55

66
const config: OpenNextConfig = {
77
default: {

examples/e2e/app-router/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
"start": "next start --port 3001",
1010
"lint": "next lint",
1111
"clean": "rm -rf .turbo node_modules .next .open-next",
12-
"setup:d1": "wrangler d1 execute NEXT_CACHE_D1 --command \"CREATE TABLE IF NOT EXISTS tags (tag TEXT NOT NULL, path TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag, path) ON CONFLICT REPLACE)\"",
13-
"build:worker": "pnpm opennextjs-cloudflare",
12+
"d1:clean": "wrangler d1 execute NEXT_CACHE_D1 --command \"DROP TABLE IF EXISTS tags\"",
13+
"d1:setup": "wrangler d1 execute NEXT_CACHE_D1 --file .open-next/cache-assets-manifest.sql",
14+
"build:worker": "pnpm opennextjs-cloudflare && pnpm d1:clean && pnpm d1:setup",
1415
"preview": "pnpm build:worker && pnpm wrangler dev",
15-
"e2e": "pnpm setup:d1 && playwright test -c e2e/playwright.config.ts"
16+
"e2e": "playwright test -c e2e/playwright.config.ts"
1617
},
1718
"dependencies": {
1819
"@opennextjs/cloudflare": "workspace:*",

packages/cloudflare/env.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { CacheAssetsManifest } from "./src/cli/build/open-next/create-cache-assets-manifest.js";
2-
31
declare global {
42
namespace NodeJS {
53
interface ProcessEnv {
@@ -9,7 +7,6 @@ declare global {
97
NEXT_PRIVATE_DEBUG_CACHE?: string;
108
OPEN_NEXT_ORIGIN: string;
119
NODE_ENV?: string;
12-
__OPENNEXT_CACHE_TAGS_MANIFEST: CacheAssetsManifest;
1310
}
1411
}
1512
}

packages/cloudflare/src/api/d1-tag-cache.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ import type { TagCache } from "@opennextjs/aws/types/overrides.js";
44

55
import { getCloudflareContext } from "./cloudflare-context.js";
66

7-
// inlined during build
8-
const manifest = process.env.__OPENNEXT_CACHE_TAGS_MANIFEST;
9-
107
/**
118
* An instance of the Tag Cache that uses a D1 binding (`NEXT_CACHE_D1`) as it's underlying data store.
129
*
1310
* The table used defaults to `tags`, but can be configured with the `NEXT_CACHE_D1_TABLE`
1411
* environment variable.
1512
*
1613
* There should be three columns created in the table; `tag`, `path`, and `revalidatedAt`.
17-
*
18-
* A combination of the D1 entries and the build-time tags manifest is used.
1914
*/
2015
class D1TagCache implements TagCache {
2116
public readonly name = "d1-tag-cache";
@@ -34,10 +29,7 @@ class D1TagCache implements TagCache {
3429

3530
if (!success) throw new Error(`D1 select failed for ${path}`);
3631

37-
const tags = this.mergeTagArrays(
38-
manifest.paths[path],
39-
results?.map((item) => item.tag)
40-
);
32+
const tags = results?.map((item) => this.removeBuildId(item.tag));
4133

4234
debug("tags for path", path, tags);
4335
return tags;
@@ -61,10 +53,7 @@ class D1TagCache implements TagCache {
6153

6254
if (!success) throw new Error(`D1 select failed for ${tag}`);
6355

64-
const paths = this.mergeTagArrays(
65-
manifest.tags[tag],
66-
results?.map((item) => item.path)
67-
);
56+
const paths = results?.map((item) => this.removeBuildId(item.path));
6857

6958
debug("paths for tag", tag, paths);
7059
return paths;
@@ -145,16 +134,6 @@ class D1TagCache implements TagCache {
145134
protected getBuildId() {
146135
return process.env.NEXT_BUILD_ID ?? "no-build-id";
147136
}
148-
149-
protected mergeTagArrays(...arrays: (string[] | undefined)[]) {
150-
const set = new Set<string>();
151-
152-
for (const arr of arrays) {
153-
arr?.forEach((v) => set.add(v));
154-
}
155-
156-
return [...set.values()].map((v) => this.removeBuildId(v));
157-
}
158137
}
159138

160139
export default new D1TagCache();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import logger from "@opennextjs/aws/logger.js";
1212

1313
import type { ProjectOptions } from "../project-options.js";
1414
import { bundleServer } from "./bundle-server.js";
15+
import { compileCacheAssetsManifestSqlFile } from "./open-next/compile-cache-assets-manifest.js";
1516
import { compileEnvFiles } from "./open-next/compile-env-files.js";
1617
import { copyCacheAssets } from "./open-next/copyCacheAssets.js";
1718
import { createServerBundle } from "./open-next/createServerBundle.js";
@@ -88,6 +89,7 @@ export async function build(projectOpts: ProjectOptions): Promise<void> {
8889
if (config.dangerous?.disableIncrementalCache !== true) {
8990
createCacheAssets(options);
9091
copyCacheAssets(options);
92+
compileCacheAssetsManifestSqlFile(options);
9193
}
9294

9395
await createServerBundle(options);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { fixRequire } from "./patches/plugins/require.js";
2020
import { shimRequireHook } from "./patches/plugins/require-hook.js";
2121
import { inlineRequirePage } from "./patches/plugins/require-page.js";
2222
import { setWranglerExternal } from "./patches/plugins/wrangler-external.js";
23-
import { extractCacheAssetsManifest } from "./utils/extract-cache-assets-manifest.js";
2423
import { normalizePath, patchCodeWithValidations } from "./utils/index.js";
2524

2625
/** The dist directory of the Cloudflare adapter package */
@@ -129,7 +128,6 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
129128
"process.env.NEXT_RUNTIME": '"nodejs"',
130129
"process.env.NODE_ENV": '"production"',
131130
"process.env.NEXT_MINIMAL": "true",
132-
"process.env.__OPENNEXT_CACHE_TAGS_MANIFEST": `${JSON.stringify(extractCacheAssetsManifest(buildOpts))}`,
133131
},
134132
platform: "node",
135133
banner: {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
2+
import path from "node:path";
3+
4+
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
5+
6+
type RawManifest = {
7+
tag: { S: string };
8+
path: { S: string };
9+
revalidatedAt: { N: string };
10+
}[];
11+
12+
/**
13+
* Generates SQL statements that can be used to initialise the cache assets manifest in an SQL data store.
14+
*/
15+
export function compileCacheAssetsManifestSqlFile(options: BuildOptions) {
16+
// TODO: Expose the function for getting this data as an adapter-agnostic utility in AWS.
17+
const rawManifestPath = path.join(options.outputDir, "dynamodb-provider", "dynamodb-cache.json");
18+
const outputPath = path.join(options.outputDir, "cache-assets-manifest.sql");
19+
20+
const table = process.env.NEXT_CACHE_D1 || "tags";
21+
22+
const stmts = [
23+
`CREATE TABLE IF NOT EXISTS ${table} (tag TEXT NOT NULL, path TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag, path) ON CONFLICT REPLACE);`,
24+
];
25+
26+
if (existsSync(rawManifestPath)) {
27+
const rawManifest: RawManifest = JSON.parse(readFileSync(rawManifestPath, "utf-8"));
28+
29+
rawManifest.forEach(({ tag: { S: tag }, path: { S: path }, revalidatedAt: { N: revalidatedAt } }) => {
30+
stmts.push(
31+
`INSERT INTO ${table} (tag, path, revalidatedAt) VALUES ('${tag}', '${path}', ${revalidatedAt});`
32+
);
33+
});
34+
}
35+
36+
writeFileSync(outputPath, stmts.join("\n"));
37+
}

packages/cloudflare/src/cli/build/utils/extract-cache-assets-manifest.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)