diff --git a/.eslintignore b/.eslintignore index 22525346b..dc4cd2527 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,5 @@ gatsby/ gatsby* mdxImportGen.js -public \ No newline at end of file +public +static/scripts/ diff --git a/.gitignore b/.gitignore index 98a65f4a4..afe163c81 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ static/fonts/MatterSQItalicVF.woff static/fonts/MatterSQItalicVF.woff2 static/fonts/MatterSQVF.woff static/fonts/MatterSQVF.woff2 +static/scripts/posthog-init.js yarn.lock .vercel .env diff --git a/gatsby-node.ts b/gatsby-node.ts index c721f00c0..b9ed770fa 100644 --- a/gatsby-node.ts +++ b/gatsby-node.ts @@ -11,7 +11,7 @@ export { onPostBuild } from './gatsby/onPostBuild' export { createResolvers } from './gatsby/createResolvers' export { onPreBootstrap } from './gatsby/onPreBootstrap' -// Implement the Gatsby API “onCreatePage”. This is +// Implement the Gatsby API "onCreatePage". This is // called after every page is created. export const onCreatePage: GatsbyNode['onCreatePage'] = async ({ page, actions }) => { const { createPage, deletePage } = actions diff --git a/gatsby-ssr.js b/gatsby-ssr.js index 9957c164f..4fa221814 100644 --- a/gatsby-ssr.js +++ b/gatsby-ssr.js @@ -32,48 +32,7 @@ export const onRenderBody = function ({ setPreBodyComponents }) { setPreBodyComponents([ React.createElement('script', { key: 'dark-mode', - dangerouslySetInnerHTML: { - __html: ` -(function () { - window.__onThemeChange = function () {} - function setTheme(newTheme) { - window.__theme = newTheme - preferredTheme = newTheme - document.body.className = newTheme - window.__onThemeChange(newTheme) - } - var preferredTheme - var slug = window.location.pathname.substring(1) - var darkQuery = window.matchMedia('(prefers-color-scheme: dark)') - darkQuery.addListener(function (e) { - if (!localStorage.getItem('theme')) { - window.__setPreferredTheme('system') - } - }) - try { - preferredTheme = - localStorage.getItem('theme') || 'light' - } catch (err) {} - window.__setPreferredTheme = function (theme) { - const newTheme = theme === 'system' ? (darkQuery.matches ? 'dark' : 'light') : theme - setTheme(newTheme) - try { - localStorage.setItem('theme', newTheme) - } catch (err) {} - return newTheme - } - setTheme(preferredTheme === 'system' ? (darkQuery.matches ? 'dark' : 'light') : preferredTheme) - - // Set initial skin value - try { - const savedSkin = JSON.parse(localStorage.getItem('siteSettings') || '{}').skinMode || 'modern' - document.body.setAttribute('data-skin', savedSkin) - const savedWallpaper = JSON.parse(localStorage.getItem('siteSettings') || '{}').wallpaper || 'keyboard-garden' - document.body.setAttribute('data-wallpaper', savedWallpaper) - } catch (err) {} -})() - `, - }, + src: '/scripts/theme-init.js', }), ]) } diff --git a/gatsby/onPreBootstrap.ts b/gatsby/onPreBootstrap.ts index 3cc8054ed..45296efa2 100644 --- a/gatsby/onPreBootstrap.ts +++ b/gatsby/onPreBootstrap.ts @@ -9,6 +9,34 @@ export const PAGEVIEW_CACHE_KEY = 'onPreBootstrap@@posthog-pageviews' export const MCP_TOOLS_CACHE_KEY = 'onPreBootstrap@@mcp-tools' export const onPreBootstrap: GatsbyNode['onPreBootstrap'] = async ({ cache }) => { + if (process.env.GATSBY_POSTHOG_API_KEY && process.env.GATSBY_POSTHOG_API_HOST) { + const posthogScript = `!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n -