Skip to content

Commit 36742df

Browse files
piehKyleAMathews
authored andcommitted
[gatsby-plugin-netlify] create rewrite rules for pages that use matchPath (#3211)
* [gatsby-plugin-netlify] create rewrite rules for pages that use matchPath * Update README.md
1 parent 8e94362 commit 36742df

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

packages/gatsby-plugin-netlify/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ plugins: [
4242
mergeLinkHeaders: true, // boolean to turn off the default gatsby js headers
4343
mergeCachingHeaders: true, // boolean to turn off the default caching headers
4444
transformHeaders: (headers, path) => headers, // optional transform for manipulating headers under each path (e.g.sorting), etc.
45+
generateMatchPathRewrites: true, // boolean to turn off automatic creation of redirect rules for client only paths
4546
},
4647
},
4748
];
@@ -127,3 +128,5 @@ You can also create a `_redirects` file in the `static` folder for the same affe
127128

128129
You can validate the `_redirects` config through the
129130
[Netlify playground app](https://play.netlify.com/redirects).
131+
132+
Redirect rules are automatically added for [client only paths](/docs/building-apps-with-gatsby/#client-only-routes). If those rules are conflicting with custom rules or if you want to have more control over them you can disable them in [configuration](#configuration) by setting `generateMatchPathRewrites` to `false`.

packages/gatsby-plugin-netlify/src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const DEFAULT_OPTIONS = {
1313
mergeLinkHeaders: true,
1414
mergeCachingHeaders: true,
1515
transformHeaders: _.identity, // optional transform for manipulating headers for sorting, etc
16+
generateMatchPathRewrites: true, // generate rewrites for client only paths
1617
}
1718

1819
export const SECURITY_HEADERS = {

packages/gatsby-plugin-netlify/src/create-redirects.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { HEADER_COMMENT } from "./constants"
22
import { appendFile, exists, readFile, writeFile } from "fs-extra"
33

4-
export default async function writeRedirectsFile(pluginData, redirects) {
4+
export default async function writeRedirectsFile(
5+
pluginData,
6+
redirects,
7+
rewrites
8+
) {
59
const { publicFolder } = pluginData
610

7-
if (!redirects.length) return null
11+
if (!redirects.length && !rewrites.length) return null
812

913
const FILE_PATH = publicFolder(`_redirects`)
1014

@@ -43,6 +47,8 @@ export default async function writeRedirectsFile(pluginData, redirects) {
4347
return pieces.join(` `)
4448
})
4549

50+
rewrites = rewrites.map(({ fromPath, toPath }) => `${fromPath} ${toPath} 200`)
51+
4652
let appendToFile = false
4753

4854
// Websites may also have statically defined redirects
@@ -56,7 +62,7 @@ export default async function writeRedirectsFile(pluginData, redirects) {
5662
}
5763
}
5864

59-
const data = `${HEADER_COMMENT}\n\n${redirects.join(`\n`)}`
65+
const data = `${HEADER_COMMENT}\n\n${[...redirects, ...rewrites].join(`\n`)}`
6066

6167
return appendToFile
6268
? appendFile(FILE_PATH, `\n\n${data}`)

packages/gatsby-plugin-netlify/src/gatsby-node.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,21 @@ exports.onPostBuild = async ({ store, pathPrefix }, userPluginOptions) => {
3535

3636
const { redirects } = store.getState()
3737

38+
let rewrites = []
39+
if (pluginOptions.generateMatchPathRewrites) {
40+
const { pages } = store.getState()
41+
rewrites = pages
42+
.filter(page => page.matchPath && page.matchPath !== page.path)
43+
.map(page => {
44+
return {
45+
fromPath: page.matchPath,
46+
toPath: page.path,
47+
}
48+
})
49+
}
50+
3851
await Promise.all([
3952
buildHeadersProgram(pluginData, pluginOptions),
40-
createRedirects(pluginData, redirects),
53+
createRedirects(pluginData, redirects, rewrites),
4154
])
4255
}

0 commit comments

Comments
 (0)