Skip to content

Commit

Permalink
Framework: Enqueue assets for rendered blocks only
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jun 2, 2020
1 parent ab878d5 commit c0babc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ public function render( $options = array() ) {
$post = $global_post;
}

if ( ! empty( $this->block_type->script ) ) {
wp_enqueue_script( $this->block_type->script );
}

if ( ! empty( $this->block_type->style ) ) {
wp_enqueue_style( $this->block_type->style );
}

/** This filter is documented in src/wp-includes/blocks.php */
return apply_filters( 'render_block', $block_content, $this->parsed_block );
}
Expand Down
9 changes: 9 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,12 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse
return $block->render();
}
add_filter( 'pre_render_block', 'gutenberg_render_block_with_assigned_block_context', 9, 2 );

/**
* Avoid enqueueing block assets of all registered blocks for all posts, instead
* deferring to block render mechanics to enqueue scripts, thereby ensuring only
* blocks of the content have their assets enqueued.
*
* @see WP_Block::render
*/
remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );

0 comments on commit c0babc4

Please sign in to comment.