Repository navigation
Replies: 1 comment
|
The on-demand setting is the first thing I would isolate. WordPress documents that block styles are enqueued when that block renders if separate block assets are enabled. If wp_head has already run, a stylesheet discovered during a dynamic render can be emitted late. Temporarily remove the should_load_separate_core_block_assets filter and reload with cache disabled; if timing changes, that is the trade-off. For an early critical stylesheet, the documented wp_enqueue_block_style() API accepts both src (the URL) and path (the filesystem path); the handbook says path allows the CSS to be inlined in the head. One more clue in your sample: you manually enqueue a stylesheet and also register the block from its metadata. Inspect the page output for two different handles pointing to the same CSS, then keep one registration path so the late copy is not a duplicate. Docs: https://developer.wordpress.org/reference/functions/wp_enqueue_block_style/ and https://developer.wordpress.org/themes/features/block-stylesheets/ |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone,
I'm facing an issue with my custom Gutenberg block plugin, and I could really use your expertise.
I am developing a plugin that registers multiple blocks. All of them are dynamic blocks that use
render.php. Each block has its own CSS file, and I want the styles to load efficiently in the to avoid Flash of Unstyled Content (FOUC) and Cumulative Layout Shift (CLS). Strangely, only the CSS for blocks that are not present on the page are being correctly loaded in the<head>. The CSS for blocks that are used on the page are being appended after the footer, causing noticeable layout shifts during page load and I suspect also affect my loading speed negatively. I assume it has something to do with therender_callbackcycle or something related.Initially, I figured that WordPress might be loading CSS in the footer because it doesn’t know which blocks are used until the content is rendered. Therefore, I tried several approaches to force the CSS to load earlier:
Enqueuing CSS in wp_enqueue_scripts: I manually registered and enqueued all block styles during the wp_enqueue_scripts action to force them into the . This did not work—the styles were still appended after the footer.
Using register_block_type with style Parameter: I registered each block with its associated CSS file using the 'style' parameter in register_block_type. However, the same issue persisted—styles for blocks on the page still ended up after the footer.
Setting should_load_separate_core_block_assets Filter to true: I enabled this filter to ensure WordPress loads styles selectively, but the problem remains. It seems like the dynamic rendering of blocks might still be interfering with the timing of the CSS enqueuing.
What I Need Help With:
Is there a way to ensure the CSS for blocks present on the page is loaded in the instead of the footer, without disrupting the block rendering lifecycle?
Could this behavior be linked to how dynamic blocks or render callbacks interact with the block assets loading process?
Is there a known solution or pattern for this issue that ensures both efficient CSS loading and correct rendering of dynamic blocks?
Below is a sanitized version of my plugin’s PHP file.
Thank you very much in advance for any help or guidance. I look forward to hearing your thoughts and suggestions! 🙏🏼
All reactions