Skip to content

Commit 61c0d81

Browse files
authored
Make redirects more foolproof (#711)
* More foolproof redirects * Also add redirects for the root, eg /react * Fix build issue
1 parent d6f4467 commit 61c0d81

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

gatsby-node.esm.js

+29-10
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ async function createComponentPages({actions, graphql}) {
460460
const railsComponentLayout = path.resolve(__dirname, 'src/layouts/rails-component-layout.tsx')
461461
const figmaComponentLayout = path.resolve(__dirname, 'src/layouts/figma-component-layout.tsx')
462462
const cssComponentLayout = path.resolve(__dirname, 'src/layouts/css-component-layout.tsx')
463+
const redirectLayout = path.resolve(__dirname, 'src/layouts/redirect-layout.tsx')
463464

464465
for (const {slug, frontmatter} of data.allMdx.nodes) {
465466
if (frontmatter.reactId) {
@@ -481,11 +482,20 @@ async function createComponentPages({actions, graphql}) {
481482
}
482483
}
483484

484-
actions.createRedirect({
485-
fromPath: `/${slug}/react/latest`,
486-
toPath: `/${slug}/react/${latestStatusFrom(statuses)}`,
487-
redirectInBrowser: true,
488-
force: true,
485+
actions.createPage({
486+
path: `/${slug}/react/latest`,
487+
component: redirectLayout,
488+
context: {
489+
location: `/${slug}/react/${latestStatusFrom(statuses)}`
490+
}
491+
})
492+
493+
actions.createPage({
494+
path: `/${slug}/react`,
495+
component: redirectLayout,
496+
context: {
497+
location: `/${slug}/react/${latestStatusFrom(statuses)}`
498+
}
489499
})
490500
}
491501

@@ -514,11 +524,20 @@ async function createComponentPages({actions, graphql}) {
514524
})
515525
})
516526

517-
actions.createRedirect({
518-
fromPath: `/${slug}/rails/latest`,
519-
toPath: `/${slug}/rails/${latestStatusFrom(statuses)}`,
520-
redirectInBrowser: true,
521-
force: true,
527+
actions.createPage({
528+
path: `/${slug}/rails/latest`,
529+
component: redirectLayout,
530+
context: {
531+
location: `/${slug}/rails/${latestStatusFrom(statuses)}`
532+
}
533+
})
534+
535+
actions.createPage({
536+
path: `/${slug}/rails`,
537+
component: redirectLayout,
538+
context: {
539+
location: `/${slug}/rails/${latestStatusFrom(statuses)}`
540+
}
522541
})
523542
}
524543

src/layouts/redirect-layout.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import { navigate } from 'gatsby'
3+
4+
export default function RedirectLayout({pageContext}) {
5+
if (typeof window !== "undefined") {
6+
navigate(pageContext.location)
7+
}
8+
9+
return <></>
10+
}

0 commit comments

Comments
 (0)