Skip to content

Commit d7566e4

Browse files
authored
[cloudflare]: add docs for withFilter (#132)
1 parent e7b634b commit d7566e4

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

pages/cloudflare/caching.mdx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,18 @@ import { defineCloudflareConfig } from "@opennextjs/cloudflare";
9494
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
9595
import d1NextTagCache from "@opennextjs/cloudflare/overrides/tag-cache/d1-next-tag-cache";
9696
import doQueue from "@opennextjs/cloudflare/overrides/queue/do-queue";
97+
//import { withFilter, softTagFilter } from "@opennextjs/cloudflare/overrides/tag-cache/tag-cache-filter";
9798

9899
export default defineCloudflareConfig({
99100
incrementalCache: r2IncrementalCache,
100101
queue: doQueue,
101102
// This is only required if you use On-demand revalidation
102103
tagCache: d1NextTagCache,
104+
//If you don't use `revalidatePath`, you can also filter internal soft tags using the `softTagFilter`
105+
// tagCache: withFilter({
106+
// tagCache: d1NextTagCache,
107+
// filterFn: softTagFilter,
108+
// }),
103109
});
104110
```
105111

@@ -449,7 +455,7 @@ Which one to choose should be based on two key factors:
449455

450456
If either of these factors is significant, opting for a sharded database is recommended. Additionally, incorporating a regional cache can further enhance performance.
451457

452-
<Tabs items={["D1NextTagCache", "doShardedTagCache"]}>
458+
<Tabs items={["D1NextTagCache", "doShardedTagCache", "withFilter"]}>
453459
<Tabs.Tab>
454460

455461
**Create a D1 database and Service Binding**
@@ -588,5 +594,34 @@ doShardedTagCache takes the following options:
588594
- `numberOfHardReplicas`: Number of replicas for the hard tag shards
589595
- `maxWriteRetries`: The number of retries to perform when writing tags
590596

597+
</Tabs.Tab>
598+
<Tabs.Tab>
599+
<Callout>
600+
The `withFilter` option is a specialized configuration that enhances your `tagCache` by layering an additional filter. It requires another tag cache to be passed in as the tagCache (e.g., `d1NextTagCache`, `doShardedTagCache` or your own).
601+
</Callout>
602+
This enhanced tag cache selectively filters which tags trigger revalidations, allowing you to focus on a specific subset and reduce unnecessary load on the underlying tag cache. For convenience, we provide the ready-to-use `softTagFilter` that automatically filters out tags used by the `revalidatePath` function making it a no-op.
603+
604+
Its primary purpose is to filter out soft tags (i.e., those used by `revalidatePath`) from the tag cache, which are not relevant for your application. It could also be used to filter out hard tags (i.e., those you define in your app) if one of your dependencies uses them.
605+
606+
```ts
607+
// open-next.config.ts
608+
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
609+
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
610+
import doQueue from "@opennextjs/cloudflare/overrides/queue/do-queue";
611+
import d1NextTagCache from "@opennextjs/cloudflare/overrides/tag-cache/d1-next-tag-cache";
612+
import { withFilter, softTagFilter } from "@opennextjs/cloudflare/overrides/tag-cache/tag-cache-filter";
613+
614+
export default defineCloudflareConfig({
615+
incrementalCache: r2IncrementalCache,
616+
queue: doQueue,
617+
tagCache: withFilter({
618+
tagCache: d1NextTagCache,
619+
filterFn: softTagFilter,
620+
}),
621+
});
622+
```
623+
624+
You can also create your own custom filter function. This function must return a boolean value indicating whether a tag should be included. It will be invoked with a single tag (as a string) as its argument. Please note that "soft tags" (i.e., those used by revalidatePath) always start with the prefix `_N_T`.
625+
591626
</Tabs.Tab>
592627
</Tabs>

0 commit comments

Comments
 (0)