Nextjs getInitialProps 错误阻止在 Netlify 上构建

Nextjs getInitialProps error preventing build on Netlify

大家好,我继承了一个使用 nextjs 和 运行ning 在 Netlify 上构建的项目。当用户使用 IE 时,我试图加载一个组件。在本地,我能够毫无问题地渲染组件视图。但是,当构建获得 运行 (代码推送到特定 b运行ch) 时,构建在 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

运行ning npm 运行 构建时出错

Cannot read property 'user-agent' of undefined

我 运行 在向 _document.js 添加代码时遇到了几个问题,所以我采用了一种不同的方法,如下所示:

  1. 创建了路由更新浏览器
  2. 向 IE 添加了一个带有消息传递和替代浏览器的组件
  3. 添加了脚本来检查浏览器是 IE 11 还是 IE,如果是则重定向到更新浏览器页面