From ed3ec62faf41fdb6131320b0963656c267b406a7 Mon Sep 17 00:00:00 2001 From: Pablo Pettinari Date: Tue, 19 Jul 2022 10:53:15 -0300 Subject: [PATCH] redirect existing root paths to the corresponding defaultLanguage path --- gatsby-node.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/gatsby-node.ts b/gatsby-node.ts index 0e5dd4ce4f38..11f28cbacad4 100644 --- a/gatsby-node.ts +++ b/gatsby-node.ts @@ -356,14 +356,27 @@ export const onCreatePage: GatsbyNode["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