gatsby-ssr.js ► pathname parameter always return a slash (/) in development mode
See original GitHub issueDescription
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:
- gatsby build
- gatsby serve
Then it’s working as expected.
Thanks!
Awesome framework by the way!
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
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 installinggatsby@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?