Gatsby构建错误“未定义文档”仅在Netlify上

时间:2019-09-03 18:39:36

标签: gatsby netlify

在Netlify上构建时,我在404页面上得到的文档未定义构建错误,但是该构建在本地计算机上成功运行。我也没有引用页面中的任何文档或窗口对象。

我尝试删除404页面,但相同错误出现在列表的下一页。

这是我的404页面

import React from "react";
import styled from "styled-components";
import AniLink from "gatsby-plugin-transition-link/AniLink";

const Container = styled.div`
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  background: black;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 800;
  color: white;
`;

const Heading = styled.h1`
  font-family: "URWAccidalia";
  font-size: 50px;
  color: white;
  text-align: center;
  z-index: 805;
`;

const HomeButton = styled(AniLink)`
  font-family: "URWAccidalia";
  font-size: 30px;
  color: white;
  text-align: center;
  margin-top: 50px;
  z-index: 806;
  background: none;
  border: none;
  text-decoration: none;
  opacity: 1;
`;

const ErrorPage = () => {
  return (
    <Container>
      <Heading>Page Not Found</Heading>
      <HomeButton fade to="/">
        Go Back to Home
      </HomeButton>
    </Container>
  );
};

export default ErrorPage;

这是来自Netlify的错误

2:20:59 PM: success Generating image thumbnails — 850/850 - 162.737 s
2:21:07 PM: error #95313 ReferenceError: document is not defined
2:21:07 PM: Building static HTML failed for path "/404/"
2:21:07 PM: See our docs page for more info on this error: https://gatsby.dev/debug-html
2:21:07 PM: 
2:21:07 PM:   ReferenceError: document is not defined
2:21:07 PM:   
2:21:07 PM:   - render-page.js:21018 Object
2:21:07 PM:     /opt/build/repo/public/render-page.js:21018:21
2:21:07 PM:   
2:21:07 PM:   - react-dom-server.node.production.min.js:26 ya
2:21:07 PM:     [repo]/[react-dom]/cjs/react-dom-server.node.production.min.js:26:264
2:21:07 PM:   
2:21:07 PM:   - react-dom-server.node.production.min.js:29 Object.useState
2:21:07 PM:     [repo]/[react-dom]/cjs/react-dom-server.node.production.min.js:29:82
2:21:07 PM:   
2:21:07 PM:   - react.production.min.js:23 useState
2:21:07 PM:     [repo]/[react]/cjs/react.production.min.js:23:312
2:21:07 PM:   
2:21:07 PM:   - render-page.js:21017 ./node_modules/react-three-fiber/dist/index.js.Object
2:21:07 PM:     /opt/build/repo/public/render-page.js:21017:76
2:21:07 PM:   
2:21:07 PM:   - react-dom-server.node.production.min.js:33 c
2:21:07 PM:     [repo]/[react-dom]/cjs/react-dom-server.node.production.min.js:33:501
2:21:07 PM:   
2:21:07 PM:   - react-dom-server.node.production.min.js:36 Sa
2:21:07 PM:     [repo]/[react-dom]/cjs/react-dom-server.node.production.min.js:36:1
2:21:07 PM:   
2:21:07 PM:   - react-dom-server.node.production.min.js:41 a.render
2:21:07 PM:     [repo]/[react-dom]/cjs/react-dom-server.node.production.min.js:41:467
2:21:07 PM:   
2:21:07 PM:   - react-dom-server.node.production.min.js:41 a.read
2:21:07 PM:     [repo]/[react-dom]/cjs/react-dom-server.node.production.min.js:41:58
2:21:07 PM:   
2:21:07 PM:   - react-dom-server.node.production.min.js:53 renderToString
2:21:07 PM:     [repo]/[react-dom]/cjs/react-dom-server.node.production.min.js:53:83
2:21:07 PM:   
2:21:07 PM:   - render-page.js:563 Module../.cache/static-entry.js.__webpack_exports__.defau    lt
2:21:07 PM:     /opt/build/repo/public/render-page.js:563:28
2:21:07 PM:   
2:21:07 PM:   - render-html.js:35 Promise
2:21:07 PM:     [repo]/[gatsby]/dist/utils/worker/render-html.js:35:36
2:21:07 PM:   
2:21:07 PM:   - debuggability.js:313 Promise._execute
2:21:07 PM:     [repo]/[bluebird]/js/release/debuggability.js:313:9
2:21:07 PM:   
2:21:07 PM:   - promise.js:488 Promise._resolveFromExecutor
2:21:07 PM:     [repo]/[bluebird]/js/release/promise.js:488:18
2:21:07 PM:   
2:21:07 PM:   - promise.js:79 new Promise
2:21:07 PM:     [repo]/[bluebird]/js/release/promise.js:79:10
2:21:07 PM:   
2:21:07 PM:   - render-html.js:31 Promise.map.path
2:21:07 PM:     [repo]/[gatsby]/dist/utils/worker/render-html.js:31:37
2:21:07 PM:   
2:21:07 PM: 
2:21:07 PM: Skipping functions preparation step: no functions directory set

1 个答案:

答案 0 :(得分:3)

简短答案:Gatsby预先渲染了构建中的组件以静态地为其提供服务(类似于服务器端渲染)。确保仅在挂载组件(客户端)时访问浏览器上下文变量,例如document。您可以结合使用useState和useEffect挂钩来实现此目的。

盖茨比(Gatsby)试图构建页面以静态方式为其提供服务,但在构建时缺少document全局变量,该变量仅在浏览器上下文中可用。

就您而言,我认为问题可能是由AniLink组件在构建时触发的,因为它使用了document变量,如您所见here。尝试延迟您的AniLink或父组件的实例化。

例如:

const ErrorPage = () => {
  const [canRender, setCanRender] = useState(false);

  useEffect(() => setCanRender(true));

  return (
    <Container>
      {
        canRender ?
          <div>
            <Heading>Page Not Found</Heading>
            <HomeButton fade to="/">
              Go Back to Home
            </HomeButton>
          </div>
        : null
    }
    </Container>
  );
}

此外,如果需要在静态呈现的组件中使用document浏览器上下文变量,则可以将其替换为react挂钩,以避免在构建时出现问题。

例如,如果您有类似这样的内容:

const Page = () => {
  return (
    <div>
      { 
        document.querySelector('nav')
          ? `This is page has a nav tag`
          : `This page doesn't have a nav tag` 
      }
    </div>
  );
}

您可以通过以下更改来解决您的问题:

const Page = () => 

  const [hasNavTag, setHasNavTag] = useState(false);

  useEffect(() => setHasNavTag(document.querySelector('nav') != null) );

  return (
    <div>
      { 
        hasNavTag
          ? `This is page has a nav tag`
          : `This page doesn't have a nav tag` 
      }
    </div>
  );
}

迟到总比没有好:)