Skip to content

Commit 6d020fe

Browse files
vicbconico974
andauthored
feat: add support for skew protection (#746)
Co-authored-by: conico974 <[email protected]>
1 parent 40f7e72 commit 6d020fe

27 files changed

+712
-85
lines changed

.changeset/curly-lions-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": minor
3+
---
4+
5+
feat: add support for experimental skew protection

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ pnpm-lock.yaml
44
.vscode/setting.json
55
test-fixtures
66
test-snapshots
7-
playwright-report
7+
playwright-report

examples/playground14/wrangler.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "node_modules/wrangler/config-schema.json",
33
"main": "worker.ts",
4-
"name": "api",
4+
"name": "playground14",
55
"compatibility_date": "2024-12-30",
66
"compatibility_flags": ["nodejs_compat", "global_fetch_strictly_public"],
77
"assets": {

examples/playground15/app/api/env/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
export const dynamic = "force-dynamic";
55

66
export async function GET() {
7-
return new Response(JSON.stringify(process.env));
7+
return new Response(JSON.stringify(process.env, null, 2), {
8+
headers: { "content-type": "application/json" },
9+
});
810
}

examples/playground15/next.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
1+
import { initOpenNextCloudflareForDev, getDeploymentId } from "@opennextjs/cloudflare";
22

33
initOpenNextCloudflareForDev();
44

@@ -10,6 +10,7 @@ const nextConfig = {
1010
// Generate source map to validate the fix for opennextjs/opennextjs-cloudflare#341
1111
serverSourceMaps: true,
1212
},
13+
deploymentId: getDeploymentId(),
1314
};
1415

1516
export default nextConfig;
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
2-
import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/kv-incremental-cache";
1+
import { defineCloudflareConfig, type OpenNextConfig } from "@opennextjs/cloudflare";
2+
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
33

4-
export default defineCloudflareConfig({
5-
incrementalCache: kvIncrementalCache,
6-
enableCacheInterception: true,
7-
});
4+
export default {
5+
...defineCloudflareConfig({
6+
incrementalCache: r2IncrementalCache,
7+
}),
8+
cloudflare: {
9+
skewProtection: {
10+
enabled: false,
11+
},
12+
},
13+
} satisfies OpenNextConfig;

examples/playground15/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"cf-typegen": "wrangler types --env-interface CloudflareEnv"
1717
},
1818
"dependencies": {
19-
"next": "^15.1.7",
19+
"next": "^15.3.4",
2020
"react": "^19.0.0",
2121
"react-dom": "^19.0.0"
2222
},

examples/playground15/wrangler.jsonc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"$schema": "node_modules/wrangler/config-schema.json",
33
"main": ".open-next/worker.js",
4-
"name": "api",
4+
"name": "playground15",
55
"compatibility_date": "2024-12-30",
66
"compatibility_flags": ["nodejs_compat", "global_fetch_strictly_public"],
77
"assets": {
88
"directory": ".open-next/assets",
99
"binding": "ASSETS",
1010
"run_worker_first": true
1111
},
12-
"kv_namespaces": [
12+
"r2_buckets": [
1313
{
14-
"binding": "NEXT_INC_CACHE_KV",
15-
"id": "<BINDING_ID>"
14+
"binding": "NEXT_INC_CACHE_R2_BUCKET",
15+
"bucket_name": "pg15"
1616
}
1717
],
1818
"vars": {

packages/cloudflare/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"@types/mock-fs": "catalog:",
6666
"@types/node": "catalog:",
6767
"@types/picomatch": "^4.0.0",
68+
"cloudflare": "^4.4.1",
6869
"diff": "^8.0.2",
6970
"esbuild": "catalog:",
7071
"eslint": "catalog:",

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ declare global {
6565
CACHE_PURGE_ZONE_ID?: string;
6666
// The API token to use for the cache purge. It should have the `Cache Purge` permission
6767
CACHE_PURGE_API_TOKEN?: string;
68+
69+
// The following variables must be provided when skew protection is enabled
70+
// The name of the worker (as defined in the wrangler configuration)
71+
// When a specific wrangler environment is used, it should be appended at the end:
72+
// - Use `worker-name` when no wrangler environment is used
73+
// - Use `worker-name-<environment>` when a wrangler environment is used via `wrangler --env=<environment>`
74+
CF_WORKER_NAME?: string;
75+
// The subdomain where the previews are deployed, i.e. `<version-name>.<domain>.workers.dev`
76+
CF_PREVIEW_DOMAIN?: string;
77+
// Should have the `Workers Scripts:Read` permission
78+
CF_WORKERS_SCRIPTS_API_TOKEN?: string;
79+
// Cloudflare account id
80+
CF_ACCOUNT_ID?: string;
6881
}
6982
}
7083

0 commit comments

Comments
 (0)