Skip to content

Commit afa08d0

Browse files
authored
templates: fix live preview relative URLs on website template (#9906)
1 parent d97d7ed commit afa08d0

File tree

6 files changed

+12
-20
lines changed

6 files changed

+12
-20
lines changed

templates/website/src/collections/Pages/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ export const Pages: CollectionConfig<'pages'> = {
3939
admin: {
4040
defaultColumns: ['title', 'slug', 'updatedAt'],
4141
livePreview: {
42-
url: ({ data }) => {
42+
url: ({ data, req }) => {
4343
const path = generatePreviewPath({
4444
slug: typeof data?.slug === 'string' ? data.slug : '',
4545
collection: 'pages',
46+
req,
4647
})
4748

4849
return path

templates/website/src/collections/Posts/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ export const Posts: CollectionConfig<'posts'> = {
5050
admin: {
5151
defaultColumns: ['title', 'slug', 'updatedAt'],
5252
livePreview: {
53-
url: ({ data }) => {
53+
url: ({ data, req }) => {
5454
const path = generatePreviewPath({
5555
slug: typeof data?.slug === 'string' ? data.slug : '',
5656
collection: 'posts',
57-
// req, TODO: thread `req` once 3.5.1 is out, see notes in `generatePreviewPath`
57+
req,
5858
})
5959

6060
return path

templates/website/src/utilities/generatePreviewPath.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const collectionPrefixMap: Partial<Record<CollectionSlug, string>> = {
88
type Props = {
99
collection: keyof typeof collectionPrefixMap
1010
slug: string
11-
req?: PayloadRequest // TODO: make this required once 3.5.1 is out, it's a new argument in that version
11+
req: PayloadRequest
1212
}
1313

1414
export const generatePreviewPath = ({ collection, slug, req }: Props) => {
@@ -26,12 +26,7 @@ export const generatePreviewPath = ({ collection, slug, req }: Props) => {
2626
encodedParams.append(key, value)
2727
})
2828

29-
let url = `/next/preview?${encodedParams.toString()}`
30-
31-
// TODO: remove this check once 3.5.1 is out, see note above
32-
if (req) {
33-
url = `${req.protocol}//${req.host}${url}`
34-
}
29+
let url = `${req.protocol}//${req.host}/next/preview?${encodedParams.toString()}`
3530

3631
return url
3732
}

templates/with-vercel-website/src/collections/Pages/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ export const Pages: CollectionConfig<'pages'> = {
3939
admin: {
4040
defaultColumns: ['title', 'slug', 'updatedAt'],
4141
livePreview: {
42-
url: ({ data }) => {
42+
url: ({ data, req }) => {
4343
const path = generatePreviewPath({
4444
slug: typeof data?.slug === 'string' ? data.slug : '',
4545
collection: 'pages',
46+
req,
4647
})
4748

4849
return path

templates/with-vercel-website/src/collections/Posts/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ export const Posts: CollectionConfig<'posts'> = {
5050
admin: {
5151
defaultColumns: ['title', 'slug', 'updatedAt'],
5252
livePreview: {
53-
url: ({ data }) => {
53+
url: ({ data, req }) => {
5454
const path = generatePreviewPath({
5555
slug: typeof data?.slug === 'string' ? data.slug : '',
5656
collection: 'posts',
57-
// req, TODO: thread `req` once 3.5.1 is out, see notes in `generatePreviewPath`
57+
req,
5858
})
5959

6060
return path

templates/with-vercel-website/src/utilities/generatePreviewPath.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const collectionPrefixMap: Partial<Record<CollectionSlug, string>> = {
88
type Props = {
99
collection: keyof typeof collectionPrefixMap
1010
slug: string
11-
req?: PayloadRequest // TODO: make this required once 3.5.1 is out, it's a new argument in that version
11+
req: PayloadRequest
1212
}
1313

1414
export const generatePreviewPath = ({ collection, slug, req }: Props) => {
@@ -26,12 +26,7 @@ export const generatePreviewPath = ({ collection, slug, req }: Props) => {
2626
encodedParams.append(key, value)
2727
})
2828

29-
let url = `/next/preview?${encodedParams.toString()}`
30-
31-
// TODO: remove this check once 3.5.1 is out, see note above
32-
if (req) {
33-
url = `${req.protocol}//${req.host}${url}`
34-
}
29+
let url = `${req.protocol}//${req.host}/next/preview?${encodedParams.toString()}`
3530

3631
return url
3732
}

0 commit comments

Comments
 (0)