Skip to content

Commit 0b88466

Browse files
authored
fix(next): prevent live preview url functions from firing unnecessarily (#13088)
Ensures Live Preview url functions aren't fired during create or on collections that do not have Live Preview enabled. --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1210743577153852
1 parent fee33b5 commit 0b88466

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -329,27 +329,36 @@ export const renderDocument = async ({
329329
viewType,
330330
}
331331

332+
const isLivePreviewEnabled = Boolean(
333+
config.admin?.livePreview?.collections?.includes(collectionSlug) ||
334+
config.admin?.livePreview?.globals?.includes(globalSlug) ||
335+
collectionConfig?.admin?.livePreview ||
336+
globalConfig?.admin?.livePreview,
337+
)
338+
332339
const livePreviewConfig: LivePreviewConfig = {
333-
...(config.admin.livePreview || {}),
340+
...(isLivePreviewEnabled ? config.admin.livePreview : {}),
334341
...(collectionConfig?.admin?.livePreview || {}),
335342
...(globalConfig?.admin?.livePreview || {}),
336343
}
337344

338345
const livePreviewURL =
339-
typeof livePreviewConfig?.url === 'function'
340-
? await livePreviewConfig.url({
341-
collectionConfig,
342-
data: doc,
343-
globalConfig,
344-
locale,
345-
req,
346-
/**
347-
* @deprecated
348-
* Use `req.payload` instead. This will be removed in the next major version.
349-
*/
350-
payload: initPageResult.req.payload,
351-
})
352-
: livePreviewConfig?.url
346+
operation !== 'create'
347+
? typeof livePreviewConfig?.url === 'function'
348+
? await livePreviewConfig.url({
349+
collectionConfig,
350+
data: doc,
351+
globalConfig,
352+
locale,
353+
req,
354+
/**
355+
* @deprecated
356+
* Use `req.payload` instead. This will be removed in the next major version.
357+
*/
358+
payload: initPageResult.req.payload,
359+
})
360+
: livePreviewConfig?.url
361+
: ''
353362

354363
return {
355364
data: doc,
@@ -380,8 +389,8 @@ export const renderDocument = async ({
380389
>
381390
<LivePreviewProvider
382391
breakpoints={livePreviewConfig?.breakpoints}
392+
isLivePreviewEnabled={isLivePreviewEnabled && operation !== 'create'}
383393
isLivePreviewing={entityPreferences?.value?.editViewType === 'live-preview'}
384-
operation={operation}
385394
url={livePreviewURL}
386395
>
387396
{showHeader && !drawerSlug && (

packages/ui/src/providers/LivePreview/index.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export type LivePreviewProviderProps = {
2121
height: number
2222
width: number
2323
}
24+
isLivePreviewEnabled?: boolean
2425
isLivePreviewing: boolean
25-
operation?: 'create' | 'update'
2626
url: string
2727
}
2828

@@ -37,8 +37,8 @@ const getAbsoluteUrl = (url) => {
3737
export const LivePreviewProvider: React.FC<LivePreviewProviderProps> = ({
3838
breakpoints: incomingBreakpoints,
3939
children,
40+
isLivePreviewEnabled,
4041
isLivePreviewing: incomingIsLivePreviewing,
41-
operation,
4242
url: incomingUrl,
4343
}) => {
4444
const [previewWindowType, setPreviewWindowType] = useState<'iframe' | 'popup'>('iframe')
@@ -226,13 +226,6 @@ export const LivePreviewProvider: React.FC<LivePreviewProviderProps> = ({
226226
)
227227
}, [isLivePreviewing, setPreference, collectionSlug, globalSlug])
228228

229-
const isLivePreviewEnabled = Boolean(
230-
operation !== 'create' &&
231-
((collectionSlug && config?.admin?.livePreview?.collections?.includes(collectionSlug)) ||
232-
(globalSlug && config.admin?.livePreview?.globals?.includes(globalSlug)) ||
233-
entityConfig?.admin?.livePreview),
234-
)
235-
236229
return (
237230
<LivePreviewContext
238231
value={{

0 commit comments

Comments
 (0)