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

gatsby-plugin-netlify does not support rewrites, or setting a different status code #11255

Closed
joostdecock opened this issue Jan 24, 2019 · 2 comments
Labels
help wanted Issue with a clear description that the community can help with.

Comments

@joostdecock
Copy link
Contributor

Summary

The netlify plugin allows you to add redirects programmatically. It does differentiate between permanent and non-permanent redirects (status code 301 or 302) but does not allow any other values.

Netlify redirect rules are as follows:

FROM TO STATUS_CODE[!] [key1=value1] [key2=value2]

The plugin allows you to set all of these except the status code:

  • FROM is set with fromPath
  • TO is set with toPath
  • The exclamation mark is set with force
  • Key value pairs are also supported

But the only way to control the status code is to get it to switch from 301 to 302 by setting isPermanent to true.

Given that this is a netlify-specific plugin, and Netlify supports plenty of status codes, I feel there should be a way to handle this through the plugin. The most common use-cases are:

  • Set status code 200 for rewrites (instead of redirects)
  • Set status code 404 for custom 404 pages

Basic example

Redirect creation is handled here as such:

redirects = redirects.map(redirect => {
    const {
      fromPath,
      isPermanent,
      redirectInBrowser, // eslint-disable-line no-unused-vars
      force,
      toPath,
      ...rest
    } = redirect

    let status = isPermanent ? `301` : `302`

    if (force) status = status.concat(`!`)

    // The order of the first 3 parameters is significant.
    // The order for rest params (key-value pairs) is arbitrary.
    const pieces = [fromPath, toPath, status]

I would propose to add a statusCode that (if it's set) would take precendce over the isPermanent setting. Something like this:

redirects = redirects.map(redirect => {
    const {
      fromPath,
      isPermanent,
      redirectInBrowser, // eslint-disable-line no-unused-vars
      force,
      toPath,
      statusCode,
      ...rest
    } = redirect

    let status = isPermanent ? `301` : `302`
    if (statusCode) status = statusCode

    if (force) status = status.concat(`!`)

    // The order of the first 3 parameters is significant.
    // The order for rest params (key-value pairs) is arbitrary.
    const pieces = [fromPath, toPath, status]

Motivation

If statusCode defaults to FALSE this wouldn't break anything for existing users, yet allow people to use this plugin to fully leverage the Netlify redirects/rewrites system.

Not to mention that unless I'm missing something, this is a rather small change.

@joostdecock joostdecock changed the title gatsby-plugin-netlify Does not support rewrites, or setting a different status code gatsby-plugin-netlify does not support rewrites, or setting a different status code Jan 24, 2019
@sidharthachatterjee
Copy link
Contributor

@joostdecock Makes sense to me

We'd love to a receive a PR adding this. Would you be interested in doing this?

@sidharthachatterjee sidharthachatterjee added the help wanted Issue with a clear description that the community can help with. label Jan 25, 2019
@joostdecock
Copy link
Contributor Author

I am.
The entire mono-repository lerna thing which is new to me. But I'll try to find some time to read up on how development is handled in the Gatsby repo and see if I can get a PR together. Would be nice to learn some new stuff :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issue with a clear description that the community can help with.
Projects
None yet
Development

No branches or pull requests

2 participants