forked from chapter-three/next-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cache options to getResource/getResourceCollection calls in basic…
… starter page templates Fixes chapter-three#808 Experimenting with Copilot Workspace here... --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/chapter-three/next-drupal/issues/808?shareId=XXXX-XXXX-XXXX-XXXX).
- Loading branch information
1 parent
a50598c
commit 1675889
Showing
2 changed files
with
26 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,40 @@ | ||
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 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) | ||
|
||
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, | ||
}) | ||
import { notFound } from "next/navigation" | ||
import { DrupalNode } from "next-drupal" | ||
|
||
if (!resource) { | ||
throw new Error( | ||
`Failed to fetch resource: ${translatedPath?.jsonapi?.individual}`, | ||
{ | ||
cause: "DrupalError", | ||
} | ||
) | ||
interface PageProps { | ||
params: { | ||
slug: string[] | ||
} | ||
|
||
return resource | ||
} | ||
|
||
type NodePageParams = { | ||
slug: string[] | ||
} | ||
type NodePageProps = { | ||
params: NodePageParams | ||
searchParams: { [key: string]: string | string[] | undefined } | ||
} | ||
export default async function Page({ params }: PageProps) { | ||
const path = `/${params.slug.join("/")}` | ||
const pathData = await drupal.translatePath(path) | ||
|
||
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, | ||
if (!pathData || !pathData.entity) { | ||
notFound() | ||
} | ||
} | ||
|
||
const RESOURCE_TYPES = ["node--page", "node--article"] | ||
const tag = `${pathData.entity.type}:${pathData.entity.id}` | ||
|
||
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, | ||
} | ||
const node = await drupal.getResource<DrupalNode>(pathData.entity.type, pathData.entity.id, { | ||
params: { | ||
include: "field_image,uid", | ||
}, | ||
next: { | ||
revalidate: 3600, | ||
// tags: [tag], | ||
}, | ||
}) | ||
} | ||
|
||
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) { | ||
if (!node) { | ||
notFound() | ||
} | ||
|
||
return ( | ||
<> | ||
{node.type === "node--page" && <BasicPage node={node} />} | ||
{node.type === "node--article" && <Article node={node} />} | ||
</> | ||
<article> | ||
<h1>{node.title}</h1> | ||
</article> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters