reduce javascript execution time shopify
Topic summary
A user seeks help reducing JavaScript execution time on their Shopify store, sharing a performance screenshot that indicates issues.
Proposed Solution:
Another participant provides a Liquid code snippet that implements lazy-loading for third-party scripts. The approach includes:
- Script Removal: Uses Liquid to strip specific URLs from
content_for_header - Deferred Injection: Delays script loading until user interaction (click, scroll, or keydown events)
- Implementation: Requires adding target URLs to the
urls_to_removearray
Note: The code snippet appears partially corrupted or reversed in the second half, which may affect implementation. The technique aims to improve initial page load performance by deferring non-critical JavaScript execution until users actively engage with the page.
Status: Solution proposed but not yet confirmed as tested or effective.
{% comment %} remove URLs from asyncLoad {% endcomment %}
{% assign urls_to_remove = ’
https:exampleUrl1.com,
https:exampleUrl2.com,
https:exampleUrl3.com,
’
| strip_newlines
| replace: ’ ', ‘’
| split: ‘,’
%}
{% for url in urls_to_remove %}
{% assign content_for_header = content_for_header | remove: url %}
{% endfor %}
{{ content_for_header }}
{% comment %} inject app scripts after user interaction {% endcomment %}
you can use this script and modify it.
