Skip to content
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

feat(v2): warn user when there are conflicting routes #3083

Merged

Conversation

teikjun
Copy link
Contributor

@teikjun teikjun commented Jul 20, 2020

Motivation

Users may accidentally create conflicting routes. This is especially common when the user enables docs-only mode or blog-only mode. This PR adds a warning to inform the user that there are overriding paths when they run yarn start or yarn build. It also adds documentation that explains the warning.

This PR closes #3041.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

  1. In docusaurus.config.js of D2 website, add a line routeBasePath: '/', to the docs-plugin section (line 116).
  2. Run yarn start. There should be 3 warnings.

image

@teikjun teikjun requested a review from yangshun as a code owner July 20, 2020 18:11
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Jul 20, 2020

Deploy preview for docusaurus-2 ready!

Built with commit 04587de

https://deploy-preview-3083--docusaurus-2.netlify.app

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jul 20, 2020
@teikjun teikjun changed the title feat(v2): warn user when routes override feat(v2): warn user when there are conflicting routes Jul 20, 2020
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and it seems to work fine.

However, can you make an implementation where detecting the bad routes is decorrelated from the effect (logging/throwing).

This way you can make the code more testable, and you'd be able to add tests easily on the most important parts.

Can you rework this PR, keeping the same behavior for now, and making the effect configurable with a onDuplicatePaths setting or something?

You can get inspiration from here, as the broken links detection behavior is quite similar to this
https://github.com/facebook/docusaurus/pull/3059/files#diff-e6be3562ac400435b5f35aa3276065b4R1

@slorber
Copy link
Collaborator

slorber commented Jul 21, 2020

Also I think we should extract a helper for this fn. This kind of logic is used 3 times

image

(also used in a different form in loadRoutes to get the routePaths)

@slorber
Copy link
Collaborator

slorber commented Jul 24, 2020

FYI, just remember this code in docs plugin. Maybe we should remove it or this plugin won't report the docs/home conflict correctly?

    async routesLoaded(routes) {
      const homeDocsRoutes = routes.filter(
        (routeConfig) => routeConfig.path === homePageDocsRoutePath,
      );

      // Remove the route for docs home page if there is a page with the same path (i.e. docs).
      if (homeDocsRoutes.length > 1) {
        const docsHomePageRouteIndex = routes.findIndex(
          (route) =>
            route.component === options.docLayoutComponent &&
            route.path === homePageDocsRoutePath,
        );

        delete routes[docsHomePageRouteIndex!];
      }
    },

@teikjun
Copy link
Contributor Author

teikjun commented Jul 24, 2020

FYI, just remember this code in docs plugin. Maybe we should remove it or this plugin won't report the docs/home conflict correctly?

Ah I was just thinking about this, wasn't sure if this code was still needed. Ok, I'll just remove this code in docs plugin 👌

The warning is reported before the routesLoaded lifecycle, so I think the user should still get the correct warning in either case.

@teikjun teikjun marked this pull request as draft July 28, 2020 09:12
@teikjun teikjun marked this pull request as ready for review July 29, 2020 19:44
@teikjun
Copy link
Contributor Author

teikjun commented Jul 29, 2020

I've completed the requested changes :D

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks nice to me, just a few minor changes

packages/docusaurus/src/server/utils.ts Outdated Show resolved Hide resolved
packages/docusaurus-types/src/index.d.ts Outdated Show resolved Hide resolved
packages/docusaurus/src/server/__tests__/utils.test.ts Outdated Show resolved Hide resolved
website/docs/api/docusaurus.config.js.md Outdated Show resolved Hide resolved
@teikjun teikjun marked this pull request as draft July 30, 2020 15:25
@teikjun teikjun marked this pull request as ready for review July 30, 2020 16:59
return flatMap(routeConfig, getFinalRoutes);
}

export function reportMessage(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be used for reporting things other than errors, so I named it in a general way.
I also prepended the type of message (info, warn, error) in front of the body so it looks similar to the other messages that we see in the cli (I've updated the screenshot in the original post)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that looks fine

@teikjun
Copy link
Contributor Author

teikjun commented Jul 30, 2020

Ready for review! :)

@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Jul 31, 2020
@slorber slorber merged commit 300aecb into facebook:master Jul 31, 2020
@slorber
Copy link
Collaborator

slorber commented Jul 31, 2020

great work, thanks :)

Works well on D2 site if I use docs only mode

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn when routes override each others
4 participants