Skip to content

Commit

Permalink
chore(docs): Wrap proxy redirect with createPages (#37133)
Browse files Browse the repository at this point in the history
Co-authored-by: Lennart <lekoarts@gmail.com>
  • Loading branch information
benrobertsonio and LekoArts committed Dec 1, 2022
1 parent df5e52a commit d8d99d6
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions docs/docs/how-to/cloud/working-with-redirects-and-rewrites.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ For instance, while rebuilding your cooking blog, you might want to move all of

## Prerequisites

- If your Gatsby project doesn't already have a `gatsby-node.js` file, add one at that top level of your project (alongside your `gatsby-config.js`).
- If your Gatsby project doesn't already have a `gatsby-node` file, add one at that top level of your project (alongside your `gatsby-config`).

## Directions

Expand All @@ -30,43 +30,37 @@ exports.createPages = async ({ graphql, actions }) => {

2. Once you've made these changes and deployed the changes through Gatsby Cloud, you should be able to test your changes once the CDN cache has been purged.

**Please note:** Most of the examples below will omit the `createPages` wrapper for the sake of brevity. However, the `createRedirect` examples will still always need to be wrapped with it like shown above.

## Advanced use cases

### Wildcard Path Redirects

In the example above, you've explicitly redirected one of your recipe urls, and after adding a few others, you realize that you won't have time to get all the old urls. So you decide that any other url that uses your old path `blog/recipes/*` should just be redirected to the new `/recipes` path. Here's how you'd handle that:

```js:title=gatsby-node.js
exports.createPages = async ({ graphql, actions }) => {
const { createRedirect } = actions

createRedirect({
fromPath: `/blog/recipes/mouthwatering-lasagna`,
toPath: `/recipes/mouthwatering-lasagna`,
})
createRedirect({
fromPath: `/blog/recipes/mouthwatering-lasagna`,
toPath: `/recipes/mouthwatering-lasagna`,
})

// All your other redirects
// All your other redirects

createRedirect({
fromPath: `/blog/recipes/*`,
toPath: `/recipes`,
})
}
createRedirect({
fromPath: `/blog/recipes/*`,
toPath: `/recipes`,
})
```

### Splat redirects

Extending the wildcard example above, you may have a high degree confidence that all of your old recipes that lived at `/blog/recipes` have been migrated to `/recipe`. In that case, you can use a `*` marker in the toPath to indicate that you want the redirect to include everything after the base url. In that case `/blog/recipes/any-awesome-url-path` would become `/recipes/any-awesome-url-path`. Here's how you'd handle that:

```js:title=gatsby-node.js
exports.createPages = async ({ graphql, actions }) => {
const { createRedirect } = actions

createRedirect({
fromPath: `/blog/recipes/*`,
toPath: `/recipes/*`,
})
}
createRedirect({
fromPath: `/blog/recipes/*`,
toPath: `/recipes/*`,
})
```

### Country based redirects
Expand Down Expand Up @@ -185,6 +179,7 @@ Here's how you could do it for our recipes site using a JSON file:

```js:title=gatsby-node.js
const redirects = require("./redirects.json")

exports.createPages = async ({ graphql, actions }) => {
const { createRedirect } = actions

Expand All @@ -202,11 +197,15 @@ exports.createPages = async ({ graphql, actions }) => {
Whenever you set a statusCode of 200 on createRedirect you create a rewrite or a reverse proxy. If your toPath is a page that already exists as part of your Gatsby site it will render that page. If the toPath is an external URL, then we will proxy that request. Maybe you have one site sitting in front of multiple other domains and you want to use rewrites to proxy the traffic. Imagine awesomesite.com was actually several different sites. Docs, dashboard, and marketing, for example. You could have all traffic start out routed to awesomesite.com and then rewrite to the other sites as needed. Such as:

```js:title=gatsby-node.js
createRedirect({
fromPath: `/docs/*`,
toPath: `https://www.awesomesite.com/docs/*`,
statusCode: 200,
})
exports.createPages = async ({ actions }) => {
const { createRedirect } = actions

createRedirect({
fromPath: `/docs/*`,
toPath: `https://www.awesomesite.com/docs/*`,
statusCode: 200,
})
}
```

The important part here is that you have an external toPath with a complete URL and a statusCode of 200 to indicate the rewrite.
Expand Down

0 comments on commit d8d99d6

Please sign in to comment.