Created
October 20, 2023 15:38
-
-
Save closetgeekshow/3827485befd3342e01c7877db4fc551b to your computer and use it in GitHub Desktop.
_document.js file for nextjs with snipcart v3.4 installed
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
| import Document, { Html, Head, Main, NextScript } from "next/document"; | |
| import envVariables from '../envVariables' | |
| const apiKey = envVariables.apiKey; | |
| const snipcartFunction = ` | |
| (function() { | |
| var c, d; | |
| (d = (c = window.SnipcartSettings).version) != null || (c.version = '3.0'); | |
| var s, S; | |
| (S = (s = window.SnipcartSettings).timeoutDuration) != null || (s.timeoutDuration = 2750); | |
| var l, p; | |
| (p = (l = window.SnipcartSettings).domain) != null || (l.domain = 'cdn.snipcart.com'); | |
| var w, u; | |
| (u = (w = window.SnipcartSettings).protocol) != null || (w.protocol = 'https'); | |
| var m, g; | |
| (g = (m = window.SnipcartSettings).loadCSS) != null || (m.loadCSS = true); | |
| var y = | |
| window.SnipcartSettings.version.includes('v3.0.0-ci') || | |
| (window.SnipcartSettings.version !== '3.0' && | |
| window.SnipcartSettings.version.localeCompare('3.4.0', void 0, { | |
| numeric: true, | |
| sensitivity: 'base', | |
| }) === -1); | |
| var f = ['focus', 'mouseover', 'touchmove', 'scroll', 'keydown']; | |
| window.LoadSnipcart = o; | |
| document.readyState === 'loading' | |
| ? document.addEventListener('DOMContentLoaded', r) | |
| : r(); | |
| function r() { | |
| window.SnipcartSettings.loadStrategy | |
| ? window.SnipcartSettings.loadStrategy === 'on-user-interaction' && | |
| f.forEach(function (t) { | |
| return document.addEventListener(t, o); | |
| }) | |
| : o(); | |
| } | |
| var a = false; | |
| function o() { | |
| if (a) return; | |
| a = true; | |
| let t = document.getElementsByTagName('head')[0], | |
| n = document.querySelector('#snipcart'), | |
| i = document.querySelector( | |
| 'script[src^="'.concat( | |
| window.SnipcartSettings.protocol, | |
| '://' | |
| ).concat( | |
| window.SnipcartSettings.domain, | |
| '"][src$="snipcart.js"]' | |
| ) | |
| ), | |
| e = document.querySelector( | |
| 'link[href^="'.concat( | |
| window.SnipcartSettings.protocol, | |
| '://' | |
| ).concat( | |
| window.SnipcartSettings.domain, | |
| '"][href$="snipcart.css"]' | |
| ) | |
| ); | |
| n || | |
| ((n = document.createElement('div')), | |
| (n.id = 'snipcart'), | |
| n.setAttribute('hidden', 'true'), | |
| document.body.appendChild(n)), | |
| h(n), | |
| i || | |
| ((i = document.createElement('script')), | |
| (i.src = ''.concat( | |
| window.SnipcartSettings.protocol, | |
| '://' | |
| ).concat( | |
| window.SnipcartSettings.domain, | |
| '/themes/v' | |
| ).concat( | |
| window.SnipcartSettings.version, | |
| '/default/snipcart.js' | |
| )), | |
| (i.async = true), | |
| t.appendChild(i)), | |
| !e && | |
| window.SnipcartSettings.loadCSS && | |
| ((e = document.createElement('link')), | |
| (e.rel = 'stylesheet'), | |
| (e.type = 'text/css'), | |
| (e.href = ''.concat( | |
| window.SnipcartSettings.protocol, | |
| '://' | |
| ).concat( | |
| window.SnipcartSettings.domain, | |
| '/themes/v' | |
| ).concat( | |
| window.SnipcartSettings.version, | |
| '/default/snipcart.css' | |
| )), | |
| t.prepend(e)), | |
| f.forEach(function (v) { | |
| return document.removeEventListener(v, o); | |
| }); | |
| } | |
| function h(t) { | |
| !y || | |
| ((t.dataset.apiKey = window.SnipcartSettings.publicApiKey), | |
| window.SnipcartSettings.addProductBehavior && | |
| (t.dataset.configAddProductBehavior = | |
| window.SnipcartSettings.addProductBehavior), | |
| window.SnipcartSettings.modalStyle && | |
| (t.dataset.configModalStyle = window.SnipcartSettings.modalStyle), | |
| window.SnipcartSettings.currency && | |
| (t.dataset.currency = window.SnipcartSettings.currency), | |
| window.SnipcartSettings.templatesUrl && | |
| (t.dataset.templatesUrl = window.SnipcartSettings.templatesUrl)); | |
| } | |
| })(); | |
| `; | |
| class MyDocument extends Document { | |
| render () { | |
| return ( | |
| <Html lang="en"> | |
| <Head /> | |
| <body> | |
| <script | |
| dangerouslySetInnerHTML={{ | |
| __html: ` | |
| window.SnipcartSettings = { | |
| publicApiKey: '${apiKey}', | |
| loadStrategy: 'on-user-interaction', | |
| modalStyle: 'side', | |
| }; | |
| ${snipcartFunction} | |
| `, | |
| }} | |
| /> | |
| <Main /> | |
| <NextScript /> | |
| </body> | |
| </Html> | |
| ); | |
| } | |
| } | |
| export default MyDocument |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment