[英]How can I use getInitialProps only during the NextJS site build?
[英]Nextjs getInitialProps error preventing build on Netlify
嘿伙计们,我继承了一个用 nextjs 构建并在 Netlify 上运行的项目。 我正在尝试在用户使用 IE 时加载组件。 在本地,我可以毫无问题地渲染组件视图。 但是当构建运行(代码推送到特定分支)时,构建在 Netlify 上失败。 Netlify 似乎不喜欢以下内容。
const userAgent = props ? props.req.headers['user-agent'] : navigator.userAgent
接受任何建议。 我是 Netlify 的新手,只是试图显示一个让用户知道他们的浏览器不受支持的组件。 下面是我在 _document.js 中的代码
_document.js
class MyDocument extends Document {
static async getInitialProps(props) {
const page = props.renderPage()
const userAgent = props ? props.req.headers['user-agent'] : navigator.userAgent
return {
...page,
userAgent
}
}
renderBrowserSupport() {
let msie = this.props.userAgent.indexOf('MSIE ');
let ie11 = this.props.userAgent.indexOf('Trident/');
if(msie > 0 || ie11 > 0) {
return (
<BrowserSupport/>
)
}
else return false;
}
render() {
<html lang="en">
<body>
{this.renderBrowserSupport()}
</body>
</html>
}
}
export default MyDocument
运行 npm 运行构建时出错
Cannot read property 'user-agent' of undefined
我在向 _document.js 添加代码时遇到了几个问题,所以我采用了不同的方法,如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.