45 lines
1.7 KiB
Plaintext
45 lines
1.7 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 %}
|
|
|
|
{% capture blocking_style %}rel="stylesheet" media="screen"{% endcapture %}
|
|
{% capture deferred_style %}rel="stylesheet" media="print" onload="this.media='all'"{% endcapture %}
|
|
|
|
{% liquid
|
|
if request.design_mode or html == blank
|
|
echo html
|
|
else
|
|
assign blocking_style = blocking_style | strip
|
|
assign deferred_style = deferred_style | strip
|
|
assign html = html | replace: 'media="screen"', 'media="print" onload="this.media='all'"'
|
|
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' or chunk contains 'portable-wallets' or chunk contains 'shopify.dynamic_checkout'
|
|
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
|
|
%}
|