Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Redirect nested routes to default lang #7135

Merged
merged 3 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import redirects from "./redirects.json"

const exec = util.promisify(child_process.exec)

const commonRedirectProps = {
isPermanent: true,
ignoreCase: true,
force: true,
}

/**
* Markdown isOutdated check
* Parse header ids in markdown file (both translated and english) and compare their info structure.
Expand Down Expand Up @@ -190,13 +196,11 @@ export const createPages: GatsbyNode<any, Context>["createPages"] = async ({
}) => {
const { createPage, createRedirect } = actions

// server side redirects
// custom redirects defined in `redirects.json`
redirects.forEach((redirect) => {
createRedirect({
...commonRedirectProps,
...redirect,
isPermanent: true,
ignoreCase: true,
force: true,
})
})

Expand Down Expand Up @@ -260,6 +264,12 @@ export const createPages: GatsbyNode<any, Context>["createPages"] = async ({
// e.g. English file: "src/content/community/index.md"
// e.g. corresponding German file: "src/content/translations/de/community/index.md"
if (language === defaultLanguage) {
createRedirect({
...commonRedirectProps,
fromPath: slug.slice(3),
toPath: slug,
})

for (const lang of supportedLanguages) {
const splitPath = relativePath.split("/")
splitPath.splice(2, 0, `translations/${lang}`)
Expand Down Expand Up @@ -315,6 +325,14 @@ export const createPages: GatsbyNode<any, Context>["createPages"] = async ({
// we can remove this logic and the `/pages-conditional/` directory.
const outdatedMarkdown = [`eth`, `dapps`, `wallets`, `what-is-ethereum`]
outdatedMarkdown.forEach((page) => {
const originalPath = `/${page}/`

createRedirect({
...commonRedirectProps,
fromPath: originalPath,
toPath: `/${defaultLanguage}${originalPath}`,
})

supportedLanguages.forEach(async (lang) => {
const markdownPath = path.resolve(
`src/content/translations/${lang}/${page}/index.md`
Expand All @@ -326,7 +344,7 @@ export const createPages: GatsbyNode<any, Context>["createPages"] = async ({
page,
lang
)
const originalPath = `/${page}/`

const slug = `/${lang}${originalPath}`

createPage<Context>({
Expand Down Expand Up @@ -356,14 +374,25 @@ export const onCreatePage: GatsbyNode<any, Context>["onCreatePage"] = async ({
page,
actions,
}) => {
const { createPage, deletePage } = actions
const { createPage, deletePage, createRedirect } = actions

const isDefaultLang = page.path.startsWith(`/${defaultLanguage}`)

// 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}`)) {
if (isDefaultLang) {
const path = page.path.slice(3)
createPage({ ...page, path })

createRedirect({
...commonRedirectProps,
fromPath: path,
toPath: page.path,
})

// 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
96 changes: 0 additions & 96 deletions redirects.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,90 +47,14 @@
"fromPath": "/beginners",
"toPath": "/en/what-is-ethereum/"
},
{
"fromPath": "/",
"toPath": "/en/"
},
{
"fromPath": "/what-is-ethereum/",
"toPath": "/en/what-is-ethereum/"
},
{
"fromPath": "/eth/",
"toPath": "/en/eth/"
},
{
"fromPath": "/dapps/",
"toPath": "/en/dapps/"
},
{
"fromPath": "/wallets/",
"toPath": "/en/wallets/"
},
{
"fromPath": "/eth2/",
"toPath": "/en/upgrades/"
},
{
"fromPath": "/upgrades/",
"toPath": "/en/upgrades/"
},
{
"fromPath": "/staking/",
"toPath": "/en/staking/"
},
{
"fromPath": "/learn/",
"toPath": "/en/learn/"
},
{
"fromPath": "/community/",
"toPath": "/en/community/"
},
{
"fromPath": "/build/",
"toPath": "/en/developers/learning-tools/"
},
{
"fromPath": "/developers/",
"toPath": "/en/developers/"
},
{
"fromPath": "/enterprise/",
"toPath": "/en/enterprise/"
},
{
"fromPath": "/whitepaper/",
"toPath": "/en/whitepaper/"
},
{
"fromPath": "/foundation/",
"toPath": "/en/foundation/"
},
{
"fromPath": "/eips/",
"toPath": "/en/eips/"
},
{
"fromPath": "/about/",
"toPath": "/en/about/"
},
{
"fromPath": "/privacy-policy/",
"toPath": "/en/privacy-policy/"
},
{
"fromPath": "/terms-of-use/",
"toPath": "/en/terms-of-use/"
},
{
"fromPath": "/cookie-policy/",
"toPath": "/en/cookie-policy/"
},
{
"fromPath": "/languages/",
"toPath": "/en/languages/"
},
{
"fromPath": "/enterprise/",
"toPath": "/en/enterprise/"
Expand Down Expand Up @@ -171,30 +95,10 @@
"fromPath": "/nft/",
"toPath": "/en/nft/"
},
{
"fromPath": "/nfts/",
"toPath": "/en/nft/"
},
{
"fromPath": "/dao/",
"toPath": "/en/dao/"
},
{
"fromPath": "/daos/",
"toPath": "/en/dao/"
},
{
"fromPath": "/defi/",
"toPath": "/en/defi/"
},
{
"fromPath": "/layer2/",
"toPath": "/en/layer-2/"
},
{
"fromPath": "/*/layer2/",
"toPath": "/:splat/layer-2/"
},
{
"fromPath": "/grants/",
"toPath": "/en/community/grants/"
Expand Down