Skip to content

Commit

Permalink
feat: Add redirects for all mdx pages that specify redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 18, 2020
1 parent b822eab commit 0a9647d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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(`
{
Expand All @@ -11,6 +11,7 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
frontmatter {
path
template
redirects
}
}
}
Expand All @@ -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
});
});
Expand Down

0 comments on commit 0a9647d

Please sign in to comment.