Skip to content

Commit c4dc0e8

Browse files
authored
Merge pull request #47 from yangshun/fix-duplicate-redirect
Add check for unique redirect URLs
2 parents a55036d + ad9a5c7 commit c4dc0e8

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

gatsby-node.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ exports.modifyWebpackConfig = ({config, stage}) => {
2828
exports.createPages = async ({graphql, boundActionCreators}) => {
2929
const {createPage, createRedirect} = boundActionCreators;
3030

31+
// Used to detect and prevent duplicate redirects
32+
const redirectToSlugMap = {};
33+
3134
const blogTemplate = resolve('./src/templates/blog.js');
3235
const communityTemplate = resolve('./src/templates/community.js');
3336
const docsTemplate = resolve('./src/templates/docs.js');
@@ -114,13 +117,22 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
114117
redirect = [redirect];
115118
}
116119

117-
redirect.forEach(fromPath =>
120+
redirect.forEach(fromPath => {
121+
if (redirectToSlugMap[fromPath] != null) {
122+
console.error(`Duplicate redirect detected from "${fromPath}" to:\n` +
123+
`* ${redirectToSlugMap[fromPath]}\n` +
124+
`* ${slug}\n`
125+
);
126+
process.exit(1);
127+
}
128+
129+
redirectToSlugMap[fromPath] = slug;
118130
createRedirect({
119131
fromPath: `/${fromPath}`,
120132
redirectInBrowser: true,
121133
toPath: `/${slug}`,
122-
}),
123-
);
134+
});
135+
});
124136
}
125137
}
126138
});

0 commit comments

Comments
 (0)