Skip to content

Commit 6783a36

Browse files
committed
docs: updates to on-demand validation quick start
1 parent 35c5f6e commit 6783a36

File tree

4 files changed

+124
-13
lines changed

4 files changed

+124
-13
lines changed

www/content/tutorials/on-demand-revalidation/configure-entity-types.mdx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@ A _revalidator_ tells Drupal how to revalidate an entity when it is created, upd
1515

1616
</Callout>
1717

18+
### Configure Tag-based Revalidation
19+
1820
1. Visit `/admin/config/services/next/entity-types`
1921
2. Click **Configure entity type** or **Edit** an existing one
2022
3. Click **On-demand Revalidation**
21-
4. Select **Path** as the revalidator **Plugin**
22-
5. Check **Revalidate page**
23+
4. Select **Cache Tag** as the revalidator **Plugin**
24+
5. If using the **Path** plugin, check the **Revalidate page** checkbox
2325
6. Click **Save**
2426

27+
### Configure Path-based Revalidation
28+
29+
7. Visit `/admin/config/services/next/entity-types`
30+
8. Click **Configure entity type** or **Edit** an existing one
31+
9. Click **On-demand Revalidation**
32+
10. Select **Path** as the revalidator **Plugin**
33+
11. Check the **Revalidate page** checkbox
34+
12. Click **Save**
35+
2536
If you want to revalidate multiple pages, you can configure **Additional paths**. Example: you might want to revalidate the `/blog` landing page when an Article is created.

www/content/tutorials/on-demand-revalidation/configure-revalidate-route.mdx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,50 @@ group: On-demand Revalidation
77

88
Implement on-demand revalidation using an API route at `/revalidate`.
99

10+
## Update Environment Variables
11+
12+
First, update `.env.local` to include the revalidate secret you specified.
13+
14+
```txt title=.env.local
15+
# Required for On-demand Revalidation
16+
DRUPAL_REVALIDATE_SECRET=secret
17+
```
18+
19+
## Create the Revalidate Route
20+
21+
Next, create the API route that will be used for on-demand revalidation.
22+
1023
<Callout>
1124

12-
If you're using the Basic Starter, revalidate route are already created for you. You can skip this step.
25+
If you're using the Basic Starter, the revalidate route is already created for you. You can skip this step.
1326

1427
</Callout>
1528

1629
## /app/api/revalidate/route.ts
1730

1831
```ts title=app/api/revalidate/route.ts
19-
import { revalidatePath } from "next/cache"
32+
import { revalidatePath, revalidateTag } from "next/cache"
2033
import type { NextRequest } from "next/server"
2134

2235
async function handler(request: NextRequest) {
2336
const searchParams = request.nextUrl.searchParams
2437
const path = searchParams.get("path")
38+
const tags = searchParams.get("tags")
2539
const secret = searchParams.get("secret")
2640

2741
// Validate secret.
2842
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
2943
return new Response("Invalid secret.", { status: 401 })
3044
}
3145

32-
// Validate path.
33-
if (!path) {
34-
return new Response("Invalid path.", { status: 400 })
46+
// Either tags or path must be provided.
47+
if (!path && !tags) {
48+
return new Response("Missing path or tags.", { status: 400 })
3549
}
3650

3751
try {
38-
revalidatePath(path)
52+
path && revalidatePath(path)
53+
tags?.split(",").forEach((tag) => revalidateTag(tag))
3954

4055
return new Response("Revalidated.")
4156
} catch (error) {

www/content/tutorials/on-demand-revalidation/index.mdx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,4 @@ group: On-demand Revalidation
77

88
[On-demand Revalidation](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation) makes it easy to update your Next.js site when content on Drupal is created, updated or deleted.
99

10-
Starting with `v1.6.0`, Next.js for Drupal supports On-demand Revalidation configurable per entity types.
11-
12-
TODO - in v2, Next.js for Drupal supports...
13-
TODO - maybe this section should just be renamed to validation?
14-
TODO - maybe move revalidation examples from Building Pages to this section...
10+
Next.js for Drupal supports On-demand Revalidation configurable per entity types. You can choose to revalidate content by path, or by tag. Tag-based invalidation is new as of Next Drupal 2.0 and allows you to invalidate content on your Next.js site using Drupal's powerful cache tag system.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: Set Revalidation Parameters
3+
excerpt: Set Revalidation Parameters When Fetching Data in Next.js
4+
weight: 370
5+
group: On-demand Revalidation
6+
---
7+
8+
When fetching data using the Next Drupal Client, you will also need to set the necessary revalidation parameters. This tells Next.js that the content should be cached, and what metadata can be used to invalidate that cache.
9+
10+
<Callout>
11+
12+
If you're using the Basic Starter, most of this configuration is already created for you. You will only need to replace `revalidate` with `tags` in the options object when using Next Drupal Client methods like `getResource` if you'd prefer to use tag-based revalidation.
13+
14+
</Callout>
15+
16+
Consider the `getNode` function in the `[...slug]` dynamic route in the basic starter:
17+
18+
```ts title=app/[..slug]/page.tsx
19+
async function getNode(slug: string[]) {
20+
const path = `/${slug.join("/")}`
21+
22+
const params: JsonApiParams = {}
23+
24+
// Translating the path also allows us to discover the entity type.
25+
const translatedPath = await drupal.translatePath(path)
26+
27+
if (!translatedPath) {
28+
throw new Error("Resource not found", { cause: "NotFound" })
29+
}
30+
31+
const type = translatedPath.jsonapi?.resourceName!
32+
const uuid = translatedPath.entity.uuid
33+
const tag = `${translatedPath.entity.type}:${translatedPath.entity.id}`
34+
35+
if (type === "node--article") {
36+
params.include = "field_image,uid"
37+
}
38+
39+
const resource = await drupal.getResource<DrupalNode>(type, uuid, {
40+
params,
41+
cache: "force-cache",
42+
next: {
43+
revalidate: 3600,
44+
// Replace `revalidate` with `tags` if using tag based revalidation.
45+
// tags: [tag],
46+
},
47+
})
48+
49+
return resource
50+
}
51+
```
52+
53+
## Path-based Revalidation
54+
55+
The call to `getResource` in the above example is already configured to use path-based invalidation. The data will be cached for 3600 seconds unless revalidated on-demand.
56+
57+
```tsx
58+
const resource = await drupal.getResource<DrupalNode>(type, uuid, {
59+
params,
60+
cache: "force-cache",
61+
next: {
62+
revalidate: 3600,
63+
},
64+
})
65+
```
66+
67+
## Tag-based Revalidation
68+
69+
To instead use tag-based invalidation, we can replace the `revalidate` option with the `tags` option:
70+
71+
```tsx
72+
const resource = await drupal.getResource<DrupalNode>(type, uuid, {
73+
params,
74+
cache: "force-cache",
75+
next: {
76+
tags: [tag],
77+
},
78+
})
79+
```
80+
81+
In this example, the tag is set earlier in the function using data from the translated path:
82+
83+
```tsx
84+
const type = translatedPath.jsonapi?.resourceName!
85+
const uuid = translatedPath.entity.uuid
86+
const tag = `${translatedPath.entity.type}:${translatedPath.entity.id}`
87+
```
88+
89+
This option accepts an array of tags, and values should match the cache tag values set by Drupal. With this configuration, the data will be cached until Drupal makes a call to the revalidate route using any of the matching cache tags.

0 commit comments

Comments
 (0)