Dynamic route aliases #6974
Unanswered
grinkus-adapt
asked this question in
Q&A
Replies: 1 comment 7 replies
-
Unfortunately, React Router 6+, which Remix uses, no longer supports regex in params. So your original solution is the correct one. Get the param from your route, and then use a regex to find the productId or categoryId. If present, return // routes/$sharedCategoryAndProductRoute.tsx
const regex = /_product-(?<productId>.+)|_category-(?<categoryId>.+)/;
export function loader({ params }: LoaderArgs) {
const { sharedCategoryAndProductRoute } = params
const match = regex.exec(sharedCategoryAndProductRoute);
if (match) {
const { productId, categoryId } = match.groups;
if (productId) {
return redirect(`/product/${productId}`);
}
if (categoryId) {
return redirect(`/category/${categoryId}`);
}
}
return new Response('Not Found', { status: 404 });
} |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I'm looking for some help/thoughts.
I'm working on a new version of an ecommerce site where in the old version all URLs were:
/product-title-that-consists-of-some-words_product-productId.html
/category-name_category-categoryId.html
^ The product and category URLs have some descriptive text in them, followed by the important bit, which is the keyword
_category-
or_product-
and then the actual ID.In the new version I'd like to use URLs that are:
/product/productId
/category/categoryId
I'd like to handle the old URL structure and then switch to new URL structure after a few months.
My initial thinking was that maybe I could set up a
/routes/$sharedCategoryAndProductRoute.tsx
and then import all other pages (in this example -- product and category pages), and according to some custom logic export what's needed.That seems... inelegant, though.
I was thinking that maybe it would be possible to use the
routes
func in theremix.config.js
, but I think this would require regex support or something. I'd need to do something like:But the
$1.tsx
there doesn't make much sense either...It's also unclear how to handle dynamic paths, since the routes function handles the path and the filename (i.e. I can handle
/product-123
to point toroutes/products/$productId.tsx
, but there's no way to map the123
from the path to the$productId
.)Maybe like this:
Maybe there's a better way?
Beta Was this translation helpful? Give feedback.
All reactions