Astro transitions are great. Some folks are coming up with some really slick examples of how to use them, and even this little ole blog uses the defaults just to add a little flair when navigating between pages. However, I ran into a little issue when I started to implement dark mode on this site. The dreaded Flash of Unstyled Content (FOUC).
What is the FOUC?
A Flash of Unstyled Content is when the browser renders the page before the CSS has been loaded. The browser will render the page with the default styles, and then apply the CSS once it has been loaded. This can cause a jarring experience for the user, especially if the page is styled differently after the CSS has been loaded (often noticeable when switching between light and dark mode!).
You can see the site initially loads with the white theme in place, then the dark mode preference is read from localstorage, and so the Tailwind dark class is applied and the theme changed. This causes the content to flicker as the styles are swapped out, and can happen during page transitions as well.
Josh Comeau has a great article on the dreaded flicker and how to prevent it, which is well worth a read!
So lets see how we can use that knowledge to prevent the FOUC when using Astro transitions and Tailwind.
The setup
So in this case I’m using a React Astro island for my theme toggle. It attempts to load the current theme into state, and then a useEffect will adjust the document body with the Tailwind class accordingly. (well, small white lie, I’m actually using a nano store but we’ll leave that out to keep things simple!)
and here he is, try giving him a click!
This works fine, but we would still have the FOUC when pages transition and on initial load, so let’s fix the page load first.
Page load
To fix the FOUC on page load, we can use a little bit of JavaScript to add a class to the document body when the page is loaded. The reason this works is because we use the Astro directive is:inline
to add the script to the page, and it will be executed before the page is rendered. As usually Astro will strip out any script tags, but with is:inline
we can keep them in place.
We can place the following code in the head of our document.
First we setup a function to set the theme which uses a theme
function to try and load the current theme from storage, which would be a previously set preference. Failing that, we fallback to match the media query to provide the preference of the user based on their system settings. Lastly, if none of that matches, it would return the 'light'
theme.
With the theme value set, we then add the class to the document body, which will apply the theme to the page before render because the script is inline in our <head>
, therefore parsed and rendered before the rest of the HTML.
Page transitions
You’ll notice that the page transitions still cause a FOUC (and if you look really closely, you’ll notice the page reload where the font resets itself, is fixed! 🎉). This is because the Astro transitions are replacing the page elements. So when the page transitions, the body class is removed and then reapplied, causing the flicker.
To fix this, we can use some additional Astro events, astro:page-load
and astro:after-swap
, to listen out for page transitions. That way we can trigger applying the body class correctly in all cases.
That’s what’s in place on this site, and how it’s working!
Thanks for reading! 💜