Skip to content

Commit

Permalink
feat: add redirects for embed pages
Browse files Browse the repository at this point in the history
  • Loading branch information
roadlittledawn committed Oct 12, 2021
1 parent bf5abb9 commit 1ce56bb
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions plugins/gatsby-plugin-embed-pages/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 { data } = await graphql(`
query MyQuery {
Expand All @@ -14,8 +14,8 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
frontmatter {
title
path
redirects
}
body
}
}
}
Expand All @@ -25,8 +25,8 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
const {
frontmatter,
fields: { fileRelativePath, slug },
body,
} = node;
const { redirects } = frontmatter;
const nodePath = frontmatter.path || slug;
const pagePath = path.join(nodePath, 'embed', '/');
const contentSourcePath = nodePath;
Expand All @@ -41,5 +41,23 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
layout: 'EmbedLayout',
},
});

if (redirects) {
redirects.forEach((fromPath) => {
reporter.info(
`Creating redirect for embed page: ${path.join(
fromPath,
'embed',
'/'
)}`
);
createRedirect({
fromPath: path.join(fromPath, 'embed', '/'),
toPath: pagePath,
isPermanent: true,
redirectInBrowser: true,
});
});
}
});
};

0 comments on commit 1ce56bb

Please sign in to comment.