From 16758892e25f7b9177106b8b095a0a1ff775d383 Mon Sep 17 00:00:00 2001 From: Brian Perry Date: Thu, 7 Nov 2024 10:23:03 -0600 Subject: [PATCH] Add cache options to getResource/getResourceCollection calls in basic starter page templates Fixes #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). --- starters/basic-starter/app/[...slug]/page.tsx | 135 +++--------------- starters/basic-starter/app/page.tsx | 3 + 2 files changed, 26 insertions(+), 112 deletions(-) diff --git a/starters/basic-starter/app/[...slug]/page.tsx b/starters/basic-starter/app/[...slug]/page.tsx index 4e61889cb..b3c9327c6 100644 --- a/starters/basic-starter/app/[...slug]/page.tsx +++ b/starters/basic-starter/app/[...slug]/page.tsx @@ -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(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 { - 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 { - 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(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" && } - {node.type === "node--article" &&
} - +
+

{node.title}

+
) } diff --git a/starters/basic-starter/app/page.tsx b/starters/basic-starter/app/page.tsx index 01aaac5aa..a3ded1598 100644 --- a/starters/basic-starter/app/page.tsx +++ b/starters/basic-starter/app/page.tsx @@ -17,6 +17,9 @@ export default async function Home() { include: "field_image,uid", sort: "-created", }, + next: { + revalidate: 3600, + }, } )