|
1 | | -import Head from "next/head" |
| 1 | +import { draftMode } from "next/headers" |
| 2 | +import { notFound } from "next/navigation" |
| 3 | +import { getDraftData } from "next-drupal/draft" |
2 | 4 | import { Article } from "@/components/drupal/Article" |
3 | 5 | import { BasicPage } from "@/components/drupal/BasicPage" |
4 | | -import { Layout } from "@/components/Layout" |
5 | 6 | import { drupal } from "@/lib/drupal" |
6 | | -import type { |
7 | | - GetStaticPaths, |
8 | | - GetStaticProps, |
9 | | - InferGetStaticPropsType, |
10 | | -} from "next" |
| 7 | +import type { Metadata, ResolvingMetadata } from "next" |
11 | 8 | import type { DrupalArticle, DrupalPage, NodesPath } from "@/types" |
12 | 9 |
|
13 | | -export const getStaticPaths = (async (context) => { |
14 | | - // Fetch the paths for the first 50 articles and pages. |
15 | | - // We'll fall back to on-demand generation for the rest. |
16 | | - const data = await drupal.query<{ |
17 | | - nodeArticles: NodesPath |
18 | | - nodePages: NodesPath |
19 | | - }>({ |
20 | | - query: `query { |
21 | | - nodeArticles(first: 50) { |
22 | | - nodes { |
23 | | - path, |
24 | | - } |
25 | | - } |
26 | | - nodePages(first: 50) { |
27 | | - nodes { |
28 | | - path, |
29 | | - } |
30 | | - } |
31 | | - }`, |
32 | | - }) |
33 | | - |
34 | | - // Build static paths. |
35 | | - const paths = [ |
36 | | - ...(data?.nodeArticles?.nodes as { path: string }[]), |
37 | | - ...(data?.nodePages?.nodes as { path: string }[]), |
38 | | - ].map(({ path }) => ({ params: { slug: path.split("/").filter(Boolean) } })) |
39 | | - |
40 | | - return { |
41 | | - paths, |
42 | | - fallback: "blocking", |
43 | | - } |
44 | | -}) satisfies GetStaticPaths |
45 | | - |
46 | | -export const getStaticProps = (async (context) => { |
47 | | - if (!context?.params?.slug) { |
48 | | - return { |
49 | | - notFound: true, |
50 | | - } |
51 | | - } |
| 10 | +async function getNode(slug: string[]) { |
| 11 | + const path = `/${slug.join("/")}` |
52 | 12 |
|
53 | 13 | const data = await drupal.query<{ |
54 | 14 | route: { entity: DrupalArticle | DrupalPage } |
@@ -92,46 +52,96 @@ export const getStaticProps = (async (context) => { |
92 | 52 | } |
93 | 53 | }`, |
94 | 54 | variables: { |
95 | | - path: `/${(context.params.slug as []).join("/")}`, |
| 55 | + path, |
96 | 56 | }, |
97 | 57 | }) |
98 | 58 |
|
99 | 59 | const resource = data?.route?.entity |
100 | 60 |
|
101 | | - // If we're not in preview mode and the resource is not published, |
102 | | - // Return page not found. |
103 | | - if (!resource || (!context.preview && resource?.status === false)) { |
104 | | - return { |
105 | | - notFound: true, |
106 | | - } |
| 61 | + if (!resource) { |
| 62 | + throw new Error(`Failed to fetch resource: ${path}`, { |
| 63 | + cause: "DrupalError", |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + return resource |
| 68 | +} |
| 69 | + |
| 70 | +type NodePageParams = { |
| 71 | + slug: string[] |
| 72 | +} |
| 73 | +type NodePageProps = { |
| 74 | + params: NodePageParams |
| 75 | + searchParams: { [key: string]: string | string[] | undefined } |
| 76 | +} |
| 77 | + |
| 78 | +export async function generateMetadata( |
| 79 | + { params: { slug } }: NodePageProps, |
| 80 | + parent: ResolvingMetadata |
| 81 | +): Promise<Metadata> { |
| 82 | + let node |
| 83 | + try { |
| 84 | + node = await getNode(slug) |
| 85 | + } catch (e) { |
| 86 | + // If we fail to fetch the node, don't return any metadata. |
| 87 | + return {} |
107 | 88 | } |
108 | 89 |
|
109 | 90 | return { |
110 | | - props: { |
111 | | - resource, |
112 | | - }, |
| 91 | + title: node.title, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export async function generateStaticParams(): Promise<NodePageParams[]> { |
| 96 | + // Fetch the paths for the first 50 articles and pages. |
| 97 | + // We'll fall back to on-demand generation for the rest. |
| 98 | + const data = await drupal.query<{ |
| 99 | + nodeArticles: NodesPath |
| 100 | + nodePages: NodesPath |
| 101 | + }>({ |
| 102 | + query: `query { |
| 103 | + nodeArticles(first: 50) { |
| 104 | + nodes { |
| 105 | + path, |
| 106 | + } |
| 107 | + } |
| 108 | + nodePages(first: 50) { |
| 109 | + nodes { |
| 110 | + path, |
| 111 | + } |
| 112 | + } |
| 113 | + }`, |
| 114 | + }) |
| 115 | + |
| 116 | + return [ |
| 117 | + ...(data?.nodeArticles?.nodes as { path: string }[]), |
| 118 | + ...(data?.nodePages?.nodes as { path: string }[]), |
| 119 | + ].map(({ path }) => ({ slug: path.split("/").filter(Boolean) })) |
| 120 | +} |
| 121 | + |
| 122 | +export default async function Page({ |
| 123 | + params: { slug }, |
| 124 | + searchParams, |
| 125 | +}: NodePageProps) { |
| 126 | + const isDraftMode = draftMode().isEnabled |
| 127 | + |
| 128 | + let node |
| 129 | + try { |
| 130 | + node = await getNode(slug) |
| 131 | + } catch (error) { |
| 132 | + // If getNode throws an error, tell Next.js the path is 404. |
| 133 | + notFound() |
113 | 134 | } |
114 | | -}) satisfies GetStaticProps<{ |
115 | | - resource: DrupalArticle | DrupalPage |
116 | | -}> |
117 | 135 |
|
118 | | -export default function Page({ |
119 | | - resource, |
120 | | -}: InferGetStaticPropsType<typeof getStaticProps>) { |
121 | | - if (!resource) return null |
| 136 | + // If we're not in draft mode and the resource is not published, return a 404. |
| 137 | + if (!isDraftMode && node?.status === false) { |
| 138 | + notFound() |
| 139 | + } |
122 | 140 |
|
123 | 141 | return ( |
124 | | - <Layout> |
125 | | - <Head> |
126 | | - <title>{resource.title}</title> |
127 | | - <meta |
128 | | - name="description" |
129 | | - content="A Next.js site powered by Drupal." |
130 | | - key="description" |
131 | | - /> |
132 | | - </Head> |
133 | | - {resource.__typename === "NodePage" && <BasicPage node={resource} />} |
134 | | - {resource.__typename === "NodeArticle" && <Article node={resource} />} |
135 | | - </Layout> |
| 142 | + <> |
| 143 | + {node.__typename === "NodePage" && <BasicPage node={node} />} |
| 144 | + {node.__typename === "NodeArticle" && <Article node={node} />} |
| 145 | + </> |
136 | 146 | ) |
137 | 147 | } |
0 commit comments