首页 > 解决方案 > 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

标签: gatsbynetlify

解决方案


简短的回答: Gatsby 在构建时预渲染组件以静态地为它们服务(模仿服务器端渲染)。确保document仅在您的组件已安装(客户端)时访问浏览器上下文变量,例如 。您可以结合使用 useState 和 useEffect 挂钩来实现此目的。

Gatsby 正在尝试构建您的页面以静态提供它,但它缺少document构建时间的全局变量,该变量仅在浏览器上下文中可用。

在您的情况下,我认为问题可能是由 AniLink 组件在构建时触发的,因为它使用了document变量,如您在此处看到的。尝试延迟 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>
  );
}

迟到总比不到好 :)


推荐阅读