react-router icon indicating copy to clipboard operation
react-router copied to clipboard

[Bug]: Navigation from '//' pathname does not work; path '*' is not matching the '//' page

Open MateuszGroth opened this issue 1 year ago • 4 comments

What version of React Router are you using?

6.22.3

Steps to Reproduce

Case 1 - Create a router to test * path

<Route
    path="/"
    element={<Outlet />}
  >
     <Route path="*" element={<>Test * Path</>} />
</Route>

Case 2 - Create a router to test 'navigate'

const TestNavigate = () => {
  const navigate = useNavigate();

  useEffect(() => {
    navigate("/test");
  }, [navigate]);

  return <Outlet />;
};

<Route path="/" element={<TestNavigate />}>
    <Route path="*" element={<>Test</>} />
</Route>

Enter the app with '//' path on either case

Expected Behavior

Case 1 - Test * Path should be rendered Case 2 - navigate should work

Actual Behavior

Case 1 - nothing is rendered Case 2 - navigate doesn't work with the following error: TypeError: Failed to construct 'URL': Invalid URL

MateuszGroth avatar Apr 09 '24 10:04 MateuszGroth

Same here. IMO, there's no clean way to handle the error right now.

I tried wrapping in try catch but it does not seem to catch it.

simPod avatar Apr 19 '24 16:04 simPod

This is a URL specification "issue" and not a Remix one.

A path-relative-URL string must be zero or more URL-path-segment strings, separated from each other by U+002F (/), and not start with U+002F (/).

from here

hjonasson avatar May 03 '24 03:05 hjonasson

But nobody said it's Remix 🤔

Adding a stacktrace, it's missing here:

router.js:425 Uncaught (in promise) TypeError: Failed to construct 'URL': Invalid URL
    at Object.createURL (router.js:425:1)
    at createClientSideRequest (router.js:3905:1)
    at startNavigation (router.js:1882:1)
    at Object.initialize (router.js:1550:1)
    at createBrowserRouter (index.js:243:4)
    at reactrouterv6.js:347:1

simPod avatar May 03 '24 07:05 simPod

I have a little workaround, it's not really clean code but for now it works waiting for the issue to be fixed.

Add this in your main.tsx

const doubleSlashRegexp = new RegExp('^//+', 'g');

if (doubleSlashRegexp.test(window.location.pathname)) {
  window.location.pathname = window.location.pathname.replaceAll(doubleSlashRegexp, '/');
}

jpiazzal avatar Jun 17 '24 15:06 jpiazzal