diff --git a/packages/gatsby-plugin-netlify/README.md b/packages/gatsby-plugin-netlify/README.md index 21dac7c10ee27..6a3dfbba6b22e 100644 --- a/packages/gatsby-plugin-netlify/README.md +++ b/packages/gatsby-plugin-netlify/README.md @@ -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 diff --git a/packages/gatsby-plugin-netlify/src/create-redirects.js b/packages/gatsby-plugin-netlify/src/create-redirects.js index 29c54cce9ad32..600dc95ebc1c7 100644 --- a/packages/gatsby-plugin-netlify/src/create-redirects.js +++ b/packages/gatsby-plugin-netlify/src/create-redirects.js @@ -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(`!`) diff --git a/packages/gatsby/src/redux/actions.js b/packages/gatsby/src/redux/actions.js index 092b634999806..3cc57eb787ad1 100644 --- a/packages/gatsby/src/redux/actions.js +++ b/packages/gatsby/src/redux/actions.js @@ -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,