Skip to content

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

Merged
merged 1 commit into from
Oct 7, 2017
Merged

Add check for unique redirect URLs #47

merged 1 commit into from
Oct 7, 2017

Conversation

yangshun
Copy link
Contributor

@yangshun yangshun commented Oct 7, 2017

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!

⠁ The following paths are being redirected to different permalinks:
  - docs/animation-ja-JP.html → docs/create-fragment.html, docs/animation.html
  - docs/index.html → docs/accessibility.html, docs/hello-world.html
error Plugin default-site-plugin returned an error


  Error: Found the following redirect_froms to be duplicated:
    - docs/animation-ja-JP.html
    - docs/index.html
  
  - gatsby-node.js:95 Object.exports.createPages
    /Users/yangshun/Developer/reactjs.org/gatsby-node.js:95:11
  
  
  - next_tick.js:188 process._tickCallback
    internal/process/next_tick.js:188:7

@reactjs-bot
Copy link

reactjs-bot commented Oct 7, 2017

Deploy preview ready!

Built with commit ad9a5c7

https://deploy-preview-47--reactjs.netlify.com

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' +
Copy link
Contributor Author

@yangshun yangshun Oct 7, 2017

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.

Copy link
Contributor

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.

@bvaughn bvaughn self-requested a review October 7, 2017 15:21
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' +
Copy link
Contributor

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);
}
Copy link
Contributor

@bvaughn bvaughn Oct 7, 2017

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}`,
-          }),
-        );
+          });
+        });
       }
     }
   });

Copy link
Contributor Author

@yangshun yangshun Oct 7, 2017

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!

@yangshun
Copy link
Contributor Author

yangshun commented Oct 7, 2017

@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 👍

...
⠁ Duplicate redirect detected from "docs/index.html" to:
* docs/accessibility.html
* docs/hello-world.html

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Copy link
Contributor

@bvaughn bvaughn left a 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!

@bvaughn bvaughn merged commit c4dc0e8 into reactjs:master Oct 7, 2017
@yangshun yangshun deleted the fix-duplicate-redirect branch October 7, 2017 16:22
jhonmike pushed a commit to jhonmike/reactjs.org that referenced this pull request Jul 1, 2020
* [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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants