Skip to content
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

Add support for additional/custom Netlify redirect rule criteria #2890

Merged
merged 9 commits into from
Nov 14, 2017

Conversation

bvaughn
Copy link
Contributor

@bvaughn bvaughn commented Nov 11, 2017

The Netlify redirects documentation has a section titled "GeoIP and Language-based redirects" that outlines how to specify country and language based redirect rules. This PR adds support for these by adding ...rest params to the redirect action and consuming them in the gatsby-plugin-netlify plug-in.

For example, given the following calls:

createRedirect({
  fromPath: '/index.html',
  redirectInBrowser: true,
  toPath: '/',
});
createRedirect({
  fromPath: '/index.html',
  toPath: '/ja/index.html',
  Language: 'ja',
});
createRedirect({
  fromPath: '/index.html',
  toPath: '/zh-CN/index.html',
  Language: 'zh',
  Country: 'CN',
});

The updated plug-in will now generate the following _redirects content:

/index.html  /  302
/index.html  /ja/index.html  302  Language=ja
/index.html  /zh-CN/index.html  302  Language=zh  Country=CN

The plug-in also warns about values with spaces in them (since at least the Netlify plug-in doesn't support them). For example:

createRedirect({
  fromPath: '/foo.html',
  toPath: '/bar.html',
  foo: 'bar baz'
});

Will log the following warning:

Invalid redirect value "bar baz" specified for key "foo". Values should not contain spaces.

Note that this PR will also add the ability to support additional Netlify redirect rules like "Role".

@gatsbybot
Copy link
Collaborator

gatsbybot commented Nov 11, 2017

Deploy preview ready!

Built with commit 23f1527

https://deploy-preview-2890--gatsbygram.netlify.com

@gatsbybot
Copy link
Collaborator

gatsbybot commented Nov 11, 2017

Deploy preview ready!

Built with commit 23f1527

https://deploy-preview-2890--using-drupal.netlify.com

Copy link
Contributor

@Daniel15 Daniel15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth treating the key/value pairs as an array and joining them with ,:

  keyValuePairs: {
    Language: ['zh', 'ja'],
  },

Right now you'd need to know to combine them with a single comma with no space, which is a leaky abstraction:

  keyValuePairs: {
    Language: 'zh,ja',
  },

I think "settings" or "config" is a good name for them. "keyValuePairs" is pretty generic - It explains the data type rather than the purpose 😄

const pieces = [
redirect.fromPath,
redirect.toPath,
redirect.isPermanent ? 301 : 302, // Status
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a side note, for a future enhancement it would be good to support 200, which serves the file without redirecting. A use case is for a simple single-page app where you want all URLs to go to the one actual HTML page. Also, 404, which serves a custom 404 page for the URL (returns the provided HTML file with a 404 status code). Maybe instead of a boolean isRedirect, this could be a mode enum with 'temporaryRedirect', 'permanentRedirect', 'internalRedirect' (200) or 'fileNotFound'? Totally outside the scope of this PR though :)

redirect.isPermanent ? 301 : 302, // Status
]

const keyValuePairs = redirect.keyValuePairs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this settings?

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 11, 2017

I wonder if it's worth treating the key/value pairs as an array and joining them with
Right now you'd need to know to combine them with a single comma with no space, which is a leaky abstraction

Hm. Maybe. I think string-to-string mapping is nice.

The plug-in specifically warns if you provide a value with a space in it, FWIW. As for "leaky" - I think it's reasonable for users to read the documentation if they're writing redirect rules 😉

I think "settings" or "config" is a good name for them. "keyValuePairs" is pretty generic - It explains the data type rather than the purpose 😄

Naming is hard. 🤔 This could be used for lots of things - language, country, role, etc. - so I opted for a name that at least described the shape of the data. I think "settings" and "config" is even more vague than "keyValuePairs". Neither describes the purpose but at least the latter hints at the structure. I dunno. This is subjective. I don't feel super strongly about it.

I think of the whole data structure that's passed to the method as "settings" or "config" though.

@Daniel15
Copy link
Contributor

Okay, I'm convinced (on both points) 😛

What about "extraData" instead of "keyValuePairs"?

oh man naming things is so hard

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 11, 2017

oh man naming things is so hard

Agreed! 😆

I guess I like "extraData" well enough. It's still pretty vague.

I found myself wanting to just use ...rest so we could just do:

createRedirect({
  fromPath: '/index.html',
  toPath: '/zh-CN/index.html',
  Language: 'zh',
  Role: 'admin',
});

But I'm not sure this is intuitive.

@Daniel15
Copy link
Contributor

Oh yeah, that's pretty nice. The other option could be to pass two arguments to createRedirect?

createRedirect({
  fromPath: '/index.html',
  toPath: '/zh-CN/index.html',
}, {
  Language: 'zh',
  Role: 'admin',
});

That looks kinda ugly though.

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 11, 2017

TBH the main reason I didn't use ...rest initially was because I didn't know how to make it work with Kyle's parameter documentation syntax 😅 I think I like it the best of the options we've discussed above.

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 11, 2017

I think I'm going to change it to ...rest and just add an example into the docs.

Thanks for the discussion!

@Daniel15
Copy link
Contributor

Sounds good! :D

@bvaughn bvaughn force-pushed the gatsby-plugin-netlify-redirects branch from 1fef813 to 8cc4ab8 Compare November 13, 2017 15:31
@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 13, 2017

Looks like "AWS CodeBuild" is failing but I don't have permissions to see why. 😄

@KyleAMathews
Copy link
Contributor

Eh sorry about that — it's a WIP thing and doesn't mean anything right now. Rolling out soon though some super cool new integration testing :-) It'll be building/testing every example site on every commit + a bunch of community sites including reactjs.org!

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 13, 2017

That's really exciting 😁

@KyleAMathews
Copy link
Contributor

For the repl tests & appveyor — I put up this PR #2908

We've faced this issue a few times already 🙄

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 13, 2017

Great! I'll back out that single commit then.

@KyleAMathews KyleAMathews merged commit bea0129 into gatsbyjs:master Nov 14, 2017
@KyleAMathews
Copy link
Contributor

Thanks!

@bvaughn
Copy link
Contributor Author

bvaughn commented Nov 14, 2017 via email

@bvaughn bvaughn deleted the gatsby-plugin-netlify-redirects branch November 20, 2017 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants