Skip to content

Commit 52a3c73

Browse files
committed
feat(examples): update example-router-migration with getResourceCollectionPathSegments()
Issue #665
1 parent d097337 commit 52a3c73

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

examples/example-router-migration/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Over time, you will be moving all the files from `/pages` to `/app`. However, th
3737
2. Delete the last files in your `/pages` directory:
3838
- `/pages/api/exit-preview.ts`
3939
- `/pages/api/preview.ts`
40+
3. Update your `lib/drupal.ts` file and switch from the `DrupalClient` class (with dual Pages/App Router support) to the new, leaner `NextDrupal` class (with only App Router support).
4041

4142
## License
4243

examples/example-router-migration/app/[...slug]/page.tsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,29 @@ export async function generateMetadata(
7676
const RESOURCE_TYPES = ["node--page", "node--article"]
7777

7878
export async function generateStaticParams(): Promise<NodePageParams[]> {
79-
// TODO: Replace getStaticPathsFromContext() usage since there is no context.
80-
const paths = await drupal.getStaticPathsFromContext(RESOURCE_TYPES, {})
81-
// console.log(
82-
// "generateStaticParams",
83-
// paths.map(({ params }) => params)
84-
// )
85-
return paths.map((path: string | { params: NodePageParams }) =>
86-
typeof path === "string" ? { slug: [] } : path?.params
79+
const resources = await drupal.getResourceCollectionPathSegments(
80+
RESOURCE_TYPES,
81+
{
82+
// The pathPrefix will be removed from the returned path segments array.
83+
// pathPrefix: "/blog",
84+
// The list of locales to return.
85+
// locales: ["en", "es"],
86+
// The default locale.
87+
// defaultLocale: "en",
88+
}
8789
)
90+
91+
return resources.map((resource) => {
92+
// resources is an array containing objects like: {
93+
// path: "/blog/some-category/a-blog-post",
94+
// type: "node--article",
95+
// locale: "en", // or `undefined` if no `locales` requested.
96+
// segments: ["blog", "some-category", "a-blog-post"],
97+
// }
98+
return {
99+
slug: resource.segments,
100+
}
101+
})
88102
}
89103

90104
export default async function NodePage({

examples/example-router-migration/lib/drupal.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { DrupalClient } from "next-drupal"
1+
import {
2+
DrupalClient,
3+
// NextDrupal
4+
} from "next-drupal"
25

36
const baseUrl: string = process.env.NEXT_PUBLIC_DRUPAL_BASE_URL || ""
47
const clientId = process.env.DRUPAL_CLIENT_ID || ""
@@ -11,3 +14,13 @@ export const drupal = new DrupalClient(baseUrl, {
1114
},
1215
debug: true,
1316
})
17+
18+
// TODO: Once you have migrated fully to App Router, switch to the leaner
19+
// NextDrupal class (which contains none of the Pages Router-specific methods.)
20+
// export const drupal = new NextDrupal(baseUrl, {
21+
// auth: {
22+
// clientId,
23+
// clientSecret,
24+
// },
25+
// debug: true,
26+
// })

0 commit comments

Comments
 (0)