Skip to content

Pattern creator: stop block styles leaking into the editor's outer page#749

Merged
obenland merged 1 commit into
trunkfrom
fix/pattern-creator-iframe-style-warnings
Jun 6, 2026
Merged

Pattern creator: stop block styles leaking into the editor's outer page#749
obenland merged 1 commit into
trunkfrom
fix/pattern-creator-iframe-style-warnings

Conversation

@obenland

@obenland obenland commented Jun 5, 2026

Copy link
Copy Markdown
Member

Problem

The pattern creator logs a cluster of console warnings in the editor canvas:

<handle> was added to the iframe incorrectly. Please use block.json or enqueue_block_assets to add styles to the iframe.

…for wporg-pattern-preview-style, wporg-post-status-style, wporg-copy-button-style, wporg-delete-button-style, plus dependency block styles surfaced through the same path (wporg-ratings-*, wporg-site-breadcrumbs, wporg-link-wrapper, wporg-language-suggest, jetpack-sharing-buttons, wporg-global-header-footer).

Root cause

enqueue_block_frontend_styles_for_canvas() (added in #740) hooks enqueue_block_assets and enqueues every registered block's frontend style so they're captured into the iframe canvas — which core's _wp_get_iframed_editor_assets() doesn't do for style_handles, only editor_style_handles.

But enqueue_block_assets fires twice on a creator request:

  1. During the iframe-asset capture — wanted; this is how the styles reach the canvas.
  2. On the outer creator page's normal wp_head() — unwanted. The outer shell renders no blocks, yet every block stylesheet was printed into <head>. Gutenberg's iframe onLoadgetCompatibilityStyles() then clones each head style whose id doesn't start with wp- and whose CSS contains .wp-block into the canvas, logging one warning per style. (That wp- exemption is why only wporg-*/jetpack-* handles appear, and why dependency block names showed up — our loop is what enqueued them on the outer page.)

Fix

Gate the enqueue to the capture context only. Core brackets its capture dispatch with add_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' ), so its presence distinguishes the capture from the outer render:

if ( false === has_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' ) ) {
    return;
}

Verification (wp-env)

Integration-tested both branches of the gate against a real registered block style:

Context Pre-fix Post-fix
Outer <head> (warning source) style leaks in → warning not enqueued → no warning
Iframe capture (canvas styles) enqueued ✓ still enqueued ✓

So the warnings are eliminated without regressing #740's canvas-styles fix.

Not addressed (not this repo)

The other console messages on the page originate from dependencies, not our code: the JQMIGRATE notice (jQuery Migrate), the 'root','media' canUser deprecation (@wordpress/core-data), and the __unstableUseRichText deprecation (@wordpress/block-editor).

🤖 Generated with Claude Code

enqueue_block_frontend_styles_for_canvas() (added in #740) enqueues every
registered block's frontend style on enqueue_block_assets so they are captured
into the iframe canvas. That action also fires on the outer creator page's
normal front-end render, where it printed every block stylesheet into <head>.
The outer shell renders no blocks, so those styles were pure leakage — and
Gutenberg's iframe onLoad cloned each one into the canvas, logging a
"<handle> was added to the iframe incorrectly" warning per style (the wporg-*
and jetpack-* handles seen in the console; core wp-* styles are exempted).

Gate the enqueue to the iframed-asset capture only. Core brackets that
capture's enqueue_block_assets dispatch with a
should_load_block_editor_scripts_and_styles => __return_false filter, so its
presence distinguishes the capture from the outer render.

Verified in wp-env: post-fix the style is no longer enqueued on the outer page
(no warning) but is still enqueued during the capture (canvas styles preserved,
no #740 regression).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 5, 2026 15:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents block frontend styles enqueued for the pattern creator’s iframe canvas from also being enqueued on the outer creator page, eliminating Gutenberg “added to the iframe incorrectly” console warnings caused by style leakage.

Changes:

  • Adds a runtime gate so enqueue_block_frontend_styles_for_canvas() runs only during core’s _wp_get_iframed_editor_assets() capture context.
  • Expands the function’s docblock to explain why the gating is necessary and how the capture context is detected.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@obenland obenland merged commit 37df88a into trunk Jun 6, 2026
4 checks passed
@obenland obenland deleted the fix/pattern-creator-iframe-style-warnings branch June 6, 2026 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants