gpt4 book ai didi

Gatsby 构建错误 "document is not defined"仅在 Netlify 上

转载 作者:行者123 更新时间:2023-12-03 23:10:47 27 4
gpt4 key购买 nike

当我在 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

最佳答案

Short answer: Gatsby pre-renders components on build to serve them statically (mimics Server-side Rendering). Be sure to access browser context variables, such as document, only when your component is mounted (client-side). You can use a combination of useState and useEffect hooks to achieve this.



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 钩子(Hook)以避免在构建时出现问题。

例如,如果你有这样的事情:
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>
);
}


迟到总比不到好 :)

关于Gatsby 构建错误 "document is not defined"仅在 Netlify 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57777138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com