Skip to content

Commit

Permalink
Add support for additional/custom Netlify redirect rule criteria (gat…
Browse files Browse the repository at this point in the history
…sbyjs#2890)

* Add support for additional/custom Netlify redirect rule criteria

* Converted plug-in to use rest params

* Added a comment

* Fixed lint issue

* Maybe fixed flaky file-system-specific 'gatsby-remark-code-repls' tests

* Backed out changes to gatsby-remark-code-repls test
  • Loading branch information
bvaughn authored and KyleAMathews committed Nov 14, 2017
1 parent f7df369 commit bea0129
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
32 changes: 30 additions & 2 deletions packages/gatsby-plugin-netlify/src/create-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,36 @@ export default async function writeRedirectsFile(pluginData, redirects) {
// Map redirect data to the format Netlify expects
// https://www.netlify.com/docs/redirects/
redirects = redirects.map(redirect => {
const status = redirect.isPermanent ? 301 : 302
return `${redirect.fromPath} ${redirect.toPath} ${status}`
const {
fromPath,
isPermanent,
redirectInBrowser, // eslint-disable-line no-unused-vars
toPath,
...rest
} = redirect

// The order of the first 3 parameters is significant.
// The order for rest params (key-value pairs) is arbitrary.
const pieces = [
fromPath,
toPath,
isPermanent ? 301 : 302, // Status
]

for (let key in rest) {
const value = rest[key]

if (typeof value === `string` && value.indexOf(` `) >= 0) {
console.warn(
`Invalid redirect value "${value}" specified for key "${key}". ` +
`Values should not contain spaces.`
)
} else {
pieces.push(`${key}=${value}`)
}
}

return pieces.join(` `)
})

let appendToFile = false
Expand Down
7 changes: 5 additions & 2 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,12 +712,14 @@ actions.setPluginStatus = (
* @param {string} redirect.redirectInBrowser Redirects are generally for redirecting legacy URLs to their new configuration. If you can't update your UI for some reason, set `redirectInBrowser` to true and Gatsby will handle redirecting in the client as well.
* @example
* createRedirect({ fromPath: '/old-url', toPath: '/new-url', isPermanent: true })
* createRedirect({ fromPath: '/url', toPath: '/zn-CH/url', Language: 'zn' })
*/
actions.createRedirect = ({
fromPath,
isPermanent = false,
toPath,
redirectInBrowser = false,
toPath,
...rest
}) => {
let pathPrefix = ``
if (store.getState().program.prefixPaths) {
Expand All @@ -729,8 +731,9 @@ actions.createRedirect = ({
payload: {
fromPath: `${pathPrefix}${fromPath}`,
isPermanent,
toPath: `${pathPrefix}${toPath}`,
redirectInBrowser,
toPath: `${pathPrefix}${toPath}`,
...rest,
},
}
}
Expand Down

0 comments on commit bea0129

Please sign in to comment.