Files
season/snippets/delay-header-scripts.liquid
T
2026-10-02 15:46:21 -06:00

39 lines
1.2 KiB
Plaintext

{% doc %}
Delays Shopify header scripts until the first interaction.
Scripts tagged nolazy, plus JSON and JSON-LD blocks, stay immediate.
@param {string} html - Markup captured from content_for_header
@example
{% capture shopify_header %}{{ content_for_header }}{% endcapture %}
{% render 'delay-header-scripts', html: shopify_header %}
{% enddoc %}
{% liquid
if request.design_mode or html == blank
echo html
else
assign chunks = html | split: '<script'
for chunk in chunks
if forloop.first
echo chunk
else
assign open_tag = chunk | split: '>' | first
if open_tag contains 'application/json' or open_tag contains 'application/ld+json' or open_tag contains 'nolazy'
echo '<script'
echo chunk
elsif open_tag == blank
echo '<script type="rocketlazyloadscript"'
echo chunk
else
assign delayed_open = open_tag | replace: 'type="module"', 'data-rocket-type="module"' | replace: "type='module'", "data-rocket-type='module'"
echo '<script type="rocketlazyloadscript"'
echo delayed_open
assign script_rest = chunk | remove_first: open_tag
echo script_rest
endif
endif
endfor
endif
%}