Preventing double loading JS scripts (like React) when developing for Gutenberg
-
07-04-2021 - |
Pregunta
Gutenberg is mostly made in JS, so I'm using create-guten-block
to create some custom blocks. If multiple plugins use this to create blocks, is it a problem that these scripts get compiled and loaded for each plugin instance. Like React, being enqueued multiple times, doesn't it slow the page down?
Solución
Any WordPress-packaged scripts enqueued properly via wp_enqueue_script
won't load multiple times. If two different plugins rely on the same dependencies, thanks to the enqueue logic, those dependencies will only be loaded once.
create-guten-block
only contains javascript relevant to the custom block itself, and sets wp-blocks
, wp-i18n
, and wp-element
(the abstraction layer WP uses on top of React) as its dependencies. See /src/init.php of the generated block code.