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
  • Loading branch information
pieh committed Dec 14, 2017
1 parent 99e3249 commit 0402b78
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 11 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,10 @@ 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 +64,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
11 changes: 9 additions & 2 deletions packages/gatsby-plugin-netlify/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ exports.onPostBuild = async ({ store, pathPrefix }, userPluginOptions) => {
const pluginData = makePluginData(store, assetsManifest, pathPrefix)
const pluginOptions = { ...DEFAULT_OPTIONS, ...userPluginOptions }

const { redirects } = store.getState()
const { redirects, pages } = store.getState()

const rewrites = pages
.filter(page => page.matchPath && page.matchPath !== page.path)
.map(page => ({
fromPath: page.matchPath,
toPath: page.path,
}))

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

0 comments on commit 0402b78

Please sign in to comment.