Skip to content
New issue

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

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

Already on GitHub? # to your account

Add support for additional/custom Netlify redirect rule criteria #2890

Merged
merged 9 commits into from
Nov 14, 2017
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