Skip to content

Commit 0844a9f

Browse files
committed
docs(cloudflare): skew protection
1 parent 39eb876 commit 0844a9f

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

pages/cloudflare/howtos/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"image": "Image Optimization",
77
"custom-worker": "Custom Worker",
88
"keep_names": "__name issues",
9-
"workerd": "workerd specific packages"
9+
"workerd": "workerd specific packages",
10+
"skew": "Skew Protection"
1011
}

pages/cloudflare/howtos/skew.mdx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Callout } from "nextra/components";
2+
3+
## Skew protection
4+
5+
The Cloudflare adapter has _experimental support_ for skew protection based on [the preview URLs](https://developers.cloudflare.com/workers/configuration/previews/).
6+
7+
<Callout type="info">
8+
Preview URLs are disabled for [Workers that implement a Durable
9+
Object](https://developers.cloudflare.com/workers/configuration/previews/#limitations). If your app uses
10+
Durable Objects, they will need to be implemented in a separate Worker.
11+
</Callout>
12+
13+
### How to enable the skew protection
14+
15+
**OpenNext config**
16+
17+
Set `cloudflare.skewProtectionEnabled` to `true` to enable skew protection.
18+
19+
```ts
20+
// open-next.config.ts
21+
export default {
22+
// ...
23+
cloudflare: {
24+
skewProtectionEnabled: true,
25+
},
26+
} satisfies OpenNextConfig;
27+
```
28+
29+
**Wrangler configuration**
30+
31+
The Worker needs to serve the correct version of the app assets. For that it need be be executed before incoming requests are matched against the assets. Set [`run_worker_first`](https://developers.cloudflare.com/workers/static-assets/binding/#run_worker_first) in your wrangler configuration to enable this behavior:
32+
33+
```jsonc
34+
// wrangler.jsonc
35+
{
36+
"name": "my-app",
37+
// ...
38+
"assets": {
39+
"directory": ".open-next/assets",
40+
"binding": "ASSETS",
41+
"run_worker_first": true,
42+
},
43+
// ...
44+
}
45+
```
46+
47+
**Environment variables**
48+
49+
The following environment variables should be set when the skew protection is use:
50+
51+
- `CF_WORKER_NAME` should be set to the name of the worker, i.e. `my-app` given the config above
52+
- `CF_PREVIEW_DOMAIN` is the the subdomain of `workers.dev` where the previews are deployed, i.e. `<version-name>.<domain>.workers.dev`
53+
- `CF_WORKERS_SCRIPTS_API_TOKEN` is an API token with the `Workers Scripts:Read` permission
54+
- `CF_ACCOUNT_ID` is the Cloudflare account id where the app is deployed.
55+
56+
Those variables are used to retrieve the past deployments of your application before the app is deployed.
57+
58+
**Next config**
59+
60+
You must set a different `deploymentId` in your next config each time your app is deployed. You will get an error if the `deployementId` has already been used when deploying.
61+
62+
The cloudflare adapter exports a `getDeploymentId()` function that can be used to generate a unique deployment id.
63+
64+
```ts
65+
// next.config.ts
66+
import { getDeploymentId } from "@opennextjs/cloudflare";
67+
68+
const nextConfig = {
69+
// ...
70+
deploymentId: getDeploymentId(),
71+
};
72+
```
73+
74+
### What you should know
75+
76+
- Because the Worker is configured to run in front of the assets Worker (`run_worker_first`), requesting an asset will count as a request to your Worker,
77+
- It is not currently possible to delete a deployment
78+
- Requests to an older deployement will be a few milli-seconds slower than requests to the latest version of the app

0 commit comments

Comments
 (0)