fix(next): adjusts args sent through live preview url

This commit is contained in:
Jacob Fletcher
2024-04-05 12:18:40 -04:00
parent 91f9973c6c
commit 1b6026304f
4 changed files with 34 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
import type { LivePreviewConfig } from 'payload/config'
import type { EditViewComponent } from 'payload/types'
import type { EditViewComponent, TypeWithID } from 'payload/types'
import React from 'react'
@@ -11,6 +11,7 @@ export const LivePreviewView: EditViewComponent = async (props) => {
const {
collectionConfig,
docID,
globalConfig,
locale,
req: {
@@ -22,8 +23,26 @@ export const LivePreviewView: EditViewComponent = async (props) => {
} = {},
} = initPageResult
// TODO(JAKE): not sure what `data` is or what it should be
const data = 'data' in props ? props.data : {}
let data: TypeWithID
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,
})
}
let livePreviewConfig: LivePreviewConfig = topLevelLivePreviewConfig
@@ -54,8 +73,9 @@ export const LivePreviewView: EditViewComponent = async (props) => {
const url =
typeof livePreviewConfig?.url === 'function'
? await livePreviewConfig.url({
collectionConfig,
data,
documentInfo: {}, // TODO: recreate this object server-side, see `useDocumentInfo`
globalConfig,
locale,
})
: livePreviewConfig?.url

View File

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

View File

@@ -1,4 +1,9 @@
export const formatLivePreviewURL = async ({ data, documentInfo }) => {
import type { LivePreviewConfig } from 'payload/config'
export const formatLivePreviewURL: LivePreviewConfig['url'] = async ({
data,
collectionConfig,
}) => {
let baseURL = 'http://localhost:3000/live-preview'
// You can run async requests here, if needed
@@ -21,6 +26,6 @@ export const formatLivePreviewURL = async ({ data, documentInfo }) => {
// 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
return `${baseURL}${
documentInfo?.slug && documentInfo.slug !== 'pages' ? `/${documentInfo.slug}` : ''
collectionConfig && collectionConfig.slug !== 'pages' ? `/${collectionConfig.slug}` : ''
}${data?.slug && data.slug !== 'home' ? `/${data.slug}` : ''}`
}

View File

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