Skip to content

Commit ec95ce8

Browse files
authored
fix(next): passes doc through edit view handler (#9302)
The Edit and Live Preview views were duplicately making the same Local API requests for document data. This is because while the top-level document view handler makes these requests _before_ rendering the Live Preview view, it wasn't passing it's data through as props. This has also led to inconsistencies in the options being passed through the requests themselves, such as `locale`, `user`, and `overrideAccess: false`. Everything is now standardized as expected through the existing `getDocumentData` utility.
1 parent 54ac8b9 commit ec95ce8

File tree

4 files changed

+16
-58
lines changed

4 files changed

+16
-58
lines changed

docs/admin/views.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ export const MyCollectionConfig: SanitizedCollectionConfig = {
350350
}
351351
```
352352

353+
### Default Props
354+
353355
Your Custom Views will be provided with the following props:
354356

355357
| Prop | Description |
@@ -359,6 +361,7 @@ Your Custom Views will be provided with the following props:
359361
| **`importMap`** | The import map object. |
360362
| **`params`** | An object containing the [Dynamic Route Parameters](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes). |
361363
| **`searchParams`** | An object containing the [Search Parameters](https://developer.mozilla.org/docs/Learn/Common_questions/What_is_a_URL#parameters). |
364+
| **`doc`** | The document being edited. Only available in Document Views. [More details](#document-views). |
362365

363366
<Banner type="success">
364367
<strong>Reminder:</strong>

packages/next/src/views/Document/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ export const renderDocument = async ({
159159
}),
160160
])
161161

162-
const serverProps: ServerProps = {
162+
const serverProps: ServerSideEditViewProps = {
163+
doc,
163164
i18n,
164165
initPageResult,
165166
locale,
Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,16 @@
1-
import type {
2-
EditViewComponent,
3-
LivePreviewConfig,
4-
PayloadServerReactComponent,
5-
TypeWithID,
6-
} from 'payload'
1+
import type { EditViewComponent, LivePreviewConfig, PayloadServerReactComponent } from 'payload'
72

8-
import { notFound } from 'next/navigation.js'
93
import React from 'react'
104

11-
import { LivePreviewClient } from './index.client.js'
125
import './index.scss'
6+
import { LivePreviewClient } from './index.client.js'
137

148
export const LivePreviewView: PayloadServerReactComponent<EditViewComponent> = async (props) => {
15-
const { initPageResult } = props
16-
17-
const {
18-
collectionConfig,
19-
docID,
20-
globalConfig,
21-
locale,
22-
req: {
23-
payload: {
24-
config: {
25-
admin: { livePreview: topLevelLivePreviewConfig },
26-
},
27-
} = {},
28-
} = {},
29-
} = initPageResult
30-
31-
let data: Record<string, unknown> | TypeWithID
9+
const { doc, initPageResult } = props
3210

33-
try {
34-
if (collectionConfig) {
35-
data = await initPageResult.req.payload.findByID({
36-
id: docID,
37-
collection: collectionConfig.slug,
38-
depth: 0,
39-
draft: true,
40-
fallbackLocale: false,
41-
locale: locale?.code,
42-
})
43-
}
44-
45-
if (globalConfig) {
46-
data = await initPageResult.req.payload.findGlobal({
47-
slug: globalConfig.slug,
48-
depth: 0,
49-
draft: true,
50-
fallbackLocale: false,
51-
locale: locale?.code,
52-
})
53-
}
54-
} catch (error) {
55-
notFound()
56-
}
11+
const { collectionConfig, globalConfig, locale, req } = initPageResult
5712

58-
let livePreviewConfig: LivePreviewConfig = topLevelLivePreviewConfig
13+
let livePreviewConfig: LivePreviewConfig = req.payload.config?.admin?.livePreview
5914

6015
if (collectionConfig) {
6116
livePreviewConfig = {
@@ -85,12 +40,12 @@ export const LivePreviewView: PayloadServerReactComponent<EditViewComponent> = a
8540
typeof livePreviewConfig?.url === 'function'
8641
? await livePreviewConfig.url({
8742
collectionConfig,
88-
data,
43+
data: doc,
8944
globalConfig,
9045
locale,
9146
payload: initPageResult.req.payload,
9247
})
9348
: livePreviewConfig?.url
9449

95-
return <LivePreviewClient breakpoints={breakpoints} initialData={data} url={url} />
50+
return <LivePreviewClient breakpoints={breakpoints} initialData={doc} url={url} />
9651
}

packages/payload/src/admin/views/types.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { SanitizedPermissions } from '../../auth/index.js'
44
import type { ImportMap } from '../../bin/generateImportMap/index.js'
55
import type { SanitizedCollectionConfig } from '../../collections/config/types.js'
66
import type { ClientConfig } from '../../config/client.js'
7-
import type { Locale, MetaConfig, PayloadComponent } from '../../config/types.js'
7+
import type { Locale, MetaConfig, PayloadComponent, ServerProps } from '../../config/types.js'
88
import type { SanitizedGlobalConfig } from '../../globals/config/types.js'
99
import type { PayloadRequest } from '../../types/index.js'
1010
import type { LanguageOptions } from '../LanguageOptions.js'
@@ -60,11 +60,10 @@ export type InitPageResult = {
6060
}
6161

6262
export type ServerSideEditViewProps = {
63+
readonly doc: Data
6364
readonly initPageResult: InitPageResult
64-
readonly params: { [key: string]: string | string[] | undefined }
65-
readonly payloadServerAction: PayloadServerAction
6665
readonly routeSegments: string[]
67-
readonly searchParams: { [key: string]: string | string[] | undefined }
68-
} & ClientSideEditViewProps
66+
} & ClientSideEditViewProps &
67+
ServerProps
6968

7069
export type ClientSideEditViewProps = {} & DocumentSlots

0 commit comments

Comments
 (0)