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
##### 2. Add the KV namespace and Service Binding to your Worker
33
+
##### 2. Add the R2 Bucket and Service Binding to your Worker
33
34
34
-
The binding name used in your app's worker is `NEXT_INC_CACHE_KV`.
35
-
The `WORKER_SELF_REFERENCE` service binding should be a self reference to your worker where `<WORKER_NAME>` is the name in your wrangler configuration file.
35
+
The binding name used in your app's worker is `NEXT_INC_CACHE_R2_BUCKET`. The service binding should be a self reference to your worker where `<WORKER_NAME>` is the name in your wrangler configuration file.
36
+
37
+
The prefix used by the R2 bucket can be configured with the `NEXT_INC_CACHE_R2_PREFIX` environment variable, and defaults to `incremental-cache`.
36
38
37
39
```jsonc
38
40
// wrangler.jsonc
39
41
{
40
42
// ...
41
43
"name":"<WORKER_NAME>",
42
-
"kv_namespaces": [
44
+
"r2_buckets": [
43
45
{
44
-
"binding":"NEXT_INC_CACHE_KV",
45
-
"id":"<BINDING_ID>",
46
+
"binding":"NEXT_INC_CACHE_R2_BUCKET",
47
+
"bucket_name":"<BUCKET_NAME>",
46
48
},
47
49
],
48
50
"services": [
@@ -56,46 +58,62 @@ The `WORKER_SELF_REFERENCE` service binding should be a self reference to your w
56
58
57
59
##### 3. Configure the cache
58
60
59
-
In your project's OpenNext config, enable the KV cache.
61
+
In your project's OpenNext config, enable the R2 cache.
62
+
63
+
You can optionally setup a regional cache to use with the R2 incremental cache. This will enable faster retrieval of cache entries and reduce the amount of requests being sent to object storage.
64
+
65
+
The regional cache has two modes:
66
+
67
+
-`short-lived`: Responses are re-used for up to a minute.
68
+
-`long-lived`: Fetch responses are re-used until revalidated, and ISR/SSG responses are re-used for up to 30 minutes.
69
+
70
+
Additionally, lazy updating of the regional cache can be enabled with the `shouldLazilyUpdateOnCacheHit` option. When requesting data from the cache, it sends a background request to the R2 bucket to get the latest entry. This is enabled by default for the `long-lived` mode.
##### 2. Add the R2 Bucket and Service Binding to your Worker
84
-
85
-
The binding name used in your app's worker is `NEXT_INC_CACHE_R2_BUCKET`. The service binding should be a self reference to your worker where `<WORKER_NAME>` is the name in your wrangler configuration file.
103
+
##### 2. Add the KV namespace and Service Binding to your Worker
86
104
87
-
The prefix used by the R2 bucket can be configured with the `NEXT_INC_CACHE_R2_PREFIX` environment variable, and defaults to `incremental-cache`.
105
+
The binding name used in your app's worker is `NEXT_INC_CACHE_KV`.
106
+
The `WORKER_SELF_REFERENCE` service binding should be a self reference to your worker where `<WORKER_NAME>` is the name in your wrangler configuration file.
88
107
89
108
```jsonc
90
109
// wrangler.jsonc
91
110
{
92
111
// ...
93
112
"name":"<WORKER_NAME>",
94
-
"r2_buckets": [
113
+
"kv_namespaces": [
95
114
{
96
-
"binding":"NEXT_INC_CACHE_R2_BUCKET",
97
-
"bucket_name":"<BUCKET_NAME>",
98
-
"preview_bucket_name":"<PREVIEW_BUCKET_NAME>",
115
+
"binding":"NEXT_INC_CACHE_KV",
116
+
"id":"<BINDING_ID>",
99
117
},
100
118
],
101
119
"services": [
@@ -109,36 +127,16 @@ The prefix used by the R2 bucket can be configured with the `NEXT_INC_CACHE_R2_P
109
127
110
128
##### 3. Configure the cache
111
129
112
-
In your project's OpenNext config, enable the R2 cache.
113
-
114
-
You can optionally setup a regional cache to use with the R2 incremental cache. This will enable faster retrieval of cache entries and reduce the amount of requests being sent to object storage.
115
-
116
-
The regional cache has two modes:
117
-
118
-
-`short-lived`: Responses are re-used for up to a minute.
119
-
-`long-lived`: Fetch responses are re-used until revalidated, and ISR/SSG responses are re-used for up to 30 minutes.
120
-
121
-
Additionally, lazy updating of the regional cache can be enabled with the `shouldLazilyUpdateOnCacheHit` option. When requesting data from the cache, it sends a background request to the R2 bucket to get the latest entry. This is enabled by default for the `long-lived` mode.
130
+
In your project's OpenNext config, enable the KV cache.
@@ -189,7 +187,7 @@ You can customize the behaviors of the queue with environment variables:
189
187
- The max number of revalidations that can be processed by an instance of durable object at the same time (`NEXT_CACHE_DO_QUEUE_MAX_REVALIDATION`)
190
188
- The max time in milliseconds that a revalidation can take before being considered as failed (`NEXT_CACHE_DO_QUEUE_REVALIDATION_TIMEOUT_MS`)
191
189
- The amount of time after which a revalidation will be attempted again if it failed. If it fails again it will exponentially back off until it reaches the max retry interval (`NEXT_CACHE_DO_QUEUE_RETRY_INTERVAL_MS`)
192
-
- The maximum number of attempts that can be made to revalidate a path (`NEXT_CACHE_DO_QUEUE_MAX_NUM_REVALIDATIONS`)
190
+
- The maximum number of attempts that can be made to revalidate a path (`NEXT_CACHE_DO_QUEUE_MAX_RETRIES`)
193
191
- Disable SQLite for this durable object. It should only be used if your incremental cache is not eventually consistent (`NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE`)
194
192
195
193
<Callout>
@@ -216,15 +214,15 @@ To use on-demand revalidation, you should also follow the [ISR setup steps](#inc
216
214
You can also skip this step if your app doesn't to use `revalidateTag` nor `revalidatePath`.
217
215
</Callout>
218
216
219
-
There are 3 different options to choose from for the tag cache: `d1NextTagCache`, `doShardedTagCache` and `d1TagCache`.
217
+
There are 2 different options to choose from for the tag cache: `d1NextTagCache`, `doShardedTagCache`.
220
218
Which one to choose should be based on two key factors:
221
219
222
220
1.**Expected Load**: Consider the volume of traffic or data you anticipate.
223
221
2.**Usage of**`revalidateTag` / `revalidatePath`: Evaluate how frequently these features will be utilized.
224
222
225
223
If either of these factors is significant, opting for a sharded database is recommended. Additionally, incorporating a regional cache can further enhance performance.
@@ -256,22 +254,43 @@ The D1 tag cache requires a `revalidations` table that tracks On-Demand revalida
256
254
257
255
##### 3. Configure the cache
258
256
259
-
In your project's OpenNext config, enable the KV cache and set up a queue (see above). The queue will send a revalidation request to a page when needed, but it will not dedupe requests.
257
+
In your project's OpenNext config, enable the R2 cache and set up a queue (see above). The queue will send a revalidation request to a page when needed, but it will not dedupe requests.
In order for the cache to be properly initialised with the build-time revalidation data, you need to run a command as part of your deploy step. This should be run as part of each deployment to ensure that the cache is being populated with each build's data.
276
+
277
+
To populate remote bindings and deploy your application at the same time, you can use the `deploy` command. Similarly, the `preview` command will populate your local bindings and start a Wrangler dev server.
278
+
279
+
```sh
280
+
# Populate remote and deploy.
281
+
opennextjs-cloudflare deploy
282
+
283
+
# Populate local and start dev server.
284
+
opennextjs-cloudflare preview
285
+
```
286
+
287
+
It is possible to only populate the cache without any other steps with the `populateCache` command.
288
+
289
+
```sh
290
+
# The target is passed as an option, either `local` or `remote`.
291
+
opennextjs-cloudflare populateCache local
292
+
```
293
+
275
294
</Tabs.Tab>
276
295
277
296
<Tabs.Tab>
@@ -286,18 +305,18 @@ The service binding should be a self reference to your worker where `<WORKER_NAM
@@ -332,87 +351,10 @@ doShardedTagCache tahes the following options:
332
351
-`baseShardSize` - The number of shards to use for the cache. The more shards you have, the more evenly the cache will be distributed across the shards. The default is 4. Soft (internal next tags used for `revalidatePath`) and hard tags (the one you define in your app) will be split in different shards
333
352
-`regionalCache` - Whether to use regional cache for the cache. The default is false. This option is useful when you want to reduce the stress on the durable object
334
353
-`regionalCacheTtlSec` - The TTL for the regional cache. The default is 5 seconds. Increasing this value will increase the time it takes for the cache to be invalidated across regions
335
-
-`enableShardReplication`: Enable replicating the Shard. Shard replication will duplicate each shards into replicas to spread the load even more
336
-
-`shardReplicationOptions.numberOfSoftReplicas`: Number of replicas for the soft tag shards
337
-
-`shardReplicationOptions.numberOfHardReplicas`: Number of replicas for the hard tag shards
354
+
-`shardReplication`: Enable replicating the Shard. Shard replication will duplicate each shards into replicas to spread the load even more
355
+
-`shardReplication.numberOfSoftReplicas`: Number of replicas for the soft tag shards
356
+
-`shardReplication.numberOfHardReplicas`: Number of replicas for the hard tag shards
338
357
-`maxWriteRetries`: The number of retries to perform when writing tags
339
358
340
-
</Tabs.Tab>
341
-
342
-
<Tabs.Tab>
343
-
344
-
<Callout>
345
-
The `d1TagCache` is not recommended for production use, as it does not scale well with the number of tags.
346
-
</Callout>
347
-
348
-
##### 1. Create a D1 database and Service Binding
349
-
350
-
The binding name used in your app's worker is `NEXT_TAG_CACHE_D1`. The service binding should be a self reference to your worker where `<WORKER_NAME>` is the name in your wrangler configuration file.
351
-
352
-
```jsonc
353
-
// wrangler.jsonc
354
-
{
355
-
// ...
356
-
"name":"<WORKER_NAME>",
357
-
"d1_databases": [
358
-
{
359
-
"binding":"NEXT_TAG_CACHE_D1",
360
-
"database_id":"<DATABASE_ID>",
361
-
"database_name":"<DATABASE_NAME>",
362
-
},
363
-
],
364
-
"services": [
365
-
{
366
-
"binding":"WORKER_SELF_REFERENCE",
367
-
"service":"<WORKER_NAME>",
368
-
},
369
-
],
370
-
}
371
-
```
372
-
373
-
The D1 database uses two tables, created when initialising the cache:
374
-
375
-
- the "tags" table keeps a record of the tag/path mappings
376
-
- the "revalidations" table tracks revalidation times
377
-
378
-
##### 2. Configure the cache
379
-
380
-
In your project's OpenNext config, enable the KV cache and set up a queue. The queue will send a revalidation request to a page when needed, but it will not dedupe requests.
In order for the cache to be properly initialised with the build-time revalidation data, you need to run a command as part of your deploy step. This should be run as part of each deployment to ensure that the cache is being populated with each build's data.
399
-
400
-
To populate remote bindings and deploy your application at the same time, you can use the `deploy` command. Similarly, the `preview` command will populate your local bindings and start a Wrangler dev server.
401
-
402
-
```sh
403
-
# Populate remote and deploy.
404
-
opennextjs-cloudflare deploy
405
-
406
-
# Populate local and start dev server.
407
-
opennextjs-cloudflare preview
408
-
```
409
-
410
-
It is possible to only populate the cache without any other steps with the `populateCache` command.
411
-
412
-
```sh
413
-
# The target is passed as an option, either `local` or `remote`.
0 commit comments