Skip to content

Commit

Permalink
templates: fix live preview relative URLs on website template (#9906)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpopus authored Dec 11, 2024
1 parent d97d7ed commit afa08d0
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 20 deletions.
3 changes: 2 additions & 1 deletion templates/website/src/collections/Pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions templates/website/src/collections/Posts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions templates/website/src/utilities/generatePreviewPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const collectionPrefixMap: Partial<Record<CollectionSlug, string>> = {
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) => {
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion templates/with-vercel-website/src/collections/Pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions templates/with-vercel-website/src/collections/Posts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const collectionPrefixMap: Partial<Record<CollectionSlug, string>> = {
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) => {
Expand All @@ -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
}

0 comments on commit afa08d0

Please sign in to comment.