-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Add check for unique redirect URLs #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Deploy preview ready! Built with commit ad9a5c7 |
gatsby-node.js
Outdated
console.error('The following paths are being redirected to different permalinks:\n' + | ||
issues.map(issue => ` - ${issue.fromPath} → ${issue.permalinks.join(', ')}`).join('\n') | ||
); | ||
throw new Error('Found the following redirect_froms to be duplicated:\n' + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might have to change this to process.exit(1)
instead of throwing an error if we want the build to halt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Replacing the throw
with process.exit(1)
seems like the right thing to do.
gatsby-node.js
Outdated
console.error('The following paths are being redirected to different permalinks:\n' + | ||
issues.map(issue => ` - ${issue.fromPath} → ${issue.permalinks.join(', ')}`).join('\n') | ||
); | ||
throw new Error('Found the following redirect_froms to be duplicated:\n' + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Replacing the throw
with process.exit(1)
seems like the right thing to do.
gatsby-node.js
Outdated
fromPath, | ||
permalinks: redirectFromPaths[fromPath], | ||
})).filter(issue => issue.permalinks.length > 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I see your point about the separate function being clearer but it feels a bit "meh" to add another for-each.
What are your thoughts about doing the check inside of createPages
, right before we call createRedirect
? We could still use this technique of redirect-to-slug-map and just fail the first time we saw a key that already existed. This would only warn about the first duplicate (where as your approach warns about all duplicates) but once this check is in place, there should never be duplicates to begin with, so presumably if we found one to warn about- it would be the only one.
For example:
diff --git a/gatsby-node.js b/gatsby-node.js
index ab60095a6..07d93da1b 100644
--- a/gatsby-node.js
+++ b/gatsby-node.js
@@ -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');
@@ -114,13 +117,23 @@ 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}`,
- }),
- );
+ });
+ });
}
}
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get what you mean. I was afraid that it would complicate the createPages
function too much. I'll change it!
@bvaughn Thanks for your feedback and the idea. I literally changed the PR to the code you provided 😂 Now the errors look like this. Pretty clear IMO 👍
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆 well I'm glad that worked out then.
Thanks again!
* [Translate: introducing JSX] First block * [Translate: introducing JSX] Why JSX * [Translate: introducing JSX] Embedding Expressions in JSX * [Translate: introducing JSX] JSX is an Expression Too and Specifying Attributtes with JSX * [Translate: introducing JSX] Everything else * [Translate: introducing JSX] Some suggestions * [Translate: introducing JSX] More suggestions * [Translate: introducing JSX] Add other suggestions
Fixes #1.
It will be faster to do the checks within the redirect registration but I think doing the check in a separate function is cleaner. There aren't too many edges (currently less than 200) so I think performance is not an issue yet.
The build does not fail when errors are thrown because Gatsby catches them I think. I'm not sure how to get around this except using
process.exit()
instead of throwing an error. @bvaughn Any thoughts?The errors are currently printed like this. Comments welcome!