Skip to content

Instantly share code, notes, and snippets.

@michelarteta
Last active May 8, 2022 20:52
Show Gist options
  • Save michelarteta/0f142b8d6e6bb7f57ee0769788a6c162 to your computer and use it in GitHub Desktop.
Save michelarteta/0f142b8d6e6bb7f57ee0769788a6c162 to your computer and use it in GitHub Desktop.
Boost your Shopify page loading speed 🚀with this simple approach.
function loadjscssfile(filename, filetype) {
if (filetype == "js") { //if filename is a external JavaScript file
var fileref = document.createElement('script')
fileref.setAttribute("type", "text/javascript")
fileref.setAttribute("src", filename)
} else if (filetype == "css") { //if filename is an external CSS file
var fileref = document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref != "undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
/* On Shopify you can call it like this */
loadjscssfile('{{ 'owl.carousel.min.css' | asset_url }}', 'css');
loadjscssfile('{{ 'theme-style.scss.css' | asset_url }}', 'css');
loadjscssfile('{{ 'theme-sections.scss.css' | asset_url }}', 'css');
loadjscssfile('{{ 'theme-responsive.scss.css' | asset_url }}', 'css');
loadjscssfile('{{ 'theme-config.scss.css' | asset_url }}', 'css');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment