akhilD
akhilD

Reputation: 681

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

I have set up a blog using Gatsby's starter template. Right now, when I open an article, the url it shows is- http://localhost:8000/JavaScript:%20Behind%20The%20Scenes/. I looked up this answer and changed the path property but then the page wouldn't load, it just showed an empty page with the same url. I don't know why it's appending %20 in the path.

Note: The path is actually the folder name. For example, in the directory /content/blog/JavaScript:Behind The Scenes/index.md, path that goes in 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.

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,
    })
  }
}

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

I checked in where is this located and found out:

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
}

The links seem correct in gatsby-meta-config.js.

Upvotes: 1

Views: 683

Answers (1)

EliteRaceElephant
EliteRaceElephant

Reputation: 8162

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

%20 is the HTML encoding for a space inside the url. You cannot have spaces in your url so it is by default escaped by the HTML encoding.

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.

You do not do any formatting to your slug in gatsby-node.js:

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

Without formatting the slug, your url defaults to the path inside your project.

My advise: Don't format the slug. Remove spaces from your folder path and you have a nice url: /content/blog/javascript-behind-the-scenes/index.md. The use of the hypen character - is also recommened by Google. Having an URL like that ranks better in 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

Supply only your social network's username in your gatsby-config.js:

  social: {
    twitter: `myTwitterName`, // remove everything before your username
    github: `myGithubName`, // remove everything before your username
    medium: ``,
    facebook: ``
  },

Upvotes: 2

Related Questions