Integrated handling of trailing slashes in Gatsby #34205
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! |
|
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! |
|
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
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 |
|
I'd like to share that we have our first canary available for testing! |
|
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? |
|
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 |
Insert Link
Translated from {{ originalLanguageInEnglish }}
Beta Was this translation helpful? Give Feedback
Beta 이 번역이 도움이 되셨나요? 피드백 제공
Beta Esta tradução foi útil? Dê sua opinião
Beta ¿Fue útil esta traducción? Envía tus comentarios

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.




Testing this feature
2022-JAN-07: Current state of #34268 is now published as a canary as alpha-trailing-slash, so you can try by installing
gatsby@alpha-trailing-slash.The goal
Introduce trailing / non-trailing slash management as a baked-in feature of Gatsby ( no plugins required ).
What's the pain our users face?
https://my.domain.io/aboutandhttps://my.domain.io/about/andhttps://my.domain.io/about/index.html, which is possible to produce today with a Gatsby site, then Gatsby users suffer from reduced SEO since the search engine crawler will pick one of those URL's as the canonical URL. This can weaken the search ranking of the related content since it's limited to the traffic that goes to one of the possible URL's.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
301301Why 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
trailingSlashas eitheralwaysornever.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?
Approach
This 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 ).
Introduce the option to force trailing slashes for all routes (including client routes) likely through a config setting in
gatsby-config.js. API example:The
trailingSlashoption would take three options:never,always, andignore.neverremoves all trailing slashes,alwaysadds it, andignoredoesn't automatically change anything and it's in user hands to keep things consistent. (Credit: SvelteKit has the same API)Upon sufficient adoption, and critical bugs are addressed, we remove the flag and make this part of standard Gatsby behavior
Users can remove the corresponding force / remove trailing slash plugins from their Gatsby implementations