Site Editor: custom block editor javascript assets don't load in iframe context #31022
Labels
[Feature] Site Editor
Related to the overarching Site Editor (formerly "full site editing")
[Type] Bug
An existing feature does not function as intended
Description
Where custom blocks rely on additional js script resources in the editor context, and where these scripts are dependant on being in the correct parent window context, the blocks fail to run correctly when added in the site editor context. This is because the scripts are loaded in the context of the main site editor window instead of in the editor iframe.
Step-by-step reproduction instructions
Invalid type: container must be a String or HTMLElement
error. This is because the mapblock-gl.js library is instantiated on the parent window but the map element lives in the editor iframe and in a number of places this library expects the map element to be in the parent window.Expected behaviour
Scripts enqueued with
enqueue_block_editor_assets
are aware of the current editor context and load in the iframe window if the editor is iframedActual behaviour
enqueue_block_editor_assets
always adds scripts to the parent window regardless of editor context.While it would be useful to have
enqueue_block_editor_assets
aware of the iframe context, it may not be the best/only solution for all use cases. For instance themapbox-gl.js
file mentioned above is 760Kb and it doesn't make sense to load this with every editor load if there are no map blocks on the current page. To avoid this Jetpack currently lazy loads the js into the editor only when a map block is added to the editor. It doesn't appear that the existing webpack lazyimport
that is used to achieve this can be used to load the files into the iframe because as soon as it is imported it is bound to the parent window context, and we can't see a way to have this sort of import run in the editor iframe context.There is a PoC implementation of a small library that can load an external library into the correct window context that works to get the mentioned map block running in the site editor. Maybe this is something worth exploring adding to core Gutenberg to give custom block developers a way to lazy load assets into the editor context?
The text was updated successfully, but these errors were encountered: