fix(live-preview): sends raw js objects through window.postMessage instead of json (#4354)

This commit is contained in:
Jacob Fletcher
2023-12-01 17:50:55 -05:00
committed by GitHub
parent fcbe5744d9
commit 03a387233d
5 changed files with 47 additions and 41 deletions

View File

@@ -19,12 +19,16 @@ export const handleMessage = async <T>(args: {
}): Promise<T> => {
const { apiRoute, depth, event, initialData, serverURL } = args
if (event.origin === serverURL && event.data) {
const eventData = JSON.parse(event?.data)
if (
event.origin === serverURL &&
event.data &&
typeof event.data === 'object' &&
event.data.type === 'payload-live-preview'
) {
const { data, externallyUpdatedRelationship, fieldSchemaJSON } = event.data
if (eventData.type === 'payload-live-preview') {
if (!payloadLivePreviewFieldSchema && eventData.fieldSchemaJSON) {
payloadLivePreviewFieldSchema = eventData.fieldSchemaJSON
if (!payloadLivePreviewFieldSchema && fieldSchemaJSON) {
payloadLivePreviewFieldSchema = fieldSchemaJSON
}
if (!payloadLivePreviewFieldSchema) {
@@ -39,9 +43,9 @@ export const handleMessage = async <T>(args: {
const mergedData = await mergeData<T>({
apiRoute,
depth,
externallyUpdatedRelationship: eventData.externallyUpdatedRelationship,
externallyUpdatedRelationship,
fieldSchema: payloadLivePreviewFieldSchema,
incomingData: eventData.data,
incomingData: data,
initialData: payloadLivePreviewPreviousData || initialData,
serverURL,
})
@@ -50,7 +54,6 @@ export const handleMessage = async <T>(args: {
return mergedData
}
}
return initialData
}

View File

@@ -8,10 +8,10 @@ export const ready = (args: { serverURL: string }): void => {
const windowToPostTo: Window = window?.opener || window?.parent
windowToPostTo?.postMessage(
JSON.stringify({
{
ready: true,
type: 'payload-live-preview',
}),
},
serverURL,
)
}

View File

@@ -125,10 +125,13 @@ export const LivePreviewProvider: React.FC<LivePreviewProviderProps> = (props) =
// Unlike iframe elements which have an `onLoad` handler, there is no way to access `window.open` on popups
useEffect(() => {
const handleMessage = (event: MessageEvent) => {
if (url?.startsWith(event.origin)) {
const data = JSON.parse(event.data)
if (data.type === 'payload-live-preview' && data.ready) {
if (
url?.startsWith(event.origin) &&
event.data &&
typeof event.data === 'object' &&
event.data.type === 'payload-live-preview'
) {
if (event.data.ready) {
setAppIsReady(true)
}
}

View File

@@ -51,12 +51,12 @@ export const LivePreview: React.FC<EditViewProps> = (props) => {
prevWindowType.current = previewWindowType
const message = JSON.stringify({
const message = {
data: values,
externallyUpdatedRelationship: mostRecentUpdate,
fieldSchemaJSON: shouldSendSchema ? fieldSchemaJSON : undefined,
type: 'payload-live-preview',
})
}
// Post message to external popup window
if (previewWindowType === 'popup' && popupRef.current) {

View File

@@ -73,13 +73,13 @@ describe('Collections - Live Preview', () => {
const handledMessage = await handleMessage({
depth: 1,
event: {
data: JSON.stringify({
data: {
data: {
title: 'Test Page (Changed)',
},
fieldSchemaJSON: schemaJSON,
type: 'payload-live-preview',
}),
},
origin: serverURL,
} as MessageEvent,
initialData: {
@@ -95,12 +95,12 @@ describe('Collections - Live Preview', () => {
const handledMessage = await handleMessage({
depth: 1,
event: {
data: JSON.stringify({
data: {
data: {
title: 'Test Page (Changed)',
},
type: 'payload-live-preview',
}),
},
origin: serverURL,
} as MessageEvent,
initialData: {