Skip to content

Commit

Permalink
feat(gatsby-plugin-netlify): Allow status codes in redirects (#11255) (
Browse files Browse the repository at this point in the history
  • Loading branch information
joostdecock authored and pieh committed Feb 5, 2019
1 parent da0cbab commit 024f6f4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/gatsby-plugin-netlify/README.md
Expand Up @@ -111,15 +111,25 @@ You can validate the `_headers` config through the

You can create redirects using the [`createRedirect`](https://www.gatsbyjs.org/docs/actions/#createRedirect) action.

In addition to the options provided by the Gatsby API, you can pass these options specific to this plugin:

| Attribute | Description |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `force` | Overrides existing content in the path. This is particularly useful for domain alias redirects. See [the Netlify documentation for this option](https://www.netlify.com/docs/redirects/#structured-configuration). |
| `statusCode` | Overrides the HTTP status code which is set to `302` by default or `301` when [`isPermanent`](https://www.gatsbyjs.org/docs/actions/#createRedirect) is `true`. Since Netlify supports custom status codes, you can set one here. For example, `200` for rewrites, or `404` for a custom error page. See [the Netlify documentation for this option](https://www.netlify.com/docs/redirects/#http-status-codes). |

An example:

```javascript
createRedirect({ fromPath: "/old-url", toPath: "/new-url", isPermanent: true })
createRedirect({ fromPath: "/url", toPath: "/zn-CH/url", Language: "zn" })
createRedirect({
fromPath: "/url_that_is/not_pretty",
toPath: "/pretty/url",
statusCode: 200,
})
```

> NOTE: You can pass the `force` option to override existing content in the path. This is particularly useful for domain alias redirects. See the Netlify documentation on this option [here](https://www.netlify.com/docs/redirects/#structured-configuration).
You can also create a `_redirects` file in the `static` folder for the same effect. Any programmatically created redirects will be appended to the file.

```sh
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-plugin-netlify/src/create-redirects.js
Expand Up @@ -21,10 +21,12 @@ export default async function writeRedirectsFile(
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(`!`)

Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby/src/redux/actions.js
Expand Up @@ -1072,16 +1072,20 @@ actions.setPluginStatus = (
* Create a redirect from one page to another. Server redirects don't work out
* of the box. You must have a plugin setup to integrate the redirect data with
* your hosting technology e.g. the [Netlify
* plugin](/packages/gatsby-plugin-netlify/)).
* plugin](/packages/gatsby-plugin-netlify/), or the [Amazon S3
* plugin](/packages/gatsby-plugin-s3/).
*
* @param {Object} redirect Redirect data
* @param {string} redirect.fromPath Any valid URL. Must start with a forward slash
* @param {boolean} redirect.isPermanent This is a permanent redirect; defaults to temporary
* @param {string} redirect.toPath URL of a created page (see `createPage`)
* @param {boolean} redirect.redirectInBrowser Redirects are generally for redirecting legacy URLs to their new configuration. If you can't update your UI for some reason, set `redirectInBrowser` to true and Gatsby will handle redirecting in the client as well.
* @param {boolean} redirect.force (Plugin-specific) Will trigger the redirect even if the `fromPath` matches a piece of content. This is not part of the Gatsby API, but implemented by (some) plugins that configure hosting provider redirects
* @param {number} redirect.statusCode (Plugin-specific) Manually set the HTTP status code. This allows you to create a rewrite (status code 200) or custom error page (status code 404). Note that this will override the `isPermanent` option which also sets the status code. This is not part of the Gatsby API, but implemented by (some) plugins that configure hosting provider redirects
* @example
* createRedirect({ fromPath: '/old-url', toPath: '/new-url', isPermanent: true })
* createRedirect({ fromPath: '/url', toPath: '/zn-CH/url', Language: 'zn' })
* createRedirect({ fromPath: '/not_so-pretty_url', toPath: '/pretty/url', statusCode: 200 })
*/
actions.createRedirect = ({
fromPath,
Expand Down

0 comments on commit 024f6f4

Please sign in to comment.