Skip to content

Commit

Permalink
redirect existing root paths to the corresponding defaultLanguage path
Browse files Browse the repository at this point in the history
  • Loading branch information
pettinarip committed Jul 19, 2022
1 parent b896260 commit ed3ec62
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,27 @@ export const onCreatePage: GatsbyNode<any, Context>["onCreatePage"] = async ({
page,
actions,
}) => {
const { createPage, deletePage } = actions
const { createPage, deletePage, createRedirect } = actions

// create routes without the lang prefix e.g. `/{path}` as our i18n plugin
// only creates `/{lang}/{path}` routes. This is useful on dev env to avoid
// getting a 404 since we don't have server side redirects
if (IS_DEV && page.path.startsWith(`/${defaultLanguage}`)) {
const isDefaultLang = page.path.startsWith(`/${defaultLanguage}`)

if (isDefaultLang) {
const path = page.path.slice(3)
createPage({ ...page, path })

createRedirect({
fromPath: path,
toPath: page.path,
isPermanent: true,
ignoreCase: true,
force: true,
})

// create routes without the lang prefix e.g. `/{path}` as our i18n plugin
// only creates `/{lang}/{path}` routes. This is useful on dev env to avoid
// getting a 404 since we don't have server side redirects
if (IS_DEV) {
createPage({ ...page, path })
}
}

const isTranslated = page.context.locale !== defaultLanguage
Expand Down

0 comments on commit ed3ec62

Please sign in to comment.