Skip to content

Commit 0758e01

Browse files
committed
docs: add revalidation examples to building pages entry
1 parent 06ebfb8 commit 0758e01

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

www/content/docs/pages.mdx

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Building Pages
33
excerpt: How to build pages using JSON:API resources from Drupal.
44
---
55

6-
In Next.js V14, data fetching has evolved significantly from previous versions. Instead of using `getStaticProps` and `getServerSideProps` you now use the native `fetch` function enhanced by Next.js to handle server-side data fetching. This also allows you to configure caching and revalidation directly within your fetch requests. These can be used in Server Components, Route Handlers, and Server Actions. You can read more about it [here](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating).
6+
In the Next.js App Router, data fetching has evolved significantly from previous versions. The native `fetch` function enhanced by Next.js can be used to handle server-side data fetching. This also allows you to configure caching and revalidation directly within your fetch requests. These can be used in Server Components, Route Handlers, and Server Actions. You can read more about it [here](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating).
77

88
The `NextDrupal` client provides several functions to help you query JSON:API resources from Drupal and manage these revalidation options on the `fetch` request level.
99

@@ -40,7 +40,17 @@ export default function AboutPage() {
4040
}
4141
```
4242

43-
TODO - time based revalidation.
43+
### Time-based Revalidation
44+
45+
To use time-based revalidation, you can pass a `revalidate` option to the `getResource` function. This will set the cache lifetime of a resource (in seconds).
46+
47+
```tsx
48+
const node = await drupal.getResource(
49+
"node--page",
50+
"07464e9f-9221-4a4f-b7f2-01389408e6c8",
51+
{ next: { revalidate: 3600 } }
52+
)
53+
```
4454

4555
---
4656

@@ -145,7 +155,58 @@ export default function Page({ params }) {
145155
}
146156
```
147157

148-
TODO - tag based revalidation.
158+
### Tag-based Revalidation
159+
160+
In addition to revalidating based on time, it is also possible to revalidate
161+
based on cache tag values. This is useful when you want to revalidate a resource
162+
based on changes to other resources.
163+
164+
Below we've adapted the Page function from the example above to include
165+
tag-based revalidation.
166+
167+
```tsx title=app/[...slug]/page.tsx
168+
import { DrupalJsonApiParams } from "drupal-jsonapi-params"
169+
170+
...
171+
172+
export default function Page({ params }) {
173+
const {slug} = params;
174+
175+
const path = drupal.translatePath(slug)
176+
177+
// Get the resource type.
178+
const type = path.jsonapi.resourceName
179+
const tag = `${path.entity.type}:${path.entity.id}`
180+
181+
const params = new DrupalJsonApiParams()
182+
183+
// Fetch the title, path and body field for pages.
184+
if (type === "node--page") {
185+
params.addFields("node--page", ["title", "path", "body"])
186+
}
187+
188+
// Fetch additional fields for articles.
189+
if (type === "node--article") {
190+
params.addFields("node--article", ["title", "path", "body", "uid"])
191+
}
192+
193+
const node = await drupal.getResource(path, path.entity.uuid {
194+
params: params.getQueryObject(),
195+
tags: [tag]
196+
})
197+
198+
// Render different Components based on Node type.
199+
if (node.type === "node--page") {
200+
return <PageComponent node={node}/>
201+
}
202+
203+
if (node.type === "node--article") {
204+
return <ArticleComponent node={node}/>
205+
}
206+
207+
return null
208+
}
209+
```
149210

150211
## Reference
151212

0 commit comments

Comments
 (0)