-
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:
After 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 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 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? // https://gist.github.com/karolmajta/6aad229b415be43f5e0ec519b144c26e
'use strict';
const pointsToFile = uri => /\/[^/]+\.[^/]+$/.test(uri);
exports.handler = (event, context, callback) => {
// Extract the request from the CloudFront event that is sent to Lambda@Edge
var request = event.Records[0].cf.request;
// Extract the URI from the request
var oldUri = request.uri;
var newUri;
if (!pointsToFile(oldUri) && !oldUri.endsWith('/')) {
const newUri = request.querystring ? `${oldUri}/?${request.querystring}` : `${oldUri}/`;
return callback(null, {
body: '',
status: '301',
statusDescription: 'Moved Permanently',
headers: {
location: [{
key: 'Location',
value: newUri,
}],
}
});
} else {
newUri = oldUri;
}
// Match any '/' that occurs at the end of a URI. Replace it with a default index
newUri = newUri.replace(/\/$/, '\/index.html');
// Log the URI as received by CloudFront and the new URI to be used to fetch from origin
console.log("Old URI: " + oldUri);
console.log("New URI: " + newUri);
// Replace the received URI with the URI that includes the index page
request.uri = newUri;
// Return to CloudFront
return callback(null, request);
}; I know on Netlify there are other options to do tthis. Note: Alternatively 3. Your pages should include a canoncial URL that includes the trailing space I tried both
This show that the URL should have a trailing slash. FYI: I added patch for
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? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 15 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. |
Beta Was this translation helpful? Give feedback.
-
@alexpchin - this is an awesome summarization of a lot of confusing points in the workflow. Kudos for putting all that together 😁 this is something I've been doing a lot of digging on in the last couple of days and I agree, there's no clear prescriptions (which is fine, "let the people choose") but there's no real guidance in any given direction either. 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 🙂 |
Beta Was this translation helpful? Give feedback.
-
Good catch ~! |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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 🙂 |
Beta Was this translation helpful? Give feedback.
-
Hey all! 👋 I'm happy to let you know that we opened an RFC for including standard handling of trailing slashes within Gatsby ( no plugins required! ) - we'd love it if you'd please review it and open questions as you have them. Thanks! |
Beta Was this translation helpful? Give feedback.
@alexpchin - this is an awesome summarization of a lot of confusing points in the workflow. Kudos for putting all that together 😁 this is something I've been doing a lot of digging on in the last couple of days and I agree, there's no clear prescriptions (which is fine, "let the people choose") but there's no real guidance in any given direction either.
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: