Skip to content

Commit 731c4fb

Browse files
authored
fix(ui): cross-domain server-side live preview throws postMessage error (#13825)
When using server-side live preview across domains, the initial `postMessage` to the iframe throws the following error: ```txt Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://your-frontend.com') does not match the recipient window's origin ('https://your-backend.com'). ``` This error is misleading, however, as it's thrown even when the iframe's source exactly matches the post message's `targetOrigin`. For example: ```ts recipient.postMessage(message, targetOrigin) ``` The problem is that the initial message is sent before the iframe is ready to receive it, resulting in the parent window posting to itself. This is not a problem when the front-end is running on the same server, but if the recipient changes while initializing, the target origin will be mismatched. Worth noting that this is not an issue with client-side live preview. --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1211376499297320
1 parent 24ace70 commit 731c4fb

File tree

1 file changed

+5
-5
lines changed
  • packages/ui/src/elements/LivePreview/Window

1 file changed

+5
-5
lines changed

packages/ui/src/elements/LivePreview/Window/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ export const LivePreviewWindow: React.FC<EditViewProps> = (props) => {
4343
// Or it could be a separate popup window
4444
// We need to transmit data to both accordingly
4545
useEffect(() => {
46-
if (!isLivePreviewing) {
46+
if (!isLivePreviewing || !appIsReady) {
4747
return
4848
}
4949

50-
// For performance, do no reduce fields to values until after the iframe or popup has loaded
51-
if (formState && window && 'postMessage' in window && appIsReady) {
50+
// For performance, do not reduce fields to values until after the iframe or popup has loaded
51+
if (formState) {
5252
const values = reduceFieldsToValues(formState, true)
5353

5454
if (!values.id) {
@@ -95,7 +95,7 @@ export const LivePreviewWindow: React.FC<EditViewProps> = (props) => {
9595
// This is because the event will ultimately trigger a server-side roundtrip
9696
// i.e., save, save draft, autosave, etc. will fire `router.refresh()`
9797
useEffect(() => {
98-
if (!isLivePreviewing) {
98+
if (!isLivePreviewing || !appIsReady) {
9999
return
100100
}
101101

@@ -112,7 +112,7 @@ export const LivePreviewWindow: React.FC<EditViewProps> = (props) => {
112112
if (previewWindowType === 'iframe' && iframeRef.current) {
113113
iframeRef.current.contentWindow?.postMessage(message, url)
114114
}
115-
}, [mostRecentUpdate, iframeRef, popupRef, previewWindowType, url, isLivePreviewing])
115+
}, [mostRecentUpdate, iframeRef, popupRef, previewWindowType, url, isLivePreviewing, appIsReady])
116116

117117
if (previewWindowType === 'iframe') {
118118
return (

0 commit comments

Comments
 (0)