From 2bd53a06eb9600406e51613980921b297efd6ffc Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 18 Jul 2024 14:20:05 -0400 Subject: [PATCH] chore(templates): add react cache to queryPostBySlug in website template (#7219) --- .../website/src/app/(frontend)/[slug]/page.tsx | 12 ++++-------- .../src/app/(frontend)/posts/[slug]/page.tsx | 13 ++++--------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/templates/website/src/app/(frontend)/[slug]/page.tsx b/templates/website/src/app/(frontend)/[slug]/page.tsx index b8ca54bac..9666ca162 100644 --- a/templates/website/src/app/(frontend)/[slug]/page.tsx +++ b/templates/website/src/app/(frontend)/[slug]/page.tsx @@ -4,7 +4,7 @@ import { PayloadRedirects } from '@/components/PayloadRedirects' import configPromise from '@payload-config' import { getPayloadHMR } from '@payloadcms/next/utilities' import { draftMode, headers } from 'next/headers' -import React from 'react' +import React, { cache } from 'react' import { homeStatic } from 'src/payload/seed/home-static' import type { Page as PageType } from '../../../payload-types' @@ -68,20 +68,16 @@ export async function generateMetadata({ params: { slug = 'home' } }): Promise { +const queryPageBySlug = cache(async ({ slug }: { slug: string }) => { const { isEnabled: draft } = draftMode() const payload = await getPayloadHMR({ config: configPromise }) - const authResult = draft ? await payload.auth({ headers: headers() }) : undefined - - const user = authResult?.user const result = await payload.find({ collection: 'pages', draft, limit: 1, - overrideAccess: false, - user, + overrideAccess: true, where: { slug: { equals: slug, @@ -90,4 +86,4 @@ const queryPageBySlug = async ({ slug }: { slug: string }) => { }) return result.docs?.[0] || null -} +}) diff --git a/templates/website/src/app/(frontend)/posts/[slug]/page.tsx b/templates/website/src/app/(frontend)/posts/[slug]/page.tsx index 3b08a12cb..62a7b02cb 100644 --- a/templates/website/src/app/(frontend)/posts/[slug]/page.tsx +++ b/templates/website/src/app/(frontend)/posts/[slug]/page.tsx @@ -5,8 +5,7 @@ import { PayloadRedirects } from '@/components/PayloadRedirects' import configPromise from '@payload-config' import { getPayloadHMR } from '@payloadcms/next/utilities' import { draftMode, headers } from 'next/headers' -import { notFound } from 'next/navigation' -import React from 'react' +import React, { cache } from 'react' import RichText from 'src/app/components/RichText' import type { Post } from '../../../../payload-types' @@ -66,20 +65,16 @@ export async function generateMetadata({ params: { slug } }): Promise return generateMeta({ doc: post }) } -const queryPostBySlug = async ({ slug }: { slug: string }) => { +const queryPostBySlug = cache(async ({ slug }: { slug: string }) => { const { isEnabled: draft } = draftMode() const payload = await getPayloadHMR({ config: configPromise }) - const authResult = draft ? await payload.auth({ headers: headers() }) : undefined - - const user = authResult?.user const result = await payload.find({ collection: 'posts', draft, limit: 1, - overrideAccess: false, - user, + overrideAccess: true, where: { slug: { equals: slug, @@ -88,4 +83,4 @@ const queryPostBySlug = async ({ slug }: { slug: string }) => { }) return result.docs?.[0] || null -} +})