block-lab icon indicating copy to clipboard operation
block-lab copied to clipboard

Implement block specific scripts similar to stylesheets

Open cameronjonesweb opened this issue 5 years ago • 3 comments

Feature description

As defined in #249, styles can be automatically included when the block is active on a page. I think it would be nice if block scripts could be included on the front end in a similar manner.

Screenshot (optional)

image

Use case

There are plenty of types of block that could utilise front end only scripts. An accordion panel for example, or tabbed content.

Additional information

Just think this could be a good feature, considering styles are already loaded this way.

cameronjonesweb avatar Jan 21 '20 01:01 cameronjonesweb

Hi @cameronjonesweb, Thanks a lot for bringing this up, and for the details. We'll think about this 😄

kienstra avatar Jan 21 '20 02:01 kienstra

This could be a really useful idea, enqueuing JS for blocks.

My only concern might be performance. I should do another PHP profiling test to see the cost of this.

So far, it looks like the performance cost of BL enqueuing block stylesheets is high, compared to the overall performance cost of BL.

For example, the CPU time of Block_Lab\Util::locate_template() in this profile is 7.6 ms and the clock (real) time is 19.3 ms, compared to an overall performance cost of BL of about 87 ms.

locate_template

Still, more testing is needed before writing off this idea. I think enqueuing JS could be really useful.

kienstra avatar Jan 25 '20 21:01 kienstra

Hi @cameronjonesweb, Thanks a lot for suggesting this.

In the meantime, in case you didn't notice it, there's an action where you could enqueue scripts for a block:

https://github.com/getblocklab/block-lab/blob/32e71bbbfbd89802a06ba363976d76e0862119fe/php/blocks/class-loader.php#L404

Of course, that's not as useful as Block Lab doing the enqueuing automatically 😄

Have a great rest of the weekend.

kienstra avatar Jan 25 '20 21:01 kienstra