You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: pages/cloudflare/howtos/skew.mdx
+14-6Lines changed: 14 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -14,21 +14,29 @@ The Cloudflare adapter has _experimental support_ for skew protection based on [
14
14
15
15
**OpenNext config**
16
16
17
-
Set `cloudflare.skewProtectionEnabled` to `true` to enable skew protection.
17
+
Set `cloudflare.skewProtection.enabled` to `true` to enable skew protection:
18
18
19
19
```ts
20
20
// open-next.config.ts
21
21
exportdefault {
22
22
// ...
23
23
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
+
},
25
33
},
26
34
} satisfiesOpenNextConfig;
27
35
```
28
36
29
37
**Wrangler configuration**
30
38
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:
32
40
33
41
```jsonc
34
42
// wrangler.jsonc
@@ -46,18 +54,18 @@ The Worker needs to serve the correct version of the app assets. For that it nee
46
54
47
55
**Environment variables**
48
56
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:
50
58
51
59
-`CF_WORKER_NAME` should be set to the name of the worker, i.e. `my-app` given the config above
52
60
-`CF_PREVIEW_DOMAIN` is the the subdomain of `workers.dev` where the previews are deployed, i.e. `<version-name>.<domain>.workers.dev`
53
61
-`CF_WORKERS_SCRIPTS_API_TOKEN` is an API token with the `Workers Scripts:Read` permission
54
62
-`CF_ACCOUNT_ID` is the Cloudflare account id where the app is deployed.
55
63
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.
57
65
58
66
**Next config**
59
67
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.
61
69
62
70
The cloudflare adapter exports a `getDeploymentId()` function that can be used to generate a unique deployment id.
0 commit comments