From afa08d0ebf20916f96026f0fcd04603568252be3 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 11 Dec 2024 14:11:24 -0600 Subject: [PATCH] templates: fix live preview relative URLs on website template (#9906) --- templates/website/src/collections/Pages/index.ts | 3 ++- templates/website/src/collections/Posts/index.ts | 4 ++-- templates/website/src/utilities/generatePreviewPath.ts | 9 ++------- .../with-vercel-website/src/collections/Pages/index.ts | 3 ++- .../with-vercel-website/src/collections/Posts/index.ts | 4 ++-- .../src/utilities/generatePreviewPath.ts | 9 ++------- 6 files changed, 12 insertions(+), 20 deletions(-) diff --git a/templates/website/src/collections/Pages/index.ts b/templates/website/src/collections/Pages/index.ts index 079bba267d9..c799b966845 100644 --- a/templates/website/src/collections/Pages/index.ts +++ b/templates/website/src/collections/Pages/index.ts @@ -39,10 +39,11 @@ export const Pages: CollectionConfig<'pages'> = { admin: { defaultColumns: ['title', 'slug', 'updatedAt'], livePreview: { - url: ({ data }) => { + url: ({ data, req }) => { const path = generatePreviewPath({ slug: typeof data?.slug === 'string' ? data.slug : '', collection: 'pages', + req, }) return path diff --git a/templates/website/src/collections/Posts/index.ts b/templates/website/src/collections/Posts/index.ts index 7d0567ae2f0..5053bc188d6 100644 --- a/templates/website/src/collections/Posts/index.ts +++ b/templates/website/src/collections/Posts/index.ts @@ -50,11 +50,11 @@ export const Posts: CollectionConfig<'posts'> = { admin: { defaultColumns: ['title', 'slug', 'updatedAt'], livePreview: { - url: ({ data }) => { + url: ({ data, req }) => { const path = generatePreviewPath({ slug: typeof data?.slug === 'string' ? data.slug : '', collection: 'posts', - // req, TODO: thread `req` once 3.5.1 is out, see notes in `generatePreviewPath` + req, }) return path diff --git a/templates/website/src/utilities/generatePreviewPath.ts b/templates/website/src/utilities/generatePreviewPath.ts index 8e1b5a2353c..5179f6889ab 100644 --- a/templates/website/src/utilities/generatePreviewPath.ts +++ b/templates/website/src/utilities/generatePreviewPath.ts @@ -8,7 +8,7 @@ const collectionPrefixMap: Partial> = { type Props = { collection: keyof typeof collectionPrefixMap slug: string - req?: PayloadRequest // TODO: make this required once 3.5.1 is out, it's a new argument in that version + req: PayloadRequest } export const generatePreviewPath = ({ collection, slug, req }: Props) => { @@ -26,12 +26,7 @@ export const generatePreviewPath = ({ collection, slug, req }: Props) => { encodedParams.append(key, value) }) - let url = `/next/preview?${encodedParams.toString()}` - - // TODO: remove this check once 3.5.1 is out, see note above - if (req) { - url = `${req.protocol}//${req.host}${url}` - } + let url = `${req.protocol}//${req.host}/next/preview?${encodedParams.toString()}` return url } diff --git a/templates/with-vercel-website/src/collections/Pages/index.ts b/templates/with-vercel-website/src/collections/Pages/index.ts index 079bba267d9..c799b966845 100644 --- a/templates/with-vercel-website/src/collections/Pages/index.ts +++ b/templates/with-vercel-website/src/collections/Pages/index.ts @@ -39,10 +39,11 @@ export const Pages: CollectionConfig<'pages'> = { admin: { defaultColumns: ['title', 'slug', 'updatedAt'], livePreview: { - url: ({ data }) => { + url: ({ data, req }) => { const path = generatePreviewPath({ slug: typeof data?.slug === 'string' ? data.slug : '', collection: 'pages', + req, }) return path diff --git a/templates/with-vercel-website/src/collections/Posts/index.ts b/templates/with-vercel-website/src/collections/Posts/index.ts index 7d0567ae2f0..5053bc188d6 100644 --- a/templates/with-vercel-website/src/collections/Posts/index.ts +++ b/templates/with-vercel-website/src/collections/Posts/index.ts @@ -50,11 +50,11 @@ export const Posts: CollectionConfig<'posts'> = { admin: { defaultColumns: ['title', 'slug', 'updatedAt'], livePreview: { - url: ({ data }) => { + url: ({ data, req }) => { const path = generatePreviewPath({ slug: typeof data?.slug === 'string' ? data.slug : '', collection: 'posts', - // req, TODO: thread `req` once 3.5.1 is out, see notes in `generatePreviewPath` + req, }) return path diff --git a/templates/with-vercel-website/src/utilities/generatePreviewPath.ts b/templates/with-vercel-website/src/utilities/generatePreviewPath.ts index 8e1b5a2353c..5179f6889ab 100644 --- a/templates/with-vercel-website/src/utilities/generatePreviewPath.ts +++ b/templates/with-vercel-website/src/utilities/generatePreviewPath.ts @@ -8,7 +8,7 @@ const collectionPrefixMap: Partial> = { type Props = { collection: keyof typeof collectionPrefixMap slug: string - req?: PayloadRequest // TODO: make this required once 3.5.1 is out, it's a new argument in that version + req: PayloadRequest } export const generatePreviewPath = ({ collection, slug, req }: Props) => { @@ -26,12 +26,7 @@ export const generatePreviewPath = ({ collection, slug, req }: Props) => { encodedParams.append(key, value) }) - let url = `/next/preview?${encodedParams.toString()}` - - // TODO: remove this check once 3.5.1 is out, see note above - if (req) { - url = `${req.protocol}//${req.host}${url}` - } + let url = `${req.protocol}//${req.host}/next/preview?${encodedParams.toString()}` return url }