Skip to content

Commit

Permalink
Merge pull request reactjs#47 from yangshun/fix-duplicate-redirect
Browse files Browse the repository at this point in the history
Add check for unique redirect URLs
  • Loading branch information
bvaughn authored Oct 7, 2017
2 parents a55036d + ad9a5c7 commit c4dc0e8
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ exports.modifyWebpackConfig = ({config, stage}) => {
exports.createPages = async ({graphql, boundActionCreators}) => {
const {createPage, createRedirect} = boundActionCreators;

// Used to detect and prevent duplicate redirects
const redirectToSlugMap = {};

const blogTemplate = resolve('./src/templates/blog.js');
const communityTemplate = resolve('./src/templates/community.js');
const docsTemplate = resolve('./src/templates/docs.js');
Expand Down Expand Up @@ -114,13 +117,22 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
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({
fromPath: `/${fromPath}`,
redirectInBrowser: true,
toPath: `/${slug}`,
}),
);
});
});
}
}
});
Expand Down

0 comments on commit c4dc0e8

Please sign in to comment.