question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gatsby-ssr.js ► pathname parameter always return a slash (/) in development mode

See original GitHub issue

Description

The pathname parameter in the onRenderBody function inside the gatsby-ssr.js file always return a slash(/) when running in development mode. But it’s working fine in production mode.

Here’s a snippet of my gatsby-ssr.js code:

const config = require('./gatsby-config')

exports.onRenderBody = ( {pathname, setHeadComponents, setBodyAttributes}, pluginOptions ) => {
  setHeadComponents([
    <link rel="canonical" href={`${config.siteMetadata.siteUrl}${pathname}`} />,
  ])
}

Steps to reproduce

Simply use the code above in your gatsby-ssr.js file and run it in development mode with gatsby develop. Then look at the canonical link in the <head> section of each rendered page from your browser and it’s always the same even when you refresh the page.

Expected result

Using the code above, when I go to http://localhost:8000/page-2/ with a browser, the pathname should return the path of the current rendered page and inject this canonical link in the <head> like this: <link rel="canonical" href="http://localhost:8000/page-2/">

Actual result

The pathname always return the root link of the website (/) which inject this canonical link instead: <link rel="canonical" href="http://localhost:8000/">

Environment

Development environment.

Workaround

The only workaround I’ve found so far is:

  1. gatsby build
  2. gatsby serve

Then it’s working as expected.

Thanks!

Awesome framework by the way!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ascorbiccommented, Sep 10, 2020

Hi @guydumais. We’re working on changing it, but currently in develop we don’t do SSR. All we do is render an entry point page, which is why you’re seeing pathname as /. If you want to try the work in progress version that does SSR, try installing gatsby@develop-ssr, but please don’t use it in production!

0reactions
Johnrobmillercommented, Aug 19, 2021

Hi @guydumais. We’re working on changing it, but currently in develop we don’t do SSR. All we do is render an entry point page, which is why you’re seeing pathname as /. If you want to try the work in progress version that does SSR, try installing gatsby@develop-ssr, but please don’t use it in production!

I’m googling “gatsby@develop-ssr” but nothing is appearing in search results. Can you be more specific?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gatsby Server Rendering APIs
To use the Gatsby SSR APIs, create a file called gatsby-ssr.js in the root of your site. ... The pathname of the page...
Read more >
How to resolve trailing slash on refresh in GatsbyJS
Have you tried removing the trailing slash in the slug creation? createPage({ path: '/' + post.uri.replace(/\/$/, ...
Read more >
How to Get the Current Page URL in Gatsby | CSS-Tricks
Build time happens when you are done developing and tell Gatsby to generate final optimized assets using the gatsby build command.
Read more >
Trailing Slashes and Gatsby - Jon
If you programmatically call createPage() and pass a path of "/cars/mustang/" , the trailing slash on that path string is not what causes...
Read more >
How To Use Static Files in Gatsby | DigitalOcean
Like many popular Static Site Generators, Gatsby embraces the use of dynamic web frameworks, using React on the frontend and Node.js on the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found