在 Gatsby Blog 中 url 的路径中追加 %20

Appending %20 in the path of the url in Gatsby Blog

我已经使用 Gatsby 的入门模板建立了一个博客。现在,当我打开一篇文章时,它显示的 url 是- http://localhost:8000/JavaScript:%20Behind%20The%20Scenes/。我查找了 并更改了 path 属性 但是随后页面无法加载,它只显示一个具有相同 url 的空白页面。我不知道为什么它在路径中附加 %20

注意:路径其实就是文件夹名。例如,在目录/content/blog/JavaScript:Behind The Scenes/index.md中,url中的路径实际上是文件夹名称。我不知道为什么。路径应该是我在该文件夹的 index.md 中写的标题。

index.md

---
title: 'The Execution Context'
date: '2020-02-16'
category: "JavaScript"
---

Blog Content..............

gatsby-node.js

const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)

exports.createPages = ({ graphql, actions }) => {
  const { createPage } = actions

  const blogPostTemplate = path.resolve(`./src/templates/blog-post.js`)

  return graphql(
    `
      {
        allMarkdownRemark(
          sort: { fields: [frontmatter___date], order: DESC }
          limit: 1000
        ) {
          edges {
            node {
              fields {
                slug
              }
              frontmatter {
                title
                category
              }
            }
          }
        }
      }
    `
  ).then(result => {
    if (result.errors) {
      throw result.errors
    }

    // Create blog posts pages.
    const posts = result.data.allMarkdownRemark.edges.filter(
      ({ node }) => !!node.frontmatter.category
    )

    posts.forEach((post, index) => {
      const previous = index === posts.length - 1 ? null : posts[index + 1].node
      const next = index === 0 ? null : posts[index - 1].node

      createPage({
        path: post.node.fields.slug,
        component: blogPostTemplate,
        context: {
          slug: post.node.fields.slug,
          previous,
          next,
        },
      })
    })
  })
}

exports.onCreateNode = ({ node, actions, getNode }) => {
  const { createNodeField } = actions

  if (node.internal.type === `MarkdownRemark`) {
    const value = createFilePath({ node, getNode })

    createNodeField({
      name: `slug`,
      node,
      value,
    })
  }
}

此外,Github 和 Twitter 链接也有问题。当我点击它们时,它显示找不到页面。它表现得很奇怪 url: https://github.com/https://github.com/myGithubName https://twitter.com/https://twitter.com/myTwitterName

我查看了它的位置并发现:

gatsby-meta-config.js

module.exports = {
  title: `My Blog`,
  description: `Blog posted about ...`,
  author: `myName`,
  introduction: `I explain with words and code.`,
  siteUrl: `https://gatsby-starter-bee.netlify.com`, // Your blog site url
  social: {
    twitter: `https://twitter.com/myTwitterName`, // Your Twitter account
    github: `https://github.com/myGithubName`,
    medium: ``,
    facebook: ``

  },
  icon: `content/assets/profile.jpeg`, // Add your favicon
  keywords: [`blog`],
  comment: {
    disqusShortName: '', // Your disqus-short-name. check disqus.com.
    utterances: 'JaeYeopHan/gatsby-starter-bee', // Your repository for archive comment
  },
  configs: {
    countOfInitialPost: 10, // Config your initial count of post
  },
  sponsor: {
    buyMeACoffeeId: 'jbee',
  },
  share: {
    facebookAppId: '', // Add facebookAppId for using facebook share feature v3.2
  },
  ga: '', // Add your google analytics tranking ID
}

gatsby-meta-config.js 中的链接似乎是正确的。

I don't know why it's appending %20 in the path.

%20 是 url 内的 space 的 HTML encoding。您的 url 中不能有 space,因此默认情况下它会被 HTML 编码转义。

the url is actually the folder name. I don't know why. Path should've been the title that I've written in index.md of that folder.

您没有对 gatsby-node.js 中的 slug 进行任何格式化:

    createNodeField({
      name: `slug`, 
      node,
      value,
    })

如果不格式化 slug,您的 url 默认为项目内的路径。

我的建议:不要格式化 slug。从您的文件夹路径中删除 spaces,您将得到一个漂亮的 url:/content/blog/javascript-behind-the-scenes/index.md。 Google 也推荐使用连字符 -。拥有这样的 URL 在 SEO 中排名更好。

Also, I have some problem with the Github and Twitter links. When I click them, it shows page not found. Weird url it shows is: https://github.com/https://github.com/myGithubName https://twitter.com/https://twitter.com/myTwitterName

gatsby-config.js:

中仅提供您社交网络的用户名
  social: {
    twitter: `myTwitterName`, // remove everything before your username
    github: `myGithubName`, // remove everything before your username
    medium: ``,
    facebook: ``
  },