#Dynamic app routing /cat/cat2/cat3/product

17 messages · Page 1 of 1 (latest)

dark crest
#

Hey, I'm looking to migrate a site to medusa + next 13 with app routing. i want to recreate existing url structure which:

  • is up to three categories deep
  • allows products at category depth 1, 2 or 3
  • if no product as last segment, then show cat index

in the backend categories have a unique handle, e.g. 'sport/tennis/shoes'. products also have a handle, which is unique.

the url would consist of up to 4 segments, and the last segment may, or may not, be a product.

is there a 'simple' way to handle this? i've searched high and low but haven't found an example which fits. any pointers at all would be very much appreciated.

acoustic karmaBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

verbal lily
#
/page.tsx
/[slug1]/page.tsx
/[slug1]/[slug2]/page.tsx
/[slug1]/[slug2]/[slug3]/page.tsx
/[slug1]/[slug2]/[slug3]/[slug4]/page.tsx

why not this?
edit: for better semantics

dark crest
#

that solution wouldn't allow
/category/subcategory/product
or /category/product

verbal lily
#

/category/subcategory/product
would be handled with
/[cat1]/[cat2]/[cat3]/page.tsx

and
/category/product
would be handled with
/[cat1]/[cat2]/page.tsx

So i think it would work

#

can always check first if last dynamic segment is a category or product.
if product then show product details
and if category then show a list

#

does subcategory always belong to a category? and not multiple category?
if so then its very much possible

dark crest
#

maybe i'm overcomplicating things and confusing myself.
what i'm working on at the moment is src/app/(main)/(categories)/[...category]/page.tsx
then testing if last segment is product, if yes then show product. if not, show category. which i guess is essentially the same. it just seems like a bit of a faff, and i assumed it would be a relatively common url format and there may be an 'exemplar' way to do it. i'm very new to next so don't have too much confidence that i know what i'm doing 🤣

verbal lily
#

(..) is a route group. Its purely for organizational purposes and does not affect final route :/

#

so in reality you only achieve this -> /[slug1]/page,tsx

dark crest
#

the [...category] is a catch all, is it not?

verbal lily
#

oh right yeah

dark crest
#

thats how i''m using it and splitting out the params

verbal lily
#

sure you can do that

#

if you choose to use [...folderName] then the logic rests on your hands to separate out cats and products

#

it just seems like a bit of a faff
dont blame yourself, blame the existing system lol

dark crest
#

i'd need to do that isProduct test either way