Unwanted trailing slash on prefixPath's root
See original GitHub issueDescription
I’m working for a company which uses gatsby for building multilanguage website.
For both SEO reasons and aesthetics, we just don’t want to see a trailing slash on the homepage, which in our case is the root of the prefixPath.
www.mydomain.com/english
would always become
www.mydomain.com/english/
I know that gatsby build --prefix-paths
and gatsby serve
will cause an express 301, just to be clear, I’m not refering to that, I see gatsby serve as a devtool and don’t care about a slash there. We use nginx to redirect trailing slash to urls without trailing slashes, but gatsby (or reach router, not sure) will still append a trailing slash, as you can see in my reproducible example here:
https://github.com/hlolli/gatsby-bug-reproduction
Steps to reproduce
Clone https://github.com/hlolli/gatsby-bug-reproduction
build with --prefix-paths and start a server via the expressjs script named server.js (also under yarn serve
).
Open in browser: http://localhost:3000/de
Expected result
http://localhost:3000/de stays
Actual result
becomes
Hint: disable javascript and open http://localhost:3000/de again, and you’ll see that the trailing slash will not get appended.
Environment
npx gatsby info --clipboard
System:
OS: Linux 5.4 NixOS 20.03 (Markhor) 20.03.1577.74a80c5a9ab (Markhor)
CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Shell: 3.0.2 - /run/current-system/sw/bin/fish
Binaries:
Node: 13.8.0 - /run/current-system/sw/bin/node
Yarn: 1.22.0 - /run/current-system/sw/bin/yarn
npm: 6.13.6 - /run/current-system/sw/bin/npm
Languages:
Python: 3.8.1 - /run/current-system/sw/bin/python
Browsers:
Firefox: 75.0
npmPackages:
gatsby: ^2.21.13 => 2.21.13
npmGlobalPackages:
gatsby-cli: 2.8.13
p.s. https://www.gatsbyjs.org/packages/gatsby-plugin-remove-trailing-slashes/ was of no help.
As well as adding redirect via <Navigate from="/de/" to="/de" />
this will cause gatsby to throw bunch of errors as far as I remember.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:10
Top GitHub Comments
After investigating the errors, I come to the conclusion, that at least the direction I took could be still feasible, but also, that a simple regex added as code change is not enough:
findPath
function infind-path.js
now resolves the path as"/de"
and not"/"
, which triggers that it looks in[...]/de/page-data.json
instead of[...]/index/page-data.json
, so it tries to load JSON data from a wrong location.404.html
insrc/
production-app.js
exists, becausepublicLoader.loadPage(browserLoc.pathname)
inproduction-app.js
tries to load JSON data from the same wrong location as well.IMO maybe fixable, but looks like a bigger operation now and the question remains, if a PR could be rejected, because it changes an introduced behavior for everyone …
I stumbled over the same problem, and think I found a) a (very ugly) workaround how to modify your application to not have that trailing slash (redirect) for the root page and b) can imagine that this workaround could be turned into an actual feature in Gatsby that would make this behavior configurable.
Here is my discussion here where I documented my journey: https://github.com/gatsbyjs/gatsby/discussions/36971