diff --git a/gatsby-node.js b/gatsby-node.js index 9eb81e796..2024d6ed5 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -1,7 +1,7 @@ const path = require(`path`); exports.createPages = async ({ actions, graphql, reporter }) => { - const { createPage } = actions; + const { createPage, createRedirect } = actions; const result = await graphql(` { @@ -11,6 +11,7 @@ exports.createPages = async ({ actions, graphql, reporter }) => { frontmatter { path template + redirects } } } @@ -25,9 +26,22 @@ exports.createPages = async ({ actions, graphql, reporter }) => { } result.data.allMdx.edges.forEach(({ node }) => { + const { frontmatter } = node; + + if (frontmatter.redirects) { + frontmatter.redirects.forEach((fromPath) => { + createRedirect({ + fromPath, + toPath: frontmatter.path, + isPermanent: true, + redirectInBrowser: true, + }); + }); + } + createPage({ - path: node.frontmatter.path, - component: path.resolve(`src/templates/${node.frontmatter.template}.js`), + path: frontmatter.path, + component: path.resolve(`src/templates/${frontmatter.template}.js`), context: {}, // additional data can be passed via context }); });