This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
wc-settings
dependencies can be enqueued too late
#5052
Labels
type: bug
The issue/PR concerns a confirmed bug.
wc-settings
script requires JSON encoded settings data to work. Currently, this data is enqueued when footer scripts are printed.If another script has a dependency of
wc-settings
(or one of its dependencies has a dependency ofwc-settings
as we're seeing with plugins consuming the@woocommerce/block-checkout
package), and this script is enqueued and printed in the header,wc-settings
will be printed in the header as well. By the time the footer hook is called, it's too late:https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/961c0c476d4228a218859c658c42f9b6eebfdec4/src/Assets/AssetDataRegistry.php#L63-L64
This results in console errors due to missing data.
An example of something which causes this, is using
block.json
andregisterBlockType
witheditorScript
defined. These scripts are output in the header, not the footer.To fix this:
enqueue_asset_data
must be called in the header and footer in both admin and the frontend. Ifwc-settings
is enqueued, the data must also be added inline at that point, otherwise it will be too late.enqueue_asset_data
needs to only be ran once, so there is only one inline script containing the data, and data is only added to the page once.Example: c71645c
There may be alternative approaches to this such as callbacks, or protection within
wc-settings
so it doesn't break when data is missing.The text was updated successfully, but these errors were encountered: