publisher-media-kit icon indicating copy to clipboard operation
publisher-media-kit copied to clipboard

Only load block assets on frontend when block exists.

Open jamesmorrison opened this issue 4 months ago • 4 comments

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

  1. Create a page with the Tabs / Tab Items blocks
  2. Verify these load on frontend without issue
  3. Create a separate page without the Tabs / Tab Items blocks
  4. Confirm the assets /dist/blocks/tabs-item-block.js and /dist/blocks/tabs-block.css are not enqueued

Changelog Entry

Fixed - Bug fix since 1.3.4 noted in #199

Credits

Props @jamesmorrison

Checklist:

jamesmorrison avatar Aug 21 '25 13:08 jamesmorrison

@dkotter / @jeffpaul Please could you review this PR?

jamesmorrison avatar Aug 21 '25 13:08 jamesmorrison

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?

jeffpaul avatar Sep 08 '25 20:09 jeffpaul

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.

fabiankaegy avatar Sep 09 '25 06:09 fabiankaegy

@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?

jamesmorrison avatar Nov 20 '25 12:11 jamesmorrison