Skip to content

Instantly share code, notes, and snippets.

@Jedtek
Last active January 21, 2020 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jedtek/74ec1ac47c84aca3214b4020b40c0719 to your computer and use it in GitHub Desktop.
Save Jedtek/74ec1ac47c84aca3214b4020b40c0719 to your computer and use it in GitHub Desktop.
{% comment %}
We use the content_for_header and capture it and if it contains certain JS listener for the Shopify preview bar then it will be a staff member whos logged in.
Please only include it once per page, as it takes a little performance hit to search the entire CFH capture.
Test the variable like { % if isAdmin %} and use it to re-direct or whatever you'd like. (Remove the extra space)
There is a default re-direct away to the homepage for these themes for now, this can be changed in a later version to be an argument.
- Jed V. Jan 2020 <root@livingthedream.fun>
Code it now hunts for:
<script>document.addEventListener('DOMContentLoaded', function() {
const previewBarInjector = new Shopify.PreviewBarInjector({
targetNode: document.body,
iframeRoot: 'https://www.example.com',
iframeSrc: 'https://www.example.com/preview_bar',
previewToken: '***********',
themeStoreId: '',
permanentDomain: 'example.myshopify.com',
});
previewBarInjector.init();
});</script>
DISCLAIMER:
Shopify can change their code at any time, resulting in the above not working - this should not be used long term for a page in production
and you should keep an eye to make sure the header injector hasn't changed.
{% endcomment %}
{% capture CFH %}{{ content_for_header }}{% endcapture %}
{% assign isAdmin = false %}
{% if CFH contains 'PreviewBarInjector' %}
{% assign isAdmin = true %}
{% else %}
{% comment %} Here we re-direct to homepage. {% endcomment %}
<script type="text/javascript">
window.location.replace(`https://${window.location.hostname}/404`); // Set to random string URL for 404, or just / for homepage.
</script>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment