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

Redirect to the same route with different params #55

Open
ghost opened this issue Jan 24, 2021 · 0 comments · May be fixed by #85
Open

Redirect to the same route with different params #55

ghost opened this issue Jan 24, 2021 · 0 comments · May be fixed by #85

Comments

@ghost
Copy link

ghost commented Jan 24, 2021

Description

If I create a guard function that redirects to the same matched route, but with different params, the guard gets stuck in an infinite redirect loop and gives up eventually with an unrelated error.

Steps to reproduce

To the intermediate sample in src/router/index.tsx I added this GuardFunction:

const invalidName = (to, from, next) => {
  const { name } = to.match.params;
  if (name === 'test') {
    next.redirect('/charmander');
  }
};

and modified the GuardedRoute:

<GuardedRoute
            path="/:name"
            exact
            component={Detail}
            guards={[invalidName, waitOneSecond, detailBeforeEnter]}
          />

Expected result

When given the route localhost:3001/test, I will get redirected to localhost:3001/charmander.

Actual result

When we get to the point of resolving the result to render react-router-dom components, matchPath returns true and the cycle continues.

if (pathToMatch && !matchPath(pathToMatch, { path, exact })) {

I'm guessing this was done to avoid infinite redirect loops, but here it's causing one.

Environment

  • OS: macOS Catalina
  • Browser and its version: Chrome 87.0.4280.141
  • React Router DOM version: 5.2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
0 participants