Skip to content
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

Closed
mikejolley opened this issue Nov 2, 2021 · 3 comments
Closed

wc-settings dependencies can be enqueued too late #5052

mikejolley opened this issue Nov 2, 2021 · 3 comments
Labels
type: bug The issue/PR concerns a confirmed bug.

Comments

@mikejolley
Copy link
Member

mikejolley commented Nov 2, 2021

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 of wc-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 and registerBlockType with editorScript 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. If wc-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.

@mikejolley mikejolley added the type: bug The issue/PR concerns a confirmed bug. label Nov 2, 2021
@mikejolley
Copy link
Member Author

mikejolley commented Nov 2, 2021

@mikejolley
Copy link
Member Author

If you use "script" to register the frontend script of a block automatically, this also breaks wc-settings on the frontend. See here the scripts are loaded in the header:

https://github.com/WordPress/WordPress/blob/f57070bfd1a7f0b2d437678ae823c3329f83c264/wp-includes/blocks.php#L109-L114

The solution I tried doesn't work for this case because blocks register data on render. So that happens too late.

We'd need a callback or something to pass this data to the settings script for it to work, so it can accept data later on.

@mikejolley
Copy link
Member Author

Closed by #5059

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug The issue/PR concerns a confirmed bug.
Projects
None yet
Development

No branches or pull requests

1 participant