Skip to content

Commit

Permalink
feat(examples): update example-router-migration with getResourceColle…
Browse files Browse the repository at this point in the history
…ctionPathSegments()

Issue #665
  • Loading branch information
JohnAlbin committed Apr 18, 2024
1 parent d097337 commit 52a3c73
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/example-router-migration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Over time, you will be moving all the files from `/pages` to `/app`. However, th
2. Delete the last files in your `/pages` directory:
- `/pages/api/exit-preview.ts`
- `/pages/api/preview.ts`
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).

## License

Expand Down
30 changes: 22 additions & 8 deletions examples/example-router-migration/app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,29 @@ export async function generateMetadata(
const RESOURCE_TYPES = ["node--page", "node--article"]

export async function generateStaticParams(): Promise<NodePageParams[]> {
// TODO: Replace getStaticPathsFromContext() usage since there is no context.
const paths = await drupal.getStaticPathsFromContext(RESOURCE_TYPES, {})
// console.log(
// "generateStaticParams",
// paths.map(({ params }) => params)
// )
return paths.map((path: string | { params: NodePageParams }) =>
typeof path === "string" ? { slug: [] } : path?.params
const resources = await drupal.getResourceCollectionPathSegments(
RESOURCE_TYPES,
{
// The pathPrefix will be removed from the returned path segments array.
// pathPrefix: "/blog",
// The list of locales to return.
// locales: ["en", "es"],
// The default locale.
// defaultLocale: "en",
}
)

return resources.map((resource) => {
// resources is an array containing objects like: {
// path: "/blog/some-category/a-blog-post",
// type: "node--article",
// locale: "en", // or `undefined` if no `locales` requested.
// segments: ["blog", "some-category", "a-blog-post"],
// }
return {
slug: resource.segments,
}
})
}

export default async function NodePage({
Expand Down
15 changes: 14 additions & 1 deletion examples/example-router-migration/lib/drupal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { DrupalClient } from "next-drupal"
import {
DrupalClient,
// NextDrupal
} from "next-drupal"

const baseUrl: string = process.env.NEXT_PUBLIC_DRUPAL_BASE_URL || ""
const clientId = process.env.DRUPAL_CLIENT_ID || ""
Expand All @@ -11,3 +14,13 @@ export const drupal = new DrupalClient(baseUrl, {
},
debug: true,
})

// TODO: Once you have migrated fully to App Router, switch to the leaner
// NextDrupal class (which contains none of the Pages Router-specific methods.)
// export const drupal = new NextDrupal(baseUrl, {
// auth: {
// clientId,
// clientSecret,
// },
// debug: true,
// })

0 comments on commit 52a3c73

Please sign in to comment.