Replies
|
Hi, thanks for the issue! What documentation did you find confusing? Can you directly link to it and highlight the portions of it? Gatsby by default creates paths with a trailing slash, everything after that is your personal preference and I don't think we make any recommendations or comments about this. Moreover the documentation should be specific to Gatsby so I don't see the need for explanations about server redirects or SEO implications as there are enough articles on the internet already. The example you cited from the canonical URL plugin just happens to have the trailing slash as that's what Gatsby is creating by default. It doesn't imply that canonical URLs have to have a trailing slash. I'd recommend sticking to one of the two options (non-trailing/trailing) and keeping it consistent everywhere. |
|
@alexpchin - this is an awesome summarization of a lot of confusing points in the workflow. Kudos for putting all that together EDIT April 2021: This post ended up helping a lot of folks so do feel free to give it a read, but if you want to know more and explore the why/how behind the workflow, I explored this further on my blog: https://jonsully.net/blog/trailing-slashes-and-gatsby/ Here's what we know for certain:
So how to we reconcile all of that? Here's what my opinion would be this. Step 0 is to make the decision to unify your site with or without the trailing slash. The goal is to have uniformity and non-duplicated content, that's all. Gatsby generates static files in preparation for a trailing slash. I don't like fighting my tools. My vote is go ahead and use the trailing slash. So, to do that: FirstMake sure your web server of choice is following standard web-server parlance.
Sidenote: I'm a Netlify guy and Netlify's platform supports a feature called Pretty URLs, which enforces all of the above for you. Make sure it's enabled (it is by default, but make some cURL requests to your site to double-ensure). See here for more info. I can't speak for other web hosting platforms, but this "holds to standard web parlance" policy should really be the standard... it's been the standard for web for 20+ years. It should require additional configuration to not do this. You can test your host's behavior yourself though, just make a quick site that's basically an SecondExplicitly use the trailing slash in all usages of the This ensures that once the PWA hydrates and the This + the next step will also prevent the odd 'trailing-slash-flash' that can occur when you reload a page that did / didn't have a trailing slash and it flips to the other way. ThirdInstall {
resolve: `gatsby-plugin-force-trailing-slashes`,
options: {
excludedPaths: [`/404.html`],
},That's it. If you can nail those three steps, your site should be exclusively running in a trailing-slash-only, SEO-friendly, link-sharing-friendly format. Deploy it, click around, refresh to your heart's content, hit it from cURL; all of your pathing should be uniform. The reality is that Gatsby sets us up by default for trailing slashes, but doesn't control any server-level settings and the
That's true, but it doesn't necessarily leave Gatsby users feeling the most satisfied. Gatsby can work without trailing slashes and with.. but you have to know the layers (including the server layer) well to set that up. I hope this provides some basis for how to do that |
|
Hi! We just had the same issue in our Gatsby project: Some URLs had trailing slashes, some not. After doing some search on Google, I landed on this discussion. We got the problem solved, here are our learnings:
To make sure all links have trailing slashes, we did the following:
With those 2 fixed we got consistent URLs into our project, with every link ending with a traling slash. Hope that helps Btw: We also wrote a blog post about our findings. Fell free to read, there are also links to our |
|
There seems to be a documented method of using and forcing trailing slashes - is there anyone who opted to not use them? I've been looking into this after noticing pages hitting 301 redirects. My entire project is fine without them, except for this minor detail. I believe it might be effecting SEO - especially when the canonical is pointing to the real source (rather than the non-trailing slash URL). A couple things worth noting:
I've setup a fork of gatsby-starter-blog that makes an attempt at removing trailing slashes and produces the 301 redirect as seen below via https://github.com/laneparton/trailing-slash-test I've also noticed how Gatsbyjs.com handles this. Using the test repo linked above on Gatsby Cloud, there is no 301 redirect. So there must be something on the host side that fixes this? |
|
Has anybody found a way to only serve from non-trailing slashes? @jon-sully This is important to us due to complexities with our monorepo and using rewrites to proxy non-gatsby URLs. If I add something like this in I end up with the redirect stripping the trailing slash, but then gatsby will still 301 and permanently redirect to the trailing slash. This causes an infinite redirect, and is unusable for us. Anybody have any advice? If not, it seems we will be forced to move away from gatsby and only use next.js. Its important for our SEO to serve from only one route, and for us, we must use a route that does not end in a trailing-slash. |
|
So I've written somewhat extensively about using trailing slashes and it's the approach I (fairly strongly) encourage but if you really prefer using non-trailing slashes I believe it can be done. I'll build a PoC to ensure I know what I'm talking about then post back here |
Insert Link
Loading
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.




I am currently working on a gatsby website and I've been struggling to find the "best practise" and implementation for all things related to trailing slashes:
gatsby-plugin-remove-trailing-slashesandgatsby-plugin-force-trailing-slashesAfter researching, it seems other people are confused as well and I don't feel that the documentation is clear as to what the best practices are and how to go about achieving them. It seems there is an unnecessary amount of out-of-the box learning when it comes to best setup. Also, which bits require server configuration and which bits do not.
Here are my questions:
1. If gatsby pages are generated with a directory structure
about/index.html, so should the URLs have a trailing slash?According to:
If the resource you are linking to is a directory then it should have a trailing slash. As Gatsby, by default creates directories for URLs, then they should include the trailiing slash?
2. Should users setup 301 redirects from no trailing slash to trailing slash?
Accordinig to the previous links, the non-trailing slash URL should have a 301 redirect to the trailinig slash URL.
When setting up, it seemed like the plugin
gatsby-plugin-force-trailing-slasheswould do this for you however it doesn't do the 301 redirect which is a server setup problem.In this project, I am using Cloudfront, so used this function to redirect from no trailing slash to trailiing slash. This is not a trivial thing for someone starting out to work out?
I know on Netlify there are other options to do tthis.
Note: Alternatively
gatsby-plugin-create-page-htmlis a plugin that creates anabout.htmlfile from anabout/index.htmlfile. However, this just means you have two resources for the same content, which I believe isn't good.3. Your pages should include a canoncial URL that includes the trailing space
I tried both
gatsby-plugin-canonical-urlsandgatsby-plugin-react-helmet-canonical-urlsto add canonic URLs to my pages. In the documentation forgatsby-plugin-canonical-urlsit says:This show that the URL should have a trailing slash.
FYI: I added patch for
gatsby-plugin-react-hemet-canonical-urlsto force an option to force a trailing slash:I know these are more of a collection of thoughts but I feel like there are lots of people asking questions related to these issues so the documentation could perhaps be more direct at addressing the solutions?