You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using Gatsby in combination with prism-react-renderer (I also posted this issue on the prism-react-rendered GitHub) and I've come across an issue which is stumping me. I'm not sure if this is a Gatsby or prism-react-renderer issues...I am attempting to avoid FOUC in prism-react-renderer when implementing a dark / light mode by injecting the dark class into my HTML before rendering the page via gatsby-ssr.js, like so:
While this all works fine for the page theme itself, the issues crop up with my CodeBlock rendering--as you can see in the code below, when I set the default theme I first check whether we have a window -- if we do, I check whether the document.documentElement.classList contains our dark theme--if it does, we set this as the default theme. If our window is undefined, such as during the SSR, we simply return the light theme. This code works perfectly in development mode, and even appears to work perfectly when being served as a static bundle, unfortunately the Highlight component renders the wrong theme even when receiving the correct props. Please see below:
As you can see below, the dark theme is correctly identified and rendered when running in development mode:
And even after building a production bundle and serving the dark theme is identified correctly, the correct props are passed, but the rendered HTML is incorrect--as you can see, the hex values on the theme are correct:
The props passed into the Highlight component are correct:
Yet the HTML is incorrect and defaulting to the default light theme (and vice versa if I switch the default to dark):
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am using Gatsby in combination with prism-react-renderer (I also posted this issue on the prism-react-rendered GitHub) and I've come across an issue which is stumping me. I'm not sure if this is a Gatsby or prism-react-renderer issues...I am attempting to avoid FOUC in prism-react-renderer when implementing a dark / light mode by injecting the
dark
class into my HTML before rendering the page via gatsby-ssr.js, like so:gatsby-ssr.js
While this all works fine for the page theme itself, the issues crop up with my CodeBlock rendering--as you can see in the code below, when I set the default theme I first check whether we have a
window
-- if we do, I check whether thedocument.documentElement.classList
contains our dark theme--if it does, we set this as the default theme. If ourwindow
is undefined, such as during the SSR, we simply return the light theme. This code works perfectly in development mode, and even appears to work perfectly when being served as a static bundle, unfortunately theHighlight
component renders the wrong theme even when receiving the correct props. Please see below:CodeBlock.tsx
As you can see below, the dark theme is correctly identified and rendered when running in development mode:
And even after building a production bundle and serving the dark theme is identified correctly, the correct props are passed, but the rendered HTML is incorrect--as you can see, the hex values on the theme are correct:
The props passed into the
Highlight
component are correct:Yet the HTML is incorrect and defaulting to the default light theme (and vice versa if I switch the default to dark):
You can access my repository with this code here:
https://github.com/cjones26/niggling-aspirations-blog/tree/738a7736e579e51dc7eb349b1404781e0007deef
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions