我的简单NextJS页面长这样(结果可以在https://www.schandillia.com/中查看):
/* eslint-disable no-unused-vars */
import React, { PureComponent, Fragment } from 'react';
import Head from 'next/head';
import compose from 'recompose/compose';
import Layout from '../components/Layout';
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
const styles = {
root: {
textAlign: 'center',
paddingTop: 200,
},
p: {
textTransform: 'uppercase',
color: 'red',
},
};
class Index extends PureComponent {
render() {
const { classes } = this.props;
const title = 'Project Proost';
const description = 'This is the description for the homepage';
return (
<Fragment>
<Head>
<title>{ title }</title>
<meta name="description" content={description} key="description" />
</Head>
<Layout>
<p className={classes.p}>amit</p>
<Button variant="contained" color="secondary">
Secondary
</Button>
</Layout>
</Fragment>
);
}
}
export default withStyles(styles)(Index);
我正在导入一堆组件,使用@material-ui/core
库为我的项目进行样式设置。同时我也有一个本地的样式定义被分配给了一个style
常量。
这里发生的问题似乎是我的样式没有在服务器上渲染,这就是为什么在加载时提供的文件没有样式的原因。然后CSS由客户端代码呈现。结果就是会有一闪而过的未经样式处理的内容,持续时间差不多一秒钟,足够引起注意。
有没有解决这个问题的方法?整个代码库可以在https://github.com/amitschandillia/proost/tree/master/web中参考。
<head>
部分。但我的问题是这种注入只发生在客户端而不是服务器端。这就是导致FOUC的原因。我需要一些方法来确保样式信息在页面推送之前在服务器端被注入。 - TheLearnerreact-create-app
创建了应用程序,我建议使用react-create-app
的react-scripts build
。 我曾经花费了相当多的时间为此配置webpack,然后发现它已经默认附带了一个简单但有用的构建工具。 - Pedro Vieira