Browse Source

Add check for unique redirect URLs

main
Yangshun Tay 8 years ago
parent
commit
ad9a5c7403
  1. 18
      gatsby-node.js

18
gatsby-node.js

@ -28,6 +28,9 @@ exports.modifyWebpackConfig = ({config, stage}) => {
exports.createPages = async ({graphql, boundActionCreators}) => { exports.createPages = async ({graphql, boundActionCreators}) => {
const {createPage, createRedirect} = boundActionCreators; const {createPage, createRedirect} = boundActionCreators;
// Used to detect and prevent duplicate redirects
const redirectToSlugMap = {};
const blogTemplate = resolve('./src/templates/blog.js'); const blogTemplate = resolve('./src/templates/blog.js');
const communityTemplate = resolve('./src/templates/community.js'); const communityTemplate = resolve('./src/templates/community.js');
const docsTemplate = resolve('./src/templates/docs.js'); const docsTemplate = resolve('./src/templates/docs.js');
@ -114,13 +117,22 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
redirect = [redirect]; redirect = [redirect];
} }
redirect.forEach(fromPath => redirect.forEach(fromPath => {
if (redirectToSlugMap[fromPath] != null) {
console.error(`Duplicate redirect detected from "${fromPath}" to:\n` +
`* ${redirectToSlugMap[fromPath]}\n` +
`* ${slug}\n`
);
process.exit(1);
}
redirectToSlugMap[fromPath] = slug;
createRedirect({ createRedirect({
fromPath: `/${fromPath}`, fromPath: `/${fromPath}`,
redirectInBrowser: true, redirectInBrowser: true,
toPath: `/${slug}`, toPath: `/${slug}`,
}), });
); });
} }
} }
}); });

Loading…
Cancel
Save