简体   繁体   English

通过 Gatsby createPages 节点 api 创建的页面,部署到 Github 页面时不起作用

[英]Pages created via the Gatsby createPages node api, not working when deployed to Github Pages

I am creating some pages with the Gatsby createPages node api as mentioned here: https://www.gatsbyjs.com/docs/programmatically-create-pages-from-data/#creating-pages我正在使用 Gatsby createPages 节点 api 创建一些页面,如此处所述: https://www.gatsbyjs.com/docs/programmatically-create-pages-from-data/#creating-pages

But when I deploy to github pages, those pages give a 404 for example this page: https://giorgioremindme.github.io/probable-future但是当我部署到 github 页面时,这些页面会给出 404,例如此页面: https://giorgioremindme.githubio。

This is what I have in my gatsby-node-js:这就是我的 gatsby-node-js 中的内容:

const fs = require(`fs`)
const yaml = require(`js-yaml`)
exports.createPages = ({ actions }) => {
  const { createPage } = actions
  const ymlDoc = yaml.safeLoad(fs.readFileSync(`./src/content/index.yaml`, `utf-8`))
  ymlDoc.forEach(element => {
    createPage({
      path: element.path,
      component: require.resolve(`./src/templates/basicTemplate.js`),
      context: {
        pageContent: element.content,
        links: element.links,
      },
    })
  })
}

https://github.com/GiorgioRemindme/giorgio-martini/blob/main/gatsby-node.js https://github.com/GiorgioRemindme/giorgio-martini/blob/main/gatsby-node.js

Locally works fine though...虽然在本地工作正常...

You can visit these new pages by clicking on the thumbnail you see on this page: https://giorgioremindme.github.io/giorgio-martini/code ignore the last thumbnail, that one is broken, is missing a valid link, but the rest do work locally, but not deployed.您可以通过单击在此页面上看到的缩略图访问这些新页面: https://giorgioremindme.github.io/giorgio-martini/code忽略最后一个缩略图,该缩略图已损坏,缺少有效链接,但是rest 在本地工作,但未部署。

Any ideas on what i need to do to make this pages work when deployed?关于在部署时我需要做什么才能使此页面正常工作的任何想法?

If you enter to: https://giorgioremindme.github.io/giorgio-martini/code (without trailing slash. You can remove it by clicking "Code" link in the header).如果您输入: https://giorgioremindme.github.io/giorgio-martini/code (没有尾部斜杠。您可以通过单击标题中的“代码”链接将其删除)。

And then you navigate, the site works smooth and no 404 are returned.然后你导航,网站运行顺畅,没有返回 404。

However, if you enter to https://giorgioremindme.github.io/giorgio-martini/code/ (note the trailing slash), your pages return a 404 when trying to navigate to them.但是,如果您输入https://giorgioremindme.github.io/giorgio-martini/code/ (注意尾部斜杠),您的页面在尝试导航到它们时会返回 404。

There's nothing wrong with your gatsby-node.js as the pages are properly created and they exist.您的gatsby-node.js没有任何问题,因为页面已正确创建并且它们存在。 Your issue is in your in-app linking/navigation, so in the views.您的问题在于您的应用内链接/导航,因此在视图中。

Nevertheless, my guess is that you are prefixing paths manually adding /code directly while Gatsby link should do it on their own.不过,我的猜测是,您是在直接手动添加/code的路径前添加前缀,而 Gatsby 链接应该自己完成。 Try using withPrefix helper when building your links manually.手动构建链接时尝试使用withPrefix助手

Double-check this behavior and the build locally to be sure where the source of the issue is (locally or in GitHub's environment) because the solution can vary depending on that.仔细检查此行为和本地构建,以确保问题的根源在哪里(本地或 GitHub 的环境中),因为解决方案可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM