Repository navigation
Support for legacy prefix-based route patterns (e.g., /services*) in React Router v6 #14609
Replies: 1 comment
|
You can preserve those public URLs during the migration by handling the legacy prefix in a catch-all route component. This works with v6's supported route syntax; it doesn't require rewriting campaign URLs. For a component-based import { Route, Routes, useLocation } from 'react-router-dom';
import Services from './Services';
import Home from './Home';
import NotFound from './NotFound';
function LegacyFallback() {
const { pathname } = useLocation();
if (pathname.startsWith('/services')) {
return <Services />;
}
return <NotFound />;
}
export function AppRoutes() {
return (
<Routes>
<Route path="/" element={<Home />} />
{/* Keep your other explicit routes here. */}
<Route path="*" element={<LegacyFallback />} />
</Routes>
);
}Substitute your existing components/import paths. The prefix check covers A normal Two limits matter for migration:
This provides a compatibility layer for the existing links while you migrate the route implementation. |
Uh oh!
There was an error while loading. Please reload this page.
We are migrating a large application from React Router v4 to v6 and have encountered an issue with route patterns that use prefix-based matching.
In v4, we used patterns like /services* to match any URL starting with /services (e.g., /services-in-city, /services/foo, /services/foo/bar). These patterns are heavily used in our production application for:
Campaign URLs
Push notifications
Multiple native and web views
React Router v6 removed support for this type of path matching. Currently, only a single wildcard * at the end of the path is supported, and arbitrary regex or prefix-based patterns are no longer allowed.
Problem:
Migrating existing paths directly is not feasible because these paths are already used in many production scenarios.
Changing the paths would break campaigns, push notifications, and other integrations.
There is no direct way in v6 to express “any path starting with /services” with the same semantics as v4.
Request:
Consider adding support for legacy prefix-based routes or a configurable matcher system that preserves backward compatibility for production apps migrating to v6.
All reactions