我切换到了 Mantine 并按照 这个 方法解决了 FART(颜色主题不精确闪烁)问题,但是网页出现了新问题。在页面渲染之前会出现未样式化的组件闪烁。如何解决这个问题?
我查看了 Next.js 的 Github,发现有一些 FOUC 问题,但解决方案是添加
<script>0</script>
,这对我没用。
这个 bug 只在生产环境中存在,在开发环境中运行正常。
示例:https://gotrip.vercel.app
如果您没有看到闪烁,请复制链接并将其粘贴到浏览器中。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 }} />
);
}
}
<script>0</script>
,对我也没有起作用。 - Megan