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