Skip to content

Commit

Permalink
[gatsby-plugin-netlify] create rewrite rules for pages that use match…
Browse files Browse the repository at this point in the history
…Path (#3211)

* [gatsby-plugin-netlify] create rewrite rules for pages that use matchPath

* Update README.md
  • Loading branch information
pieh authored and KyleAMathews committed Jan 29, 2018
1 parent 8e94362 commit 36742df
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/gatsby-plugin-netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ plugins: [
mergeLinkHeaders: true, // boolean to turn off the default gatsby js headers
mergeCachingHeaders: true, // boolean to turn off the default caching headers
transformHeaders: (headers, path) => headers, // optional transform for manipulating headers under each path (e.g.sorting), etc.
generateMatchPathRewrites: true, // boolean to turn off automatic creation of redirect rules for client only paths
},
},
];
Expand Down Expand Up @@ -127,3 +128,5 @@ You can also create a `_redirects` file in the `static` folder for the same affe

You can validate the `_redirects` config through the
[Netlify playground app](https://play.netlify.com/redirects).

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`.
1 change: 1 addition & 0 deletions packages/gatsby-plugin-netlify/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const DEFAULT_OPTIONS = {
mergeLinkHeaders: true,
mergeCachingHeaders: true,
transformHeaders: _.identity, // optional transform for manipulating headers for sorting, etc
generateMatchPathRewrites: true, // generate rewrites for client only paths
}

export const SECURITY_HEADERS = {
Expand Down
12 changes: 9 additions & 3 deletions packages/gatsby-plugin-netlify/src/create-redirects.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { HEADER_COMMENT } from "./constants"
import { appendFile, exists, readFile, writeFile } from "fs-extra"

export default async function writeRedirectsFile(pluginData, redirects) {
export default async function writeRedirectsFile(
pluginData,
redirects,
rewrites
) {
const { publicFolder } = pluginData

if (!redirects.length) return null
if (!redirects.length && !rewrites.length) return null

const FILE_PATH = publicFolder(`_redirects`)

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

rewrites = rewrites.map(({ fromPath, toPath }) => `${fromPath} ${toPath} 200`)

let appendToFile = false

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

const data = `${HEADER_COMMENT}\n\n${redirects.join(`\n`)}`
const data = `${HEADER_COMMENT}\n\n${[...redirects, ...rewrites].join(`\n`)}`

return appendToFile
? appendFile(FILE_PATH, `\n\n${data}`)
Expand Down
15 changes: 14 additions & 1 deletion packages/gatsby-plugin-netlify/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,21 @@ exports.onPostBuild = async ({ store, pathPrefix }, userPluginOptions) => {

const { redirects } = store.getState()

let rewrites = []
if (pluginOptions.generateMatchPathRewrites) {
const { pages } = store.getState()
rewrites = pages
.filter(page => page.matchPath && page.matchPath !== page.path)
.map(page => {
return {
fromPath: page.matchPath,
toPath: page.path,
}
})
}

await Promise.all([
buildHeadersProgram(pluginData, pluginOptions),
createRedirects(pluginData, redirects),
createRedirects(pluginData, redirects, rewrites),
])
}

0 comments on commit 36742df

Please sign in to comment.