admin管理员组文章数量:1418589
I get this error when I run gatsby build. I have not used "document" in my code. Can someone explain what this means?
ERROR
Page data from page-data.json for the failed page "/": {
"ponentChunkName": "ponent---src-pages-index-js", "path": "/", "result": { "pageContext": {} }, "staticQueryHashes": [] }failed Building static HTML for pages - 2.990s
ERROR #95312
"document" is not available during server side rendering.
I get this error when I run gatsby build. I have not used "document" in my code. Can someone explain what this means?
Share Improve this question asked Jan 25, 2022 at 16:19 ytwizytwiz 231 silver badge3 bronze badges 1ERROR
Page data from page-data.json for the failed page "/": {
"ponentChunkName": "ponent---src-pages-index-js", "path": "/", "result": { "pageContext": {} }, "staticQueryHashes": [] }failed Building static HTML for pages - 2.990s
ERROR #95312
"document" is not available during server side rendering.
- Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Feb 3, 2022 at 13:09
1 Answer
Reset to default 4The reason why this issue appears is because somewhere in your code you are using document
global object and, because gatsby develop
is rendered by the browser, where there are window
and document global objects, it piles, however, when you run gatsby build
, the code is piled in the Node server, where there's no window
or document
variables because they are not even defined yet, they are client-side variables parsed in the SSR (Server-Side Rendering).
This is an extreme reduction of what's happening, you can find a more detailed explanation in Debugging HTML Builds docs.
To fix/bypass this issue, you only need to add the following condition where you are using document
object.
if(window !== "undefined"){
// your document or window manipulation
}
You can use both window
or document
in the condition, they are equivalent in terms of bypassing the server-side rendering.
If you are not using document
in your project, the issue may still rise if some of your dependencies (third-party) are using it (i.e: canvas, maps, sliders that uses JavaScript calculations, etc). If that's your scenario, the way to bypass it is to ignore webpacks bundling by adding a null
loader:
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === "build-html" || stage === "develop-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /bad-module/,
use: loaders.null(),
},
],
},
})
}
}
Where /bad-module/
is a regular expression (test
) (that's why is between slashes, /
). In this case, you need to replace bad-module
for the dependency folder name in node_modules
.
本文标签: javascriptPage data from pagedatajson for the failed page quotquotStack Overflow
版权声明:本文标题:javascript - Page data from page-data.json for the failed page "" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745297198a2652152.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论