Repository navigation
Fix Custom HTML block preview not rendering on first load (#78576) - #78582
itzmekhokan wants to merge 7 commits into
Conversation
The isolated SandBox iframe could miss its initial resize postMessage before the parent listener was attached, leaving the preview at 0 height. Schedule delayed resize messages, listen for nested iframe loads, and re-sync the parent listener when srcDoc changes.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @nazmulasif, @jaideep-bishnoi, @doni32, @branostupak, @thewhitedrewcarey. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
A time-based solution feels rather fragile, and could still suffer from the same type of race conditions if it takes longer than 100ms for the parent window to attach its event handler. If parent window readiness timing is an issue, what if the messaging was two-way? So a parent frame could send a message to the sandbox indicating that it's ready, and the sandbox responding accordingly with the resize message. |
|
Thanks for this fix! I agree with @aduth above, syncing resizes through two-way
So
On the editor side,
The rest of the logic should work the same as before, I think. |
Replace delayed resize retries with a ready/resize handshake between the parent frame and isolated iframe, per review feedback on WordPress#78582.
Add an explicit MessageEvent type for the embedded iframe handler parameter to satisfy strict package type checking in CI.
|
Thanks @aduth and @alecgeatches for the feedback! I've replaced the delayed retry approach with a two-way postMessage handshake: Parent sends { action: 'ready' } after the resize listener is attached |
Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
|
@alecgeatches @aduth Would y'all be able to take a look at the updated approach and make sure it's inline with what you were thinking? @itzmekhokan can you take a look at the failing check and update the changelog accordingly? |
@aaronjorbin I've updated the changelog. |
aduth
left a comment
There was a problem hiding this comment.
Thanks for updating the approach, I think this is looking like the right direction now. I left some review comments for you to consider.
|
|
||
| const observer = new MutationObserver( function () { | ||
| sendResize(); | ||
| attachIframeLoadListeners(); |
There was a problem hiding this comment.
This is going to continuously add event handlers any time there's a DOM mutation, which is a big memory and could become quite bad for performance. I would expect we'd only need to add listeners once.
There was a problem hiding this comment.
Good catch — that would keep re-scanning and re-binding on every mutation. I added a WeakSet so each nested iframe gets its load listener exactly once. I kept the call inside the MutationObserver (rather than only running it once at init) because embeds can inject iframes after load, and the WeakSet ensures we only wire up newly-added ones.
| if ( ! data ) { | ||
| return null; | ||
| } | ||
|
|
||
| let message = data; | ||
|
|
||
| if ( 'string' === typeof message ) { | ||
| try { | ||
| message = JSON.parse( message ); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| if ( | ||
| 'object' !== typeof message || | ||
| null === message || | ||
| ! ( 'action' in message ) || | ||
| 'string' !== typeof ( message as SandBoxMessage ).action | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| return message as SandBoxMessage; |
There was a problem hiding this comment.
Maybe we could just rely on the try block to catch any the validation issues we're expecting, to simplify this?
Something more like:
| if ( ! data ) { | |
| return null; | |
| } | |
| let message = data; | |
| if ( 'string' === typeof message ) { | |
| try { | |
| message = JSON.parse( message ); | |
| } catch { | |
| return null; | |
| } | |
| } | |
| if ( | |
| 'object' !== typeof message || | |
| null === message || | |
| ! ( 'action' in message ) || | |
| 'string' !== typeof ( message as SandBoxMessage ).action | |
| ) { | |
| return null; | |
| } | |
| return message as SandBoxMessage; | |
| try { | |
| const message = JSON.parse( data ); | |
| if ( typeof ( message as SandBoxMessage ).action === 'string' ) { | |
| return message as SandBoxMessage; | |
| } | |
| } catch {} | |
| return null; |
There was a problem hiding this comment.
Simplified to lean on the try block as suggested. One caveat: our own sandbox iframe posts the resize message as an object ({ action: 'resize', … }) rather than a JSON string, so a JSON.parse-only version would reject our own messages. I kept a typeof data === 'string' ? JSON.parse( data ) : data branch inside the try so both the object path and the string path (some embed providers) still work, and added a comment noting why.
There was a problem hiding this comment.
I kept a typeof data === 'string' ? JSON.parse( data ) : data branch inside the try so both the object path and the string path (some embed providers) still work, and added a comment noting why.
But if we're inside the try block and we only care to handle the object, then should we just skip the string parse flow altogether?
Something like...
try {
if ( message.action === 'ready' ) {
return message as SandboxMessage;
}
} catch {}- Simplify parseSandBoxMessage to lean on the try block. - Collapse the ready-message handling in the iframe to a single object path. - Use NodeList.forEach and a WeakSet so nested iframe load listeners are attached only once instead of on every DOM mutation. - Revert useLayoutEffect back to useEffect; the postMessage handshake already resolves the race regardless of effect timing. - Replace the source-string-matching test with behavioral tests that verify the parent resizes on a resize message and ignores foreign sources.
|
I was having some difficulty trying to test this. Specifically, the testing instructions work as intended on It also has me wondering: Is the "two-way messaging" actually a necessary part of the fix? Or is the issue only due to how nested embeds may load dynamically and we need to send a second message after the initial resize when those nested frames finish loading? The latter would be a simpler fix if we could limit it to that. The branch could also use a rebase/refresh at this point to resolve the conflicts. Apologies for the delays in getting feedback here since the pull request was opened. |
Not something to address here, but this feels like a rather unfortunate side-effect of #77212 (cc @alecgeatches). It would be worth looking into some of these incompatibilities to see if there's any options to improve support without regressing on the hardened security introduced there. |
|
== Test Report === Environment
=== Steps taken
=== Expected result
=== Additional Notes
=== Screenshots/Screencast with results
=== Support Content
|
|
== Patch Test Report === Environment
=== Steps taken
=== Expected result
=== Actual Results
=== Additional Notes
=== Screenshots/Screencast with results
=== Support Content
|
|
Thanks for working on this, @itzmekhokan! On trunk, the Custom HTML block no longer renders its content in a |

What?
Fixes a race condition in
IsolatedSandBoxthat caused the Custom HTML block preview iframe to remain at zero height after adding or reloading content in the editor.Fixes #78576.
Why?
After WordPress 7.0, the Custom HTML block preview uses
IsolatedSandBox(via #77212) instead of a same-origin sandbox. The preview iframe relies onpostMessageresize events to set its height. On first mount or when reopening a post, the initial resize message could be sent before the parent window's message listener was attached, leaving the iframe atheight={0}and making the block appear blank in the editor.How?
srcdocloads synchronously.IsolatedSandBoxtouseLayoutEffectand re-sync the message listener wheneversrcDocchanges.Testing Instructions
<p>Hello World</p>, click Update.<iframe src="https://example.com" width="400" height="300"></iframe>).allow-same-originfor core/html blocks #77212 — this is expected and separate from this fix).Regression checks
allow-same-originfor core/html blocks #77212 is preserved (scripts cannot access parent cookies).Test plan
npm run test:unit -- --testPathPattern="packages/components/src/sandbox/test"passesnpm run test:e2e -- test/e2e/specs/editor/blocks/html.spec.jspassesUse of AI Tools
AI assistance: Yes
Tool(s): Claude