Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#55151 closed defect (bug) (fixed)

View scripts of blocks are loaded in editor

Reported by: ocean90's profile ocean90 Owned by: ocean90's profile ocean90
Milestone: 5.9.1 Priority: normal
Severity: normal Version: 5.9
Component: Editor Keywords: has-patch commit fixed-major dev-feedback
Focuses: javascript, performance Cc:

Description

Since WordPress 5.9 you can set a view script for a block which is supposed to be only loaded on the front end. Unfortunately it's currently also loaded in the editor which can cause unexpected behaviour and also performance issues depending on the size of the script.

As already documented on a related GitHub issue in the Gutenberg repo, the reason for this behaviour is caused by the preloading of some REST API routes via block_editor_rest_api_preload(). This also includes the posts endpoint and thus the rendered content as returned by WP_Block:render() which ultimately enqueues the script. Since the preload doesn't happen in an encapsulated process it can pollute any global state like the one for scripts and styles.

I'm proposing to backup and restore the globals like we already do for the global post object.

Change History (5)

This ticket was mentioned in PR #2312 on WordPress/wordpress-develop by ocean90.


3 years ago
#1

  • Keywords has-patch added

Related: https://github.com/WordPress/gutenberg/issues/33542

Due to the REST API preloading we may enqueue alot of assets in the editor which are only intended for the frontend. By resetting the globals for scripts/styles we can prevent this.

Trac ticket: https://core.trac.wordpress.org/ticket/55151

#2 @gziolo
3 years ago

  • Keywords commit added

#3 @ocean90
3 years ago

  • Owner set to ocean90
  • Resolution set to fixed
  • Status changed from new to closed

In 52733:

Editor: Prevent front-end assets of a block from being enqueued in the block editor.

Since WordPress 5.9 you can set a view script for a block which is supposed to be only loaded on the front end. Unfortunately it's currently also loaded in the editor which can cause unexpected behaviour and also performance issues depending on the size of the scripts.
This is caused by the preloading of REST API routes via block_editor_rest_api_preload() which doesn't happen in an encapsulated process and so does pollute any global state like the one for scripts and styles.
Similar to the global $post, core now backups the globals $wp_scripts and $wp_styles and restores the backup after the preloading.

Props gziolo, ocean90.
Fixes #55151.

#4 @ocean90
3 years ago

  • Keywords fixed-major dev-feedback added
  • Resolution fixed deleted
  • Status changed from closed to reopened

#5 @ocean90
3 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 52745:

Editor: Prevent front-end assets of a block from being enqueued in the block editor.

Since WordPress 5.9 you can set a view script for a block which is supposed to be only loaded on the front end. Unfortunately it's currently also loaded in the editor which can cause unexpected behaviour and also performance issues depending on the size of the scripts.
This is caused by the preloading of REST API routes via block_editor_rest_api_preload() which doesn't happen in an encapsulated process and so does pollute any global state like the one for scripts and styles.
Similar to the global $post, core now backups the globals $wp_scripts and $wp_styles and restores the backup after the preloading.

Props gziolo, ocean90.
Merges [52733] to the 5.9 branch.
Fixes #55151.

Note: See TracTickets for help on using tickets.