-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow redirects from same path when they have different options #18220
Comments
From my understanding this is happening because redirects with the same gatsby/packages/gatsby/src/redux/reducers/redirects.js Lines 8 to 13 in d766bc0
Is there a way to prevent duplicates while allowing redirects with the same |
They're being done this way currently for performance. I'm not sure what would be the best way of achieving the same performance improvement due to the way JS's Maps and Sets work, in that they require a primitive type for comparisons. Unless the data is stringified as an object, but that adds extra JSON serialising / deserialising overhead. |
Could it leave to the user to set an |
I played around with this idea and have a working implementation. Should I open a PR or would something like this need an RFC first? |
My idea was to have it be a map of lists, where the key is the path. That way it can still do decent lookups, and acts as a hash collision when it finds duplicate redirects at a path. |
What would be on the lists? The |
Keys in For reference - This was initial implementation:
and it was allowing for multiple redirects using same |
One problem: it(`returns equal content digests for objects with different property order`, () => {
expect(createContentDigest({ a: 'a', b: 'b' })).toEqual(createContentDigest({ b: 'b', a: 'a' }))
}) This means that the following code would generate two redirect entries with this approach: createRedirect({
fromPath: '/',
toPath: '/en/',
});
createRedirect({
toPath: '/en/',
fromPath: '/',
}); |
Technically, "/" is not a page on this site. All pages are instead prefixed by "fr" or "en" and I've set up redirects from "/" to the appropriate language (this applies to all pages, not just the home page). Unfortunately, there seems to have been a regression in gatsby-plugin-netlify. As brought up in gatsbyjs/gatsby#18220, if multiple redirects are created from the same page, all but the latest will be ignored, even if the redirects have different options. Previously, I was creating the following redirects for each page: /quebec/ /en/quebec/ 302 /quebec/ /fr/quebec/ 302 Language=fr This let me redirect people to French/English based on their language header. However, because of the linked issue, this is no longer possible - only the French redirect is being generated. This is effectively breaking redirects for all non-French speakers. As a temporary fix, I am going to give up on sniffing the user language and instead always redirect to the default language.
When I made the PR that changed redirects to this initially, I tested by serialising the objects to string (and also manually by creating a string). It MASSIVELY impacted performance by turning the entire entry into a string. Having a map of fromPath -> list of entire redirect entries makes the most sense, as performance won’t be impacted too much (unless you have a lot of redirects from the same path) |
Oh, I see. So the check for duplicates would use |
I updated #19048 to do this. |
Description
I'm creating a website with multiple locales and I'd like to have different routes for each locale (e.g.
/en/about
and/pt/about
). I also want the/
route to redirect to the correct locale based on the user's browser language setting.On Netlify this can be achieved with the following
_redirects
file:I'm trying to generate this using
createRedirects
andgatsby-netlify-plugin
like the documentation explains but it's generating only one redirect rule:Steps to reproduce
gatsby-netlify-plugin
togatsby-config.js
.createPages
togatsby-node.js
:gatsby build
.Expected result
The following
_redirect
file should be created on thepublic
folder:Actual result
The following
_redirect
file is created on thepublic
folder:Environment
The text was updated successfully, but these errors were encountered: