Skip to content

Commit

Permalink
Try to talk copilot into modifying the existing file.
Browse files Browse the repository at this point in the history
  • Loading branch information
backlineint committed Nov 7, 2024
1 parent 1675889 commit 84d1e07
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions starters/basic-starter/app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,136 @@ export default async function Page({ params }: PageProps) {
</article>
)
}
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,
next: {
revalidate: 3600,
// tags: [`${type}:${uuid}`],
},
})

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} />}
</>
)
}

0 comments on commit 84d1e07

Please sign in to comment.