Skip to content

Commit 5f184c0

Browse files
committed
docs: building pages updates
* Add pages router examples to building pages entry. * Add todos about possible changes to on-demand revalidation section.
1 parent 0758e01 commit 5f184c0

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

www/content/docs/pages.mdx

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,92 @@ See the [fetching JSON:API resources](/docs/fetching-resources) section for more
214214

215215
## Pages Router
216216

217-
TODO - Port some examples from the previous docs.
217+
When using the Pages Router, you will use the `getStaticProps` and `getServerSideProps` methods to fetch data for your pages.
218+
219+
### Basic Example
220+
221+
Here's an example which uses `getResource` to fetch a `page` node by ID:
222+
223+
```tsx
224+
const node = await drupal.getResource(
225+
"node--page",
226+
"07464e9f-9221-4a4f-b7f2-01389408e6c8"
227+
)
228+
```
229+
230+
A full page would look like this:
231+
232+
```tsx title=pages/about.tsx
233+
// node will be populated at build time by getStaticProps
234+
export default function AboutPage({ node }) {
235+
return (
236+
<article>
237+
<h1>{node.title}</h1>
238+
// ...
239+
</article>
240+
)
241+
}
242+
243+
export async function getStaticProps() {
244+
// Fetch the node from Drupal.
245+
const node = await drupal.getResource(
246+
"node--page",
247+
"07464e9f-9221-4a4f-b7f2-01389408e6c8"
248+
)
249+
250+
// Pass the node as props to the AboutPage.
251+
return {
252+
props: {
253+
node,
254+
},
255+
}
256+
}
257+
```
258+
259+
---
260+
261+
## Dynamic pages
262+
263+
You can use Next.js [dynamic route](https://nextjs.org/docs/basic-features/pages#pages-with-dynamic-routes) to build static pages for Drupal entity types.
264+
265+
Start by creating a page at `/pages/[...slug].tsx`, where `[...slug]` maps to the **path alias** for an entity type (or content type) in Drupal.
266+
267+
This means `/pages/[...slug].tsx` will handle all pages with the following aliases: `/about`, `/team`, `/another/path` ...etc.
268+
269+
To build static pages, there are two functions we need to implement:
270+
271+
1. `getStaticPaths`: to tell Next.js all the routes that we want to be rendered.
272+
2. `getStaticProps`: to fetch data for pages.
273+
274+
```tsx title=pages/[...slug].tsx
275+
export default function Page({ node }) {
276+
return (
277+
<article>
278+
<h1>{node.title}</h1>
279+
// ...
280+
</article>
281+
)
282+
}
283+
284+
export async function getStaticPaths(context) {
285+
// Build paths for all `node--page`.
286+
return {
287+
paths: await drupal.getStaticPathsFromContext("node--page", context),
288+
fallback: false,
289+
}
290+
}
291+
292+
export async function getStaticProps(context) {
293+
// Fetch the node based on the context.
294+
// next-drupal automatically handles the slug value.
295+
const node = await drupal.getResourceFromContext("node--page", context)
296+
297+
return {
298+
props: {
299+
node,
300+
},
301+
}
302+
}
303+
```
304+
305+
---

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ group: On-demand Revalidation
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

1010
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...

0 commit comments

Comments
 (0)