Nextjs css 闪烁
Nextjs css flickering
我正在使用 material ui 和 nextjs 开发 ui一个 React 应用程序。我正在使用 <Autocomplete />
组件,由 material UI 提供,并用我自己的样式覆盖它的一些样式,如下所示:
<Autocomplete
classes={{
root: `${styles[`search__autocomplete`]} ${
styles[`search--${variant}__autocomplete`]
}`,
inputRoot: `${styles[`search__autocomplete-input`]} ${
styles[`search--${variant}__autocomplete-input`]
}`
}}
/>
variant
是一个道具,它被传递给组件,styles
是一个变量,它从 css 模块: import styles from "Search.module.sass"
.
现在,当我在本地处理这个时,一切看起来都很棒:
但是,在我通过 next build && next export
将其部署到生产环境后,我开始遇到“闪烁”效果,大约 1/3 秒我的页面看起来像这样:
我的猜测是,这可能与 nextjs 将我的 css 导出到生产环境中的多个文件有关:
<link
rel="preload"
href="/_next/static/css/4dcd7fa805fb41261f08.css"
as="style"
/>
<link
rel="stylesheet"
href="/_next/static/css/4dcd7fa805fb41261f08.css"
data-n-g=""
/>
<link
rel="preload"
href="/_next/static/css/a23cf79bceae4047fddb.css"
as="style"
/>
<link
rel="stylesheet"
href="/_next/static/css/a23cf79bceae4047fddb.css"
data-n-p=""
/>
我该如何解决?
解决方案是创建自定义 pages/_document.js
页面:
import React from 'react';
// Modules
import Document, { Html, Head, Main, NextScript } from 'next/document';
// MUI Core
import { ServerStyleSheets } from '@material-ui/core/styles';
class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600;700&display=swap" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side rendering (SSR).
MyDocument.getInitialProps = async (ctx) => {
// Resolution order
//
// On the server:
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. document.getInitialProps
// 4. app.render
// 5. page.render
// 6. document.render
//
// On the server with error:
// 1. document.getInitialProps
// 2. app.render
// 3. page.render
// 4. document.render
//
// On the client
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. app.render
// 4. page.render
// Render app and page and get the context of the page with collected side effects.
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () => originalRenderPage({
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [
...React.Children.toArray(initialProps.styles),
sheets.getStyleElement(),
],
};
};
export default MyDocument;
我正在使用 material ui 和 nextjs 开发 ui一个 React 应用程序。我正在使用 <Autocomplete />
组件,由 material UI 提供,并用我自己的样式覆盖它的一些样式,如下所示:
<Autocomplete
classes={{
root: `${styles[`search__autocomplete`]} ${
styles[`search--${variant}__autocomplete`]
}`,
inputRoot: `${styles[`search__autocomplete-input`]} ${
styles[`search--${variant}__autocomplete-input`]
}`
}}
/>
variant
是一个道具,它被传递给组件,styles
是一个变量,它从 css 模块: import styles from "Search.module.sass"
.
现在,当我在本地处理这个时,一切看起来都很棒:
但是,在我通过 next build && next export
将其部署到生产环境后,我开始遇到“闪烁”效果,大约 1/3 秒我的页面看起来像这样:
我的猜测是,这可能与 nextjs 将我的 css 导出到生产环境中的多个文件有关:
<link
rel="preload"
href="/_next/static/css/4dcd7fa805fb41261f08.css"
as="style"
/>
<link
rel="stylesheet"
href="/_next/static/css/4dcd7fa805fb41261f08.css"
data-n-g=""
/>
<link
rel="preload"
href="/_next/static/css/a23cf79bceae4047fddb.css"
as="style"
/>
<link
rel="stylesheet"
href="/_next/static/css/a23cf79bceae4047fddb.css"
data-n-p=""
/>
我该如何解决?
解决方案是创建自定义 pages/_document.js
页面:
import React from 'react';
// Modules
import Document, { Html, Head, Main, NextScript } from 'next/document';
// MUI Core
import { ServerStyleSheets } from '@material-ui/core/styles';
class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600;700&display=swap" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side rendering (SSR).
MyDocument.getInitialProps = async (ctx) => {
// Resolution order
//
// On the server:
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. document.getInitialProps
// 4. app.render
// 5. page.render
// 6. document.render
//
// On the server with error:
// 1. document.getInitialProps
// 2. app.render
// 3. page.render
// 4. document.render
//
// On the client
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. app.render
// 4. page.render
// Render app and page and get the context of the page with collected side effects.
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () => originalRenderPage({
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [
...React.Children.toArray(initialProps.styles),
sheets.getStyleElement(),
],
};
};
export default MyDocument;