使用 Mantine 用于 Nextjs 的无样式内容 Flash (FOUC)

发布于 2025-01-12 14:32:52 字数 1595 浏览 0 评论 0原文

我改用 Mantine 并按照这种方法解决了 FART(闪现) inAccurate color Theme),但网页遇到了新问题。在页面渲染之前,会出现一闪而过的 Unstyled 组件。如何解决这个问题? 我查看了 Nextjs Github,发现有一些 FOUC 问题,但解决方案是添加 ,这对我来说不起作用。

此错误仅在生产中存在,在开发中它工作正常。

示例:https://gotrip.vercel.app 如果您没有看到 Flash,请复制链接并将其粘贴到浏览器中。

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>
        );
    }
}

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>
        );
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2025-01-19 14:32:52

在经历了这个Github问题之后,我在我的项目中应用了三种解决方案,因为我有类似的解决方案问题。

如果有人遇到此问题,请尝试以下操作,它应该对您有用。

  1. 如果您尚未完成此操作,请使用此说明。
    (注意:我建议也应用可选步骤)


  2. 如果您在 Vercel 上托管项目或进行本地开发 - 使用“yarn build”而不是 npm

  3. 如果您使用的是 12.1.6,请将 Next.js 降级到 12.1.5 或升级到更新的版本(@canary 版本)

我希望这可以解决您的问题。这是我的第一个答案,所以请友善:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文