Flash of Unstyled Content (FOUC) for Nextjs using Mantine
Asked Answered
S

1

12

I switched to Mantine and followed this approach to solve the FART (Flash of inAccurate coloR Theme), but the webpage runs into a new issue. There is a flash of Unstyled component before the page renders. How to solve this issue? I looked into Nextjs Github and there were issues few FOUC issues, but the solution was to add <script>0</script>, which didn't work for me.

This bug is only in production, in development it works fine.

Example: https://gotrip.vercel.app If you don't see the flash, copy the link and paste it in the browser.

import Document, { Html, Head, Main, NextScript } from "next/document";
import { createGetInitialProps } from "@mantine/next";

const getInitialProps = createGetInitialProps();

export default class _Document extends Document {
    static async getInitialProps(ctx) {
        const initialProps = await Document.getInitialProps(ctx);
        return { ...getInitialProps, ...initialProps };
    }

    render() {
        const setInitialTheme = `
      function getUserPreference() {
        if(window.localStorage.getItem('theme')) {
          return window.localStorage.getItem('theme')
        }
        return window.matchMedia('(prefers-color-scheme: dark)').matches 
          ? 'dark' 
          : 'light'
      }
      document.body.dataset.theme = getUserPreference();
    `;
        return (
            <Html>
                <Head />
                <body>
                    <script dangerouslySetInnerHTML={{ __html: setInitialTheme }} />
                    <Main />
                    <NextScript />
                </body>
            </Html>
        );
    }
}
Sung answered 8/3, 2022 at 19:29 Comment(7)
Can you describe the flash? I am not seeing it. Tried in Private/Incognito/Guest window with Firefox 97 and Chrome 95.Auxesis
I was expecting dark mode to appear - based on your code it looks like there's a browser preference for this but I'm not sure where that might live.Auxesis
If I don't use incognito and set dark mode, then close and reopen, I do see a flash of white across the whole page. Is this the one you're talking about, or was it specific to one component?Auxesis
@Auxesis There is an example of the flashing behavior in this github issue. Also, I am able to see the issue when I refresh my site in FireFox. The code for this site can be found here on my github.Ambrogio
@Ambrogio are you sure this is the same issue as OP? I can see the issue on your site, but not on OP's site.Auxesis
@Auxesis I think so, we're both using the Mantine library. Additionally, the fix suggested inside of NextJS threads related to FOUC to add <script>0</script> also does not work for meAmbrogio
@Ambrogio I am not convinced this is the same issue. You are using SSR (and this is the cause of your flash, according to Mantine dev), OP is not. I can see the flash on your site, whereas I don't see it on OP's.Auxesis
H
2

After going through this Github issue I applied three solutions to my project because I have had similar issues.

If anybody comes across this issue try the following it should work for you.

  1. If you have not done it configure your Next.js project to work with Mantine using this instruction. (NOTICE: I recommend applying the optional step also)

  2. If you host your project on Vercel or for local development - use "yarn build" instead of npm

  3. Downgrade Next.js to 12.1.5 if you are on 12.1.6 or upgrade to something newer (@canary versions)

I hope this solves your problem. This is my first answer so be kind :)

Harlequinade answered 18/6, 2022 at 0:39 Comment(1)
Instruction link isn't workingIonian

© 2022 - 2024 — McMap. All rights reserved.