Co-authored-by: Jacob Fletcher <jacobsfletch@gmail.com> Co-authored-by: Alessio Gravili <alessio@gravili.de> Co-authored-by: PatrikKozak <patrik@trbl.design> Co-authored-by: Lucas Blancas <lablancas@gmail.com> Co-authored-by: Stef Gootzen <37367280+stefgootzen@users.noreply.github.com> Co-authored-by: Jarrod Flesch <30633324+JarrodMFlesch@users.noreply.github.com> Co-authored-by: Jessica Chowdhury <67977755+JessChowdhury@users.noreply.github.com> Co-authored-by: PatrikKozak <35232443+PatrikKozak@users.noreply.github.com> Co-authored-by: Greg Willard <Wickett06@gmail.com> Co-authored-by: James Mikrut <james@payloadcms.com> Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com> Co-authored-by: Elliot DeNolf <denolfe@gmail.com> fix: WhereBuilder component does not accept all valid Where queries (#3087) fix: passes in height to resizeOptions upload option to allow height resize (#3171)
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import type { RequestCookie } from 'next/dist/compiled/@edge-runtime/cookies'
|
|
|
|
import type { Page } from '../../payload-types'
|
|
|
|
export const fetchPage = async (
|
|
slug: string,
|
|
draft?: boolean,
|
|
): Promise<Page | undefined | null> => {
|
|
let payloadToken: RequestCookie | undefined
|
|
|
|
if (draft) {
|
|
const { cookies } = await import('next/headers')
|
|
payloadToken = cookies().get('payload-token')
|
|
}
|
|
|
|
const pageRes: {
|
|
docs: Page[]
|
|
} = await fetch(
|
|
`${process.env.NEXT_PUBLIC_CMS_URL}/api/pages?where[slug][equals]=${slug}${
|
|
draft && payloadToken ? '&draft=true' : ''
|
|
}`,
|
|
{
|
|
method: 'GET',
|
|
// this is the key we'll use to on-demand revalidate pages that use this data
|
|
// we do this by calling `revalidateTag()` using the same key
|
|
// see `app/api/revalidate.ts` for more info
|
|
next: { tags: [`pages_${slug}`] },
|
|
...(draft && payloadToken
|
|
? {
|
|
headers: {
|
|
Authorization: `JWT ${payloadToken?.value}`,
|
|
},
|
|
}
|
|
: {}),
|
|
},
|
|
).then(res => res.json())
|
|
|
|
return pageRes?.docs?.[0] ?? null
|
|
}
|