Skip to content

Redirect to same path with different search querystring #105

@Mobiletainment

Description

@Mobiletainment

Description

I am running into an issue with a guard that redirects to the same path.
What I'd like to do in the guard is to check if the route was called without a search query parameter.
If that's the case, a default search query parameter is applied and next.redirect(samePath?param=new) is invoked.

Steps to reproduce

Basic example:

const requireViewParameter = (to: GuardToRoute, _from: GuardFunctionRouteProps | null, next: Next) => {
    const query = to.location.search;
    if (query.length > 0) {
      return next();
    }

    console.log('No id was was set, redirecting to the default view');
    
    next.redirect({
      ...to.location,
      search: `?id=1`,
    });
};

<GuardProvider guards={[requireViewParameter]}>
    <GuardedRoute exact path={'view'} component={View} />
</GuardProvider>

Open the application and set the path to /view without any query parameters

Expected result

The guard runs and the URL is updated to /view?id=1

Actual result

The guard runs and invokes the redirect, but the new query parameter is not applied. The app stays on /view instead.

Environment

  • OS: Windows 10
  • Browser and its version: Chrome Canary 99.0.4819.0 (Official Build) 64-bit
  • React Router DOM version:
    • "react": "17.0.2"
    • "react-router-dom": "5.3.0"
    • "react-router-guards": "^1.0.2"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions