[wp-trac] [WordPress Trac] #54090: block editor styles enqueued during ajax requests

WordPress Trac noreply at wordpress.org
Wed Sep 8 18:53:22 UTC 2021


#54090: block editor styles enqueued during ajax requests
-----------------------------+-----------------------------
 Reporter:  mnelson4         |      Owner:  (none)
     Type:  defect (bug)     |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Bootstrap/Load   |    Version:
 Severity:  normal           |   Keywords:
  Focuses:  javascript, css  |
-----------------------------+-----------------------------
 Block editor styles (like `wp-includes/css/dist/block-library/editor.css`
 and `admin/css/common.min.css`) are getting enqueued during WP ajax
 requests when a custom block is registered with an "editor_style" whose
 dependencies include "wp-edit-blocks".

 This is a problem because AJAX requests are usually expected to be treated
 like front-end requests, or at least NOT like a block editor page; and
 this affects plugins/themes where the themes enqueued during an AJAX
 request matter.

 For example, my plugin Print My Blog compiles a ton of posts into a single
 HTML page and temporarily writes it to the server. I send one or more AJAX
 requests to generate this HTML page (sometimes it can contain thousands of
 posts, so I can't always do it in a single request). Each request writes
 to wp-uploads temporarily. It usually works just fine. Plus, so far it
 usually enqueued the same CSS and JS as a regular frontend request. But if
 there is another plugin that registers a custom block like this...

 {{{#!php
 <?php
 wp_register_style(
     'yarpp-block-style',
     plugins_url( 'style/yarpp-block-editor.css', dirname( __FILE__ ) ),
     array( 'wp-edit-blocks' ), // <-- style depends on "wp-edit-blocks"
     $version
 );
 register_block_type(
     'yarpp/yarpp-block',
     array(
     ...
     'editor_style'    => 'yarpp-block-style', // <-- style is used for
 editing the block
     'render_callback' => array( $this, 'yarpp_block_render_callback' )
     ...
 );
 }}}
 (that example is from YARPP, but there's a similar block in WooCommerce
 for example, so it seems pretty standard)

 ...then the AJAX request gets `admin/css/common.min.css` `wp-
 includes/css/dist/block-library/editor.css` enqueued so my plugin
 generates the HTML page with a bunch of styles intended for the admin.
 This means, for example, the page suddenly has a gray background (like in
 the admin), and different font sizes, among other styles.

 It seems buggy that block editor styles and scripts are enqueued during
 AJAX requests. I understand this doesn't usually matter because they're
 usually they're not used. But in the case of my plugin, and any other
 plugin that might use the enqueued styles and scripts during a AJAX
 request, it creates a bit of a problem.

 I would expect block editor styles and scripts would only be enqueued on
 admin pages that actually use the block editor.

 Here's a short video demonstrating the issue:
 https://drive.google.com/file/d/1GbjRFlpkuVz7kZWyGiVw0bLZ3Bt1rCsv/view

 Does that make sense?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54090>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list