Skip to content

Commit d7abccf

Browse files
committed
feat: Add ability to configure country and language redirects
1 parent aa21da6 commit d7abccf

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/create-redirects.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ export default async function writeRedirectsFile(pluginData: any, redirects: any
1717
`headers`,
1818
`signed`,
1919
`edge_handler`,
20-
`Language`,
21-
`Country`,
20+
])
21+
22+
const NETLIFY_CONDITIONS_ALLOWLIST = new Set([
23+
`language`,
24+
`country`,
2225
])
2326

2427
// Map redirect data to the format Netlify expects
@@ -41,6 +44,20 @@ export default async function writeRedirectsFile(pluginData: any, redirects: any
4144
console.warn(
4245
`Invalid redirect value "${value}" specified for key "${key}". ` + `Values should not contain spaces.`,
4346
)
47+
} else if (key === 'conditions') {
48+
// "conditions" key from Gatsby contains only "language" and "country"
49+
// which need special transformation to match Netlify _redirects
50+
// https://www.gatsbyjs.com/docs/reference/config-files/actions/#createRedirect
51+
52+
for (const conditionKey in value) {
53+
if (NETLIFY_CONDITIONS_ALLOWLIST.has(conditionKey)) {
54+
const conditionValue = Array.isArray(value[conditionKey]) ? value[conditionKey].join(',') : value[conditionKey]
55+
// Gatsby gives us "country", we want "Country"
56+
const conditionName = conditionKey.charAt(0).toUpperCase() + conditionKey.slice(1)
57+
pieces.push(`${conditionName}=${conditionValue}`)
58+
}
59+
}
60+
4461
} else if (NETLIFY_REDIRECT_KEYWORDS_ALLOWLIST.has(key)) {
4562
pieces.push(`${key}=${value}`)
4663
}

0 commit comments

Comments
 (0)