Only load block assets on frontend when block exists.
Description of the Change
Adds conditional check that the block (Tabs / Tab Items) exist on the post before enqueueing assets.
Closes #199
How to test the Change
- Create a page with the Tabs / Tab Items blocks
- Verify these load on frontend without issue
- Create a separate page without the Tabs / Tab Items blocks
- Confirm the assets
/dist/blocks/tabs-item-block.jsand/dist/blocks/tabs-block.cssare not enqueued
Changelog Entry
Fixed - Bug fix since 1.3.4 noted in #199
Credits
Props @jamesmorrison
Checklist:
- [x] I agree to follow this project's Code of Conduct.
- [ ] I have updated the documentation accordingly.
- [ ] I have added Critical Flows, Test Cases, and/or End-to-End Tests to cover my change.
- [ ] All new and existing tests pass.
@dkotter / @jeffpaul Please could you review this PR?
I am curious though, looking at the original issue, why this ends up loading the block editor script at all on the front-end? Seems like there may be a bigger issue here with how this block is built/registered that may be worth changing to fix the problem here
@fabiankaegy any insights from your perspective on this?
I am curious though, looking at the original issue, why this ends up loading the block editor script at all on the front-end? Seems like there may be a bigger issue here with how this block is built/registered that may be worth changing to fix the problem here
@fabiankaegy any insights from your perspective on this?
Yeah ideally we should enqueue the assets via the viewScript field in block.json.
@jeffpaul / @dkotter / @fabiankaegy Sorry it's taken so long for me to make the requested changes here.
I've refactored this to use the viewScript; tested and working locally.
I chose to leave the frontend.js file in case it's needed, let me know if you'd like this removed.
Please could you review the updates here?