Last active
March 25, 2025 12:17
-
-
Save savchukoleksii/a868e32aa7679a5253623a44d580a010 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template id="shopify_body_scripts"> | |
<body> | |
// your scripts | |
</body> | |
</template> | |
<script> | |
(() => { | |
const shopify_body_scripts = document?.getElementById('shopify_body_scripts'); | |
if (!shopify_body_scripts) { | |
return; | |
} | |
const innerText = shopify_body_scripts?.innerHTML?.trim() || ""; | |
if (!innerText) { | |
return; | |
} | |
if (!innerText?.length) { | |
return; | |
} | |
const body_temp = document.createElement('div'); | |
if (!body_temp) { | |
return; | |
} | |
body_temp.innerHTML = shopify_body_scripts | |
?.innerHTML | |
?.replaceAll('<body>', '') | |
?.replaceAll('<\/body>', '') || ""; | |
const assets = Array.from(body_temp.querySelectorAll('script, link, style')) || []; | |
let body_elements = body_temp.children; | |
for (let asset of assets) { | |
body_elements = [ | |
...body_elements, | |
asset.cloneNode(true), | |
]; | |
asset?.remove(); | |
} | |
if (!body_elements) { | |
return; | |
} | |
if (!body_elements?.length) { | |
return; | |
} | |
let bodyLazyElements = [] | |
// Loop through each element | |
for (let i = 0; i < body_elements.length; i++) { | |
const body_element = body_elements[i]; | |
if (!body_element) { | |
continue; | |
} | |
// Create a new element | |
const newElement = document.createElement(body_element.tagName) | |
// Get all the attributes of the element | |
const attributes = body_element?.attributes || [] | |
// Insert the innerHTML of the element | |
if (!!body_element?.textContent) { | |
newElement.innerHTML = body_element?.innerHTML || ''; | |
} | |
// Loop through each attribute | |
for (let j = 0; j < attributes.length; j++) { | |
let attribute = attributes[j]; | |
newElement.setAttribute(attribute.name, attribute.value) | |
} | |
let immediate = [ | |
newElement?.innerText?.includes('var __st = '), | |
newElement?.id === 'inveterate-theme-snippets-script', | |
newElement?.id === 'inveterate-app-embed-script', | |
newElement?.innerText?.includes('window.rebuyAppEmbedEnabled'), | |
newElement?.src?.includes('gobot'), | |
newElement?.id === 'lyvecom', | |
newElement?.src?.includes('lyve'), | |
]?.includes(true); | |
switch (newElement.tagName) { | |
case "SCRIPT": { | |
newElement.async = false; | |
newElement.defer = true; | |
{%- if template.suffix == 'ck' -%} | |
if (newElement.innerHTML?.includes('vfLoader.js')) { | |
continue; | |
} | |
if (newElement?.src?.includes('announcement-bar-essential-apps.js')) { | |
continue; | |
} | |
if (newElement?.src?.includes('cdn.rebuyengine.com')) { | |
continue; | |
} | |
{%- endif -%} | |
if (newElement?.id === 'inveterate-app-embed-log-script') { | |
continue; | |
} | |
if (!!newElement?.src?.includes("rebuy-extensions.js")) { | |
continue; | |
} | |
if (newElement.tagName === 'SCRIPT' && !immediate) { | |
bodyLazyElements.push(newElement) | |
} else { | |
document.body.appendChild(newElement) | |
} | |
} | |
break; | |
case "LINK": { | |
document.body.appendChild(newElement); | |
} | |
break; | |
case "DIV": { | |
document.body.appendChild(newElement); | |
} | |
break; | |
default: { | |
document.body.appendChild(newElement); | |
} | |
} | |
} | |
setTimeout(() => { | |
window.dispatchEvent(new Event('body-sync-scripts-loaded')); | |
document.dispatchEvent(new Event('body-sync-scripts-loaded')); | |
}, 3000); | |
let body_scripts_loaded = false | |
const lazyLoadShopifyBodyScripts = function () { | |
if (!!body_scripts_loaded) { | |
return; | |
} | |
body_scripts_loaded = true | |
bodyLazyElements?.forEach(function (element) { | |
document?.body?.appendChild(element); | |
}); | |
setTimeout(function () { | |
window.dispatchEvent(new Event('body-lazy-scripts-loaded')) | |
document.dispatchEvent(new Event('body-lazy-scripts-loaded')) | |
}, 1000); | |
document.body.classList.add('interacted'); | |
} | |
window.addEventListener('scroll', lazyLoadShopifyBodyScripts, { | |
once: true, | |
passive: false, | |
}); | |
const eventNames = [ | |
'keydown', | |
'touchstart', | |
'mouseover', | |
]; | |
if (!eventNames) { | |
return; | |
} | |
if (!eventNames?.length) { | |
return; | |
} | |
eventNames?.forEach((eventName) => { | |
document.addEventListener(eventName, lazyLoadShopifyBodyScripts, { | |
once: true, | |
passive: false, | |
}) | |
}); | |
})(); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
(() => { | |
let userInteraction = false; | |
function loadOnceOnUserInteraction(cb) { | |
if (userInteraction) { | |
return; | |
} | |
cb(); | |
userInteraction = true; | |
} | |
function registerCallbackOnUserInteraction(asyncLoad) { | |
let events = ['touchstart', 'mouseover', 'keydown']; | |
if (!events) { | |
return; | |
} | |
if (!events.length) { | |
return; | |
} | |
events.map((eventName) => { | |
document.addEventListener( | |
eventName, | |
() => { | |
loadOnceOnUserInteraction(asyncLoad); | |
}, | |
{ once: true } | |
); | |
}); | |
} | |
function PreviewBarInjectorInit(...params) { | |
if (!window.Shopify?.PreviewBarInjector) { | |
return; | |
} | |
const previewBarInjector = new window.Shopify.PreviewBarInjector(...params); | |
if (!previewBarInjector) { | |
return; | |
} | |
previewBarInjector.init(); | |
} | |
function PreviewBarInjectorLoader(...params) { | |
return new Promise((resolve) => { | |
if (window.Shopify?.PreviewBarInjector) { | |
PreviewBarInjectorInit(...params); | |
return resolve(true); | |
} | |
const PreviewBarInjectorLoaderInterval = setInterval(() => { | |
if (!window.Shopify?.PreviewBarInjector) { | |
return; | |
} | |
PreviewBarInjectorInit(...params); | |
clearInterval(PreviewBarInjectorLoaderInterval); | |
return resolve(true); | |
}, 1000); | |
}); | |
} | |
window.registerCallbackOnUserInteraction = registerCallbackOnUserInteraction; | |
window.PreviewBarInjectorLoader = PreviewBarInjectorLoader; | |
window.PreviewBarInjectorInit = PreviewBarInjectorInit; | |
})(); | |
</script> | |
{%- assign wrapped_content_for_header = wrapped_content_for_header | |
| replace: 'new Shopify.PreviewBarInjector', 'PreviewBarInjectorLoader' | |
| replace: 'previewBarInjector.init();', '' | |
| replace: "window.addEventListener('load', asyncLoad, false)", 'registerCallbackOnUserInteraction(asyncLoad)' | |
| replace: 's.src = urls[i]', 'if(!/^\?/.test(urls[i])) s.src = urls[i]' | |
-%} | |
{%- capture global_scripts_to_remove -%} | |
https:\/\/static.rechargecdn.com\/static\/js\/recharge.js?shop={{- shop.permanent_domain -}}, | |
https:\/\/static.rechargecdn.com\/assets\/js\/widget.min.js?shop={{- shop.permanent_domain -}} | |
{%- endcapture -%} | |
{%- assign global_scripts_to_remove = global_scripts_to_remove | strip_newlines | split: ',' -%} | |
{%- capture account_scripts_to_remove -%} | |
// scripts to remove from account | |
{%- endcapture -%} | |
{%- assign account_scripts_to_remove = account_scripts_to_remove | strip_newlines | split: ',' -%} | |
{%- for global_script_to_remove in global_scripts_to_remove -%} | |
{%- capture global_script_to_remove_replace -%}{{- global_script_to_remove | strip -}}{%- endcapture -%} | |
{%- assign wrapped_content_for_header = wrapped_content_for_header | |
| replace: global_script_to_remove_replace, '' | |
-%} | |
{%- endfor -%} | |
{%- if template.name == 'account' -%} | |
{%- for account_script_to_remove in account_scripts_to_remove -%} | |
{%- capture account_script_to_remove_replace -%}{{- account_script_to_remove | strip -}}{%- endcapture -%} | |
{%- assign wrapped_content_for_header = wrapped_content_for_header | |
| replace: account_script_to_remove_replace, '' | |
-%} | |
{%- endfor -%} | |
{%- endif -%} | |
<template id="shopify_header_scripts"> | |
<head> | |
// your embeded scripts | |
{{- wrapped_content_for_header -}} | |
</head> | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
(() => { | |
const shopify_header_scripts = document?.getElementById('shopify_header_scripts'); | |
if (!shopify_header_scripts) { | |
return; | |
} | |
const temp = document.createElement('div'); | |
if (!temp) { | |
return; | |
} | |
temp.innerHTML = shopify_header_scripts | |
?.innerHTML | |
?.replaceAll('<head>', '') | |
?.replaceAll('<\/head>', ''); | |
let elements = temp.children; | |
if (!elements) { | |
return; | |
} | |
if (!elements?.length) { | |
return; | |
} | |
let lazyElements = [] | |
// Loop through each element | |
for (let i = 0; i < elements.length; i++) { | |
// Create a new element | |
const newElement = document.createElement(elements[i].tagName) | |
// Get all the attributes of the element | |
const attributes = elements[i].attributes | |
// Insert the innerHTML of the element | |
if (elements[i].innerHTML) { | |
newElement.innerHTML = elements[i].innerHTML || ""; | |
} | |
// Loop through each attribute | |
for (let j = 0; j < attributes.length; j++) { | |
let attribute = attributes[j]; | |
newElement.setAttribute(attribute.name, attribute.value) | |
} | |
if (!newElement) { | |
return; | |
} | |
if (!![ | |
'title', | |
'base', | |
'meta', | |
'noscript', | |
'template', | |
'object', | |
]?.includes(newElement?.tagName?.toLowerCase())) { | |
setTimeout(() => { | |
document.head.appendChild(newElement); | |
}, 0); | |
continue; | |
} | |
let immediate = [ | |
newElement?.id === 'web-pixels-manager-setup', | |
newElement?.innerText?.includes('window.performance'), | |
newElement?.classList?.contains('analytics'), | |
newElement?.innerText?.includes('window.ShopifyAnalytics'), | |
newElement?.innerText?.includes('Shopify.shop = '), | |
newElement?.innerText?.includes('__st'), | |
newElement?.innerText?.includes('PreviewBarInjectorLoader'), | |
newElement?.innerText?.includes('Shopify.currency'), | |
newElement?.src?.includes('preview_bar_injector'), | |
newElement?.src?.includes('gobot'), | |
newElement?.id === 'inveterate-theme-snippets-script', | |
newElement?.id === 'inveterate-app-embed-script', | |
!!window?.Shopify?.designMode && !!newElement?.src?.includes('lyve'), | |
]?.includes(true); | |
switch (newElement?.tagName) { | |
case "SCRIPT": { | |
newElement.async = false; | |
newElement.defer = true; | |
if ([ | |
"https://surveys.okendo.io/js/client-setup.js", | |
"https://surveys.okendo.io/js/okendo-connect.quizzes.js", | |
newElement?.id === 'inveterate-app-embed-log-script', | |
]?.includes(newElement?.src)) { | |
continue; | |
} | |
if (!!newElement?.src?.includes("app.latest.en.")) { | |
continue; | |
} | |
if (!!newElement?.src?.includes("checkout-web")) { | |
continue; | |
} | |
if (!!newElement?.innerText?.includes("fullstory.com")) { | |
continue; | |
} | |
if (!!newElement?.src?.includes("rebuy-extensions.js")) { | |
continue; | |
} | |
{%- if template.name == "account" -%} | |
if ([ | |
"https://widget.gotolstoy.com/we/widget.js" | |
]?.includes(newElement?.src)) { | |
continue; | |
} | |
{%- endif -%} | |
{%- if template.suffix == 'ck' -%} | |
if (newElement?.innerHTML?.includes('vfLoader.js')) { | |
continue; | |
} | |
if (newElement?.src?.includes('announcement-bar-essential-apps.js')) { | |
continue; | |
} | |
if (newElement?.src?.includes('cdn.rebuyengine.com')) { | |
continue; | |
} | |
{%- endif -%} | |
if (newElement.tagName === 'SCRIPT' && !immediate) { | |
lazyElements.push(newElement) | |
} else { | |
document.head.appendChild(newElement) | |
} | |
} | |
break; | |
case "LINK": { | |
if ([ | |
"https://d3hw6dc1ow8pp2.cloudfront.net/styles/main.min.css", | |
"https://dov7r31oq5dkj.cloudfront.net/f1730d93-8231-488c-a594-020611c6304f/widget-style-customisations.css?v=a222d4ca-69e3-4c4b-97ce-74ce89bb0b4d", | |
"https://cdn.shopify.com/extensions/af12378b-8141-4d99-b456-903cc90ed97a/wishlist-hero-5/assets/default.css" | |
]?.includes(newElement?.href)) { | |
continue; | |
} | |
if (!!newElement?.href?.includes("app.latest.en.")) { | |
continue; | |
} | |
document.head.appendChild(newElement); | |
} | |
break; | |
case "DIV": { | |
console.log('newElement', newElement); | |
newElement.innerHTML = newElement.innerHTML.replaceAll('/community/membership', '/pages/membership') || ""; | |
newElement.innerHTML = newElement.innerHTML.replaceAll(`or compare all plans on`, 'or compare to our') || ""; | |
const linkEl = newElement.querySelector('a.inveterate__popup__landing_link'); | |
if (!!linkEl) { | |
linkEl.textContent = 'Annual Plan here'; | |
} | |
try { | |
document.body.appendChild(newElement); | |
} catch (error) { | |
console.log('error', error); | |
} | |
} | |
break; | |
default: { | |
document.head.appendChild(newElement); | |
} | |
} | |
} | |
setTimeout(() => { | |
window.dispatchEvent(new Event('head-sync-scripts-loaded')); | |
document.dispatchEvent(new Event('head-sync-scripts-loaded')); | |
}, 1000); | |
let loaded = false | |
const lazyLoadShopifyScripts = function () { | |
if (!!loaded) { | |
return; | |
} | |
loaded = true | |
lazyElements?.forEach(function (element) { | |
document?.head?.appendChild(element); | |
}); | |
setTimeout(function () { | |
window.dispatchEvent(new Event('head-lazy-scripts-loaded')); | |
document.dispatchEvent(new Event('head-lazy-scripts-loaded')); | |
}, 1000); | |
document.body.classList.add('interacted'); | |
} | |
window.addEventListener('scroll', lazyLoadShopifyScripts, { | |
once: true, | |
passive: false, | |
}); | |
const eventNames = [ | |
'keydown', | |
'touchstart', | |
'mouseover', | |
]; | |
if (!eventNames) { | |
return; | |
} | |
if (!eventNames?.length) { | |
return; | |
} | |
eventNames?.forEach((eventName) => { | |
document.addEventListener(eventName, lazyLoadShopifyScripts, { | |
once: true, | |
passive: false, | |
}); | |
}); | |
})(); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{%- capture wrapped_content_for_header -%} | |
{{ content_for_header }} | |
{%- endcapture -%} | |
{%- render 'async-load-head-scripts-wrapper' with wrapped_content_for_header: wrapped_content_for_header, template: template -%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment