Skip to content

Commit 8719e57

Browse files
committed
fixup! sync skew protection, add assets
1 parent 262cd9c commit 8719e57

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

pages/cloudflare/howtos/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"custom-worker": "Custom Worker",
88
"keep_names": "__name issues",
99
"workerd": "workerd specific packages",
10-
"skew": "Skew Protection"
10+
"skew": "Skew Protection",
11+
"assets": "Static assets"
1112
}

pages/cloudflare/howtos/assets.mdx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Static assets (public folder)
2+
3+
The [static assets of Next located in the public folder](https://nextjs.org/docs/app/api-reference/file-conventions/public-folder) are served using [Workers Static Assets](https://developers.cloudflare.com/workers/static-assets/).
4+
5+
Workers Static Assets can intercept requests to the application ([`run_worker_first=false`](https://developers.cloudflare.com/workers/static-assets/) which is the default). This is the most cost efficient option as asset requests will not be billed in that case.
6+
7+
Another option is to run the Worker first (`run_worker_first=true`) to offer more flexibility at the expense of a higher cost.
8+
9+
Note that `run_worker_first` could also be set to a list of patterns - this is a great option if you need more flexibility for only a subset of the statids assets.
10+
11+
### run_worker_first=false
12+
13+
When `run_worker_first` is set to `false`, requests are intercepted before reaching the worker and are not billed:
14+
15+
```jsonc
16+
// wrangler.jsonc
17+
{
18+
"name": "my-app",
19+
// ...
20+
"assets": {
21+
"directory": ".open-next/assets",
22+
"binding": "ASSETS",
23+
// Optional as false is the default value
24+
"run_worker_first": false,
25+
},
26+
// ...
27+
}
28+
```
29+
30+
This is the most cost efficient option to use when you do not need to serve assets behind the middleware or Next rewrites and headers from [the Next config](https://nextjs.org/docs/app/api-reference/config/next-config-js).
31+
32+
When `run_worker_first=false` you can still configure [headers](https://developers.cloudflare.com/workers/static-assets/headers/) and [redirects](https://developers.cloudflare.com/workers/static-assets/redirects/) via Worker Static Assets.
33+
34+
### run_worker_first=true
35+
36+
When `run_worker_first` is set to `true`, all the requests will reach the Worker and be billed:
37+
38+
```jsonc
39+
// wrangler.jsonc
40+
{
41+
"name": "my-app",
42+
// ...
43+
"assets": {
44+
"directory": ".open-next/assets",
45+
"binding": "ASSETS",
46+
"run_worker_first": true,
47+
},
48+
// ...
49+
}
50+
```
51+
52+
The Open Next asset resolver will be used to retrieve the assets from the Worker.
53+
54+
When `run_worker_first=true`, assets are served behind the middleware and Next rewrites and headers from [the Next config](https://nextjs.org/docs/app/api-reference/config/next-config-js). The [headers](https://developers.cloudflare.com/workers/static-assets/headers/) and [redirects](https://developers.cloudflare.com/workers/static-assets/redirects/) configured for the Worker Static Assets do not apply in this case.
55+
56+
`run_worker_first=true` should be used if you plan to use skew protection.

pages/cloudflare/howtos/skew.mdx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,29 @@ The Cloudflare adapter has _experimental support_ for skew protection based on [
1414

1515
**OpenNext config**
1616

17-
Set `cloudflare.skewProtectionEnabled` to `true` to enable skew protection.
17+
Set `cloudflare.skewProtection.enabled` to `true` to enable skew protection:
1818

1919
```ts
2020
// open-next.config.ts
2121
export default {
2222
// ...
2323
cloudflare: {
24-
skewProtectionEnabled: true,
24+
skewProtection: {
25+
enabled: true,
26+
// Maximum number of previous versions to use.
27+
// Optional, default to 20.
28+
maxNumberOfVersions: 20,
29+
// Age of the oldest version to use (from the last deplyment date)
30+
// Optional, default to 7 days.
31+
maxVersionAgeDays: 7,
32+
},
2533
},
2634
} satisfies OpenNextConfig;
2735
```
2836

2937
**Wrangler configuration**
3038

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:
39+
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) to `tue` in your wrangler configuration to enable this behavior:
3240

3341
```jsonc
3442
// wrangler.jsonc
@@ -46,18 +54,18 @@ The Worker needs to serve the correct version of the app assets. For that it nee
4654

4755
**Environment variables**
4856

49-
The following environment variables should be set when the skew protection is use:
57+
The following environment variables should be set when the skew protection is used:
5058

5159
- `CF_WORKER_NAME` should be set to the name of the worker, i.e. `my-app` given the config above
5260
- `CF_PREVIEW_DOMAIN` is the the subdomain of `workers.dev` where the previews are deployed, i.e. `<version-name>.<domain>.workers.dev`
5361
- `CF_WORKERS_SCRIPTS_API_TOKEN` is an API token with the `Workers Scripts:Read` permission
5462
- `CF_ACCOUNT_ID` is the Cloudflare account id where the app is deployed.
5563

56-
Those variables are used to retrieve the past deployments of your application before the app is deployed.
64+
Those variables are used to retrieve the past deployments of your application.
5765

5866
**Next config**
5967

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.
68+
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 by a previous deployment.
6169

6270
The cloudflare adapter exports a `getDeploymentId()` function that can be used to generate a unique deployment id.
6371

0 commit comments

Comments
 (0)