Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: false, // boolean to turn off the default gatsby js headers (disabled by default, until gzip is fixed for server push)
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),
])
}