|
| 1 | +import { draftMode } from "next/headers" |
| 2 | +import { notFound } from "next/navigation" |
| 3 | +import { getDraftData } from "next-drupal/draft" |
| 4 | +import { Article } from "@/components/drupal/Article" |
| 5 | +import { BasicPage } from "@/components/drupal/BasicPage" |
| 6 | +import { drupal } from "@/lib/drupal" |
| 7 | +import type { Metadata, ResolvingMetadata } from "next" |
| 8 | +import type { DrupalNode, JsonApiParams } from "next-drupal" |
| 9 | + |
| 10 | +async function getNode(slug: string[]) { |
| 11 | + const path = `/${slug.join("/")}` |
| 12 | + |
| 13 | + const params: JsonApiParams = {} |
| 14 | + |
| 15 | + const draftData = getDraftData() |
| 16 | + |
| 17 | + if (draftData.path === path) { |
| 18 | + params.resourceVersion = draftData.resourceVersion |
| 19 | + } |
| 20 | + |
| 21 | + // Translating the path also allows us to discover the entity type. |
| 22 | + const translatedPath = await drupal.translatePath(path) |
| 23 | + |
| 24 | + if (!translatedPath) { |
| 25 | + throw new Error("Resource not found", { cause: "NotFound" }) |
| 26 | + } |
| 27 | + |
| 28 | + const type = translatedPath.jsonapi?.resourceName! |
| 29 | + const uuid = translatedPath.entity.uuid |
| 30 | + |
| 31 | + if (type === "node--article") { |
| 32 | + params.include = "field_image,uid" |
| 33 | + } |
| 34 | + |
| 35 | + const resource = await drupal.getResource<DrupalNode>(type, uuid, { |
| 36 | + params, |
| 37 | + }) |
| 38 | + |
| 39 | + if (!resource) { |
| 40 | + throw new Error( |
| 41 | + `Failed to fetch resource: ${translatedPath?.jsonapi?.individual}`, |
| 42 | + { |
| 43 | + cause: "DrupalError", |
| 44 | + } |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + return resource |
| 49 | +} |
| 50 | + |
| 51 | +type NodePageParams = { |
| 52 | + slug: string[] |
| 53 | +} |
| 54 | +type NodePageProps = { |
| 55 | + params: NodePageParams |
| 56 | + searchParams: { [key: string]: string | string[] | undefined } |
| 57 | +} |
| 58 | + |
| 59 | +export async function generateMetadata( |
| 60 | + { params: { slug } }: NodePageProps, |
| 61 | + parent: ResolvingMetadata |
| 62 | +): Promise<Metadata> { |
| 63 | + let node |
| 64 | + try { |
| 65 | + node = await getNode(slug) |
| 66 | + } catch (e) { |
| 67 | + // If we fail to fetch the node, don't return any metadata. |
| 68 | + return {} |
| 69 | + } |
| 70 | + |
| 71 | + return { |
| 72 | + title: node.title, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +const RESOURCE_TYPES = ["node--page", "node--article"] |
| 77 | + |
| 78 | +export async function generateStaticParams(): Promise<NodePageParams[]> { |
| 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 | + } |
| 89 | + ) |
| 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 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +export default async function NodePage({ |
| 105 | + params: { slug }, |
| 106 | + searchParams, |
| 107 | +}: NodePageProps) { |
| 108 | + const isDraftMode = draftMode().isEnabled |
| 109 | + |
| 110 | + let node |
| 111 | + try { |
| 112 | + node = await getNode(slug) |
| 113 | + } catch (error) { |
| 114 | + // If getNode throws an error, tell Next.js the path is 404. |
| 115 | + notFound() |
| 116 | + } |
| 117 | + |
| 118 | + // If we're not in draft mode and the resource is not published, return a 404. |
| 119 | + if (!isDraftMode && node?.status === false) { |
| 120 | + notFound() |
| 121 | + } |
| 122 | + |
| 123 | + return ( |
| 124 | + <> |
| 125 | + {node.type === "node--page" && <BasicPage node={node} />} |
| 126 | + {node.type === "node--article" && <Article node={node} />} |
| 127 | + </> |
| 128 | + ) |
| 129 | +} |
0 commit comments