Integrated handling of trailing slashes in Gatsby #34205
-
Using this feature2022-FEB-08: This is now released in Gatsby 4.7: https://www.gatsbyjs.com/docs/reference/release-notes/v4.7/#trailingslash-option The goalIntroduce trailing / non-trailing slash management as a baked-in feature of Gatsby ( no plugins required ). What's the pain our users face?
How do users work around this today?
Gatsby users are looking for consistent treatment of URL's for their sites. As of November 2021, Gatsby only provides "forcing URL's to be a particular format" via Plugins ( which means, users must jump through a few hurdles to get consistent URL's in their sites). Okay, so what do we do about this?There are the following flavors of treatment that have been written about extensively in our open source project by community members, and by Gatsby employees alike:
Redirects, and expected behavior from the hosting provider ( e.g. Gatsby Cloud )added to RFC on 2022-Jan-07 To ensure consistency within a given Gatsby site, as well as consistency across the various modes of running your Gatsby site ( e.g. develop, serve, and production ) hosting providers shall apply standard redirect behavior for sites that leverage the new trailingSlash option. The following table not only specifies the redirect scenarios that will occur within gatsby develop, gatsby serve, and on Gatsby Cloud, but also specifies how other hosting providers should apply these redirects, given the settings specified for a given Gatsby site via gatsby-config.js. Consider a page that exists at https://example.org/about/index.html
✨ Gatsby Cloud searches for an appropriate path, and will render the corresponding page if found. It’s highly unlikely that the desired experience is to 404 a visitor when they attempt to access a resource with a different semantic than is configured ( e.g. attempting to access /about instead of /about/ should not 404 the visitor! ). Therefore, Gatsby Cloud makes additional attempts to get the visitor to the desired resource. Why use a 301 redirect? Since the primary use case is that the website owners want all pages to follow a consistent semantic, this is not truly a temporary redirect. Therefore, a 301 seems most appropriate as the applied redirect when the website owners specify While the techniques of implementing the Redirect and Rendered url may vary among providers, the outcome for the site owners and site visitors shall be consistent with the specification above. What are we not including?
ApproachThis feature will be purely additive, and the default behavior will be as Gatsby behaves today ( no opinion on whether you use trailing / non-trailing slashes ).
|
Beta Was this translation helpful? Give feedback.
Replies: 17 comments 27 replies
-
Ah great! Love to see this. Thanks for writing this up @pragmaticpat! I enjoyed our discussion on it some months ago and think this is a great framework to go off of. It's a hard but important issue to tackle given how much it touches!
I love this third option in the API and I think it's exactly necessary for those sites that come from WordPress migrations, have a mix of trailing and non-trailing page variants, and other out-of-the-box builds. Great call out. Would love to get my hands dirty on this one once it fulfills the RFC phase. Thanks again! |
Beta Was this translation helpful? Give feedback.
-
As mentioned in the initial RFC post above, we drew some inspiration from Sveltekit's API for how we'd present the option to users. A nuance here is that we don't explicitly call out the presence of automatic redirects when using the Saying it another way, given the RFC in its current state, it might be possible for a visitor to 404 when requesting For trailingSlash handling to be more "feature complete," perhaps we need to include some form of automated redirects so visitors don't 404 when they directly access a route as I described above ( understanding that some of this really would be best served in the chosen hosting layer ). Prompt to the followers of this RFC
Thanks all! |
Beta Was this translation helpful? Give feedback.
-
You can follow the progress here. |
Beta Was this translation helpful? Give feedback.
-
Hey team 👋 I read through everything a few times and wanted to add my 2c.
Totally agree with this and love the sentiment.
I share your suspicion around how the outcome should feel for the developer — using 'Always' or 'Never' should be a pleasant experience. Classically, the "ah, it just works" moment would be the best, right? That said, I fervently agree that this should be handled in the hosting / server layer. I think setting up some kind of application-layer redirects to compensate for hosting-layer misconfigurations is a rabbit hole full of confusion and bad times 😅. I'd suggest that instead of that sort of magic we instead increase the amount of documentation around "how your selected web-server should behave". And that's not necessarily super a complex thing:
For what it's worth, to my knowledge this is actually the default setup for every web-server I've seen (directories vs. documents is the history of the web) but for whatever reason these defaults get changed or adjusted by vendors or individuals etc. etc. That all said, I think that particular thing (server trailing-slash / directory-document configuration), while outside of what Gatsby can actually control or deal with, is really important to making this whole project work. As an example, if a Gatsby user sets up Gatsby (one way or another) so that Gatsby produces 'named index directories' (e.g. the resulting file is In the case where an end-user hits Obviously that's not Gatsby's fault — the 1,2,3 step controls are all Gatsby can do.. but that doesn't fix the issue or help the dev 😕. Outside of adding docs around how the web-server should be setup in conjunction with using the Finally, I think I know the answer but I just want to call out some behavior that may result of this RFC. If the developer has the directive E.g. the 404 page is basically the only page that lives outside of the trailing slash requirements (which is true today too) since it needs to be a document at |
Beta Was this translation helpful? Give feedback.
-
I'd like to share that we have our first canary available for testing! 🚀 Thank you @LekoArts for leading the charge on this one. |
Beta Was this translation helpful? Give feedback.
-
Taking the canary through its paces. A couple of items to note ( some of which we've already discussed internally, but in the spirit of transparency! ... )
I've asked @LekoArts about the third item above. Any other findings from folks that have tried this feature yet? |
Beta Was this translation helpful? Give feedback.
-
Hey, I probably come late to the party but you'll probably find this resource helpful :) https://github.com/slorber/trailing-slash-guide For what it's worth, we also have 3 values in Docusaurus and so far it seems to suit everyone's need for all hosting providers: https://docusaurus.io/docs/deployment#trailing-slashes Note, for |
Beta Was this translation helpful? Give feedback.
-
I have home page without slash: example.com |
Beta Was this translation helpful? Give feedback.
-
This is now released in Gatsby 4.7: |
Beta Was this translation helpful? Give feedback.
-
After using this with
|
Beta Was this translation helpful? Give feedback.
-
Greetings All 👋 And for starters, a great big thank you for all the hard efforts spent trying to get Gatsby to have more finite control over the trailing slash patterns. It is greatly appreciated. Second, my own personal apologies for not having been more available while working through the direct development / canary process! I had a big voice on the matter; I wish I would've had more time in the development phase to help out more tangibly. I've finally got some time to poke around and am updating my own Gatsby site to 4.7 (okay, 4.8. Y'all release fast! 😆) and wanted to share my notes on the trailing slash stuff. I have a couple of well-Google'd blog articles on the matter I'd like to update to current info as well, so this post will serve as my "findings / results" preceding an update to those blog posts. Going to follow this process on both sides even though I only use trailing slashes. I've had a number of folks email me hoping for better guidance when not using slashes and I'm hoping that this turn-key approach will work great for all of them. This is from the perspective of upgrading a prior Gatsby app to >=4.7 Trailing Slashes EnabledMy steps:
Results
Overall, for the yes-trailing-slash side of things, I'm quite happy with these results. There is still a LARGE dependency on the web-server serving files in a 'proper' way, but I believe most known web hosts either do this by default or can be configured to do so. I'd love to see the docs point it out just a little bigger than it currently is, but I appreciate nonetheless that it currently is and that this RFC maintains a table of expectations. In my opinion, @slorber's examples of how host's handle file structures is the gold-standard for showing and understanding host behavior with regard to serving files. It's actually similar to an illustrated deep-dive I did exclusively on Netlify's platform to show the same ideas. All in all though, the yes-trailing-slashes config + Gatsby Cloud or Netlify hosting (@ defaults) = a good, working, intuitive setup, and that's a win. For a brand new Gatsby app and/or Gatsby user that hasn't dug into the finite components of web-hosting, this is about as turn-key as it gets. And for existing apps that are previously setup to use yes-trailing-slashes through any variety of means suggested previously, I would consider this to be a safe, solid migration forward. 👍 Trailing Slashes DisabledThis will be a bit unique because I have a few My steps:
Results
So, conclusion for the yes-trailing-slash side of things is that we're just one step away from everything working great. The files have to get produced in the right structure / format (not named directories) so that the web-server will respond the right way. I actually had someone email me this morning hoping for an answer on the not-trailing-slash front — I'm hopeful we can clear this one up and have great results on both sides of things! Either way, this has come a long way and I'm really thrilled to see it through. Thank you again to all involved 🙌🏻 |
Beta Was this translation helpful? Give feedback.
-
I'm curious how this is intended to affect functions? doest |
Beta Was this translation helpful? Give feedback.
-
Hello, I wonder now whether this behaviour is due to my set-up, browser behaviour or a bug. Any help would be appreciated. You can also find the repository here.. Thank you |
Beta Was this translation helpful? Give feedback.
-
trailingSlash : "never" is working fine for /about but not working for /about.php |
Beta Was this translation helpful? Give feedback.
-
I have gatsby website that hosted on gatsby cloud. I use new option of gatsby "trailingSlash: "always":
I use default config gatsby-plugin-gatsby-cloud without any options. After Example: But if I use "never" option I get 301 redirect from
I've noticed this problem on many gatsby sites including Reproduction Link |
Beta Was this translation helpful? Give feedback.
-
I got redirected here from #37536. @LekoArts I'm using the trailingSlash never option, but the way this is handled means that my sitemap does not include the canonical URL of the site's homepage. I don't this understand how this can be considered acceptable since the whole purpose of a sitemap is to include the canonical URLs which should be indexed for a site. |
Beta Was this translation helpful? Give feedback.
-
Good day everyone. I could not find a working example online of a Gatsby site hosted on GitHub Pages that works without a trailing slash / no-trailing-slash. For anyone who is looking for one, here are two urls: https://no-trailing-slash.github.io https://randychilau.github.io/gatsby-no-trailing-slash-github-pages Question: Why two urls?
When you go to But if I created a "user" site, then you can see a site with the no-trailing-slash experience from start to finish, so I created a personal account
hope that makes some sense. And the sitemaps: Here is the repo with the code and the overly detailed README: Please feel free to ask me any questions, provide feedback on any of the writings, suggest alternatives, etc. As stated in the README, but bears repeating: Cheers! |
Beta Was this translation helpful? Give feedback.
This is now released in Gatsby 4.7:
https://www.gatsbyjs.com/docs/reference/release-notes/v4.7/#trailingslash-option