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.

Unwanted trailing slash on prefixPath's root

See original GitHub issue

Description

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

http://localhost:3000/de

becomes

http://localhost:3000/de/

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:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:10

github_iconTop GitHub Comments

1reaction
4lmcommented, May 18, 2020

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:

VM63:1 GET http://localhost:3000/de/page-data/de/page-data.json 404 (Not Found)
  • is triggered because findPath function in find-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.
VM63:1 GET http://localhost:3000/de/page-data/404.html/page-data.json 404 (Not Found)
  • Goes away if one adds an 404.html in src/
production-app.js:128 Uncaught (in promise) Error: page resources for /de not found. Not rendering React
    at production-app.js:128
  • As far as I can see, this thrown error in production-app.js exists, because publicLoader.loadPage(browserLoc.pathname) in production-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 …

0reactions
janpiocommented, Nov 5, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should You Have a Trailing Slash at the End of URLs? - Ahrefs
A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. But should you...
Read more >
nginx unwanted location redirect with trailing slash - Server Fault
If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass,...
Read more >
Remove leading and trailing slash / in python - Stack Overflow
path to return the current URL in Django, and it is returning /get/category . I need it as get/category (without leading and trailing...
Read more >
React Router - Remove Trailing Slash from URLs
This is a super quick example of how to remove trailing slashes from URLs in React apps that use React Router.
Read more >
gatsby-plugin-remove-trailing-slashes
This plugin removes trailing slashes from your project's paths. For example, yoursite.com/about/ becomes yoursite.com/about .
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