Last active
February 1, 2024 21:02
-
-
Save julesbravo/0ad0b88007d2c90e9a29723aea8de128 to your computer and use it in GitHub Desktop.
JS to check if a Shopify site is headless
This file contains 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
!function(){let e=0;if(window?.__remixContext?.state?.loaderData?.root?.analytics?.shopifySalesChannel==="hydrogen"){alert("Is Headless (Hydrogen)");return}!window.Shopify&&e++,document.querySelectorAll('link[href^="https://cdn.shopify.com/oxygen"]').length>0&&e++,window.__remixContext&&e++,document.querySelectorAll('script[src*="_nuxt"]').length>0&&e++,document.querySelectorAll('script[src*="_next"]').length>0&&e++,document.getElementById("___gatsby")&&e++,e>1?alert("Probably Headless"):e>0?alert("Maybe Headless"):alert("Probably Not Headless")}(); |
This file contains 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
(function() { | |
// JS code to check if a site is headless or not | |
let score = 0; | |
if(window?.__remixContext?.state?.loaderData?.root?.analytics?.shopifySalesChannel === 'hydrogen') { | |
// Definitely Hydrogen just alert and return | |
alert('Is Headless (Hydrogen)'); | |
return; | |
} | |
// Online storefront sites use window.Shopify | |
if(!window.Shopify) { | |
score++; | |
} | |
// Sites with resources that start with https://cdn.shopify.com/oxygen are usually headless | |
if(document.querySelectorAll('link[href^="https://cdn.shopify.com/oxygen"]').length > 0) { | |
score++; | |
} | |
// Sites built on remix are usually headless | |
if(window.__remixContext) { | |
score++; | |
} | |
// Sites built with nuxt are usually headless | |
if(document.querySelectorAll('script[src*="_nuxt"]').length > 0) { | |
score++; | |
} | |
// Sites built with next are usually headless | |
if(document.querySelectorAll('script[src*="_next"]').length > 0) { | |
score++; | |
} | |
// Sites built with gatsby are usually headless | |
if(document.getElementById('___gatsby')) { | |
score++; | |
} | |
if(score > 1) { | |
alert('Probably Headless'); | |
} else if(score > 0) { | |
alert('Maybe Headless'); | |
} else { | |
alert('Probably Not Headless'); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment