Skip to content

[Bug]: createMemoryRouter - Throwing a redirect in a loader/action, now changes the page URL #10010

@adrianbrowning

Description

@adrianbrowning

What version of React Router are you using?

6.8.0

Steps to Reproduce

I've created 2 examples, one that is working (version I've reverted to) and the latest, where it is now broken.

Working (v6.6.2): https://stackblitz.com/edit/react-ts-9g8mr3?file=App.tsx
Broken (v6.8.0): https://stackblitz.com/edit/react-ts-9gbuje?file=App.tsx

  1. Create a brand new project
  2. Install the latest version of react-router-dom (time of writing v6.8.0)
  3. In App.tsx add the following:
import * as React from 'react';
import './style.css';

import {
  RouterProvider,
  createMemoryRouter,
  Link,
  redirect,
  Form
} from 'react-router-dom';

const router = createMemoryRouter(
  [
    {
      path: '/first',
      element: <section>
      <Link to="/throw">Throw Redirect</Link>
      <Form method="post" action="/throw">
        <button type="submit">Post to throw</button>
      </Form>
    </section> 
    },
    {
      path: "/redirect",
      element: <section><p>I see you have found your way here :(</p><Link to="/first">Back to First</Link></section>
    },
    {
      path: '/throw',
      loader: () => {
        throw redirect('/redirect');
      },
      action: () => {
        throw redirect('/redirect');
      },
    },
  ],
  {
    initialEntries: ['/first'],
    initialIndex: 0,
  }
);

export default function App() {
  return (
    <RouterProvider router={router} fallbackElement={<p>Nothing here!</p>} />
  );
}
  1. Once the page is loaded, click Link Throw Redirect and notice the actual URL in the browser has been updated.

Expected Behavior

Like in v6.6.2, the URL should not update when using the memory router option.

Actual Behavior

The current behaviour is the URL is being updated with the thrown path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions