fix(next): live preview url (#5692)

This commit is contained in:
Jacob Fletcher
2024-04-05 15:00:58 -04:00
committed by GitHub
7 changed files with 62 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
import type { LivePreviewConfig } from 'payload/config' import type { LivePreviewConfig } from 'payload/config'
import type { EditViewComponent } from 'payload/types' import type { EditViewComponent, TypeWithID } from 'payload/types'
import { notFound } from 'next/navigation.js'
import React from 'react' import React from 'react'
import { LivePreviewClient } from './index.client.js' import { LivePreviewClient } from './index.client.js'
@@ -11,6 +12,7 @@ export const LivePreviewView: EditViewComponent = async (props) => {
const { const {
collectionConfig, collectionConfig,
docID,
globalConfig, globalConfig,
locale, locale,
req: { req: {
@@ -22,8 +24,30 @@ export const LivePreviewView: EditViewComponent = async (props) => {
} = {}, } = {},
} = initPageResult } = initPageResult
// TODO(JAKE): not sure what `data` is or what it should be let data: TypeWithID
const data = 'data' in props ? props.data : {}
try {
if (collectionConfig) {
data = await initPageResult.req.payload.findByID({
id: docID,
collection: collectionConfig.slug,
depth: 0,
draft: true,
fallbackLocale: null,
})
}
if (globalConfig) {
data = await initPageResult.req.payload.findGlobal({
slug: globalConfig.slug,
depth: 0,
draft: true,
fallbackLocale: null,
})
}
} catch (error) {
notFound()
}
let livePreviewConfig: LivePreviewConfig = topLevelLivePreviewConfig let livePreviewConfig: LivePreviewConfig = topLevelLivePreviewConfig
@@ -54,10 +78,11 @@ export const LivePreviewView: EditViewComponent = async (props) => {
const url = const url =
typeof livePreviewConfig?.url === 'function' typeof livePreviewConfig?.url === 'function'
? await livePreviewConfig.url({ ? await livePreviewConfig.url({
collectionConfig,
data, data,
documentInfo: {}, // TODO: recreate this object server-side, see `useDocumentInfo` globalConfig,
// @ts-expect-error
locale, locale,
payload: initPageResult.req.payload,
}) })
: livePreviewConfig?.url : livePreviewConfig?.url

View File

@@ -2,6 +2,7 @@ import type { Translations } from '@payloadcms/translations'
import type { Permissions } from '../../auth/index.js' import type { Permissions } from '../../auth/index.js'
import type { SanitizedCollectionConfig } from '../../collections/config/types.js' import type { SanitizedCollectionConfig } from '../../collections/config/types.js'
import type { Locale } from '../../config/types.js'
import type { SanitizedGlobalConfig } from '../../globals/config/types.js' import type { SanitizedGlobalConfig } from '../../globals/config/types.js'
import type { PayloadRequest } from '../../types/index.js' import type { PayloadRequest } from '../../types/index.js'

View File

@@ -63,9 +63,11 @@ export type LivePreviewConfig = {
*/ */
url?: url?:
| ((args: { | ((args: {
collectionConfig?: SanitizedCollectionConfig
data: Record<string, any> data: Record<string, any>
documentInfo: any // TODO: remove or populate this globalConfig?: SanitizedGlobalConfig
locale: Locale locale: Locale
payload: Payload
}) => Promise<string> | string) }) => Promise<string> | string)
| string | string
} }

View File

@@ -2,5 +2,5 @@ import type { Tenant } from '../payload-types.js'
export const tenant1: Omit<Tenant, 'createdAt' | 'id' | 'updatedAt'> = { export const tenant1: Omit<Tenant, 'createdAt' | 'id' | 'updatedAt'> = {
title: 'Tenant 1', title: 'Tenant 1',
clientURL: 'http://localhost:3001', clientURL: 'http://localhost:3000',
} }

View File

@@ -2,5 +2,5 @@ import type { Tenant } from '../payload-types.js'
export const tenant2: Omit<Tenant, 'createdAt' | 'id' | 'updatedAt'> = { export const tenant2: Omit<Tenant, 'createdAt' | 'id' | 'updatedAt'> = {
title: 'Tenant 2', title: 'Tenant 2',
clientURL: 'http://localhost:3002', clientURL: 'http://localhost:3001',
} }

View File

@@ -1,17 +1,34 @@
export const formatLivePreviewURL = async ({ data, documentInfo }) => { import type { LivePreviewConfig } from 'payload/config'
import type { Tenant } from '../payload-types.js'
export const formatLivePreviewURL: LivePreviewConfig['url'] = async ({
data,
collectionConfig,
payload,
}) => {
let baseURL = 'http://localhost:3000/live-preview' let baseURL = 'http://localhost:3000/live-preview'
// You can run async requests here, if needed // You can run async requests here, if needed
// For example, multi-tenant apps may need to lookup additional data // For example, multi-tenant apps may need to lookup additional data
if (data.tenant) { if (data.tenant) {
try { try {
const fullTenant = await fetch( const fullTenant = (await payload
`http://localhost:3000/api/tenants?where[id][equals]=${data.tenant}&limit=1&depth=0`, .find({
) collection: 'tenants',
.then((res) => res.json()) where: {
.then((res) => res?.docs?.[0]) id: {
equals: data.tenant,
},
},
limit: 1,
depth: 0,
})
.then((res) => res?.docs?.[0])) as Tenant
baseURL = fullTenant?.clientURL if (fullTenant?.clientURL) {
baseURL = `${fullTenant.clientURL}/live-preview`
}
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@@ -21,6 +38,6 @@ export const formatLivePreviewURL = async ({ data, documentInfo }) => {
// I.e. append '/posts' to the URL if the document is a post // I.e. append '/posts' to the URL if the document is a post
// You can also do this on individual collection or global config, if preferred // You can also do this on individual collection or global config, if preferred
return `${baseURL}${ return `${baseURL}${
documentInfo?.slug && documentInfo.slug !== 'pages' ? `/${documentInfo.slug}` : '' collectionConfig && collectionConfig.slug !== 'pages' ? `/${collectionConfig.slug}` : ''
}${data?.slug && data.slug !== 'home' ? `/${data.slug}` : ''}` }${data?.slug && data.slug !== 'home' ? `/${data.slug}` : ''}`
} }

View File

@@ -37,7 +37,7 @@
], ],
"paths": { "paths": {
"@payload-config": [ "@payload-config": [
"./test/fields/config.ts" "./test/live-preview/config.ts"
], ],
"@payloadcms/live-preview": [ "@payloadcms/live-preview": [
"./packages/live-preview/src" "./packages/live-preview/src"