Next.js Discord

Discord Forum

Optional route parameters are not yet supported [[tenant]]

Unanswered
Highlander posted this in #help-forum
Open in Discord
HighlanderOP

43 Replies

dynamic route is [tenant]
catch-all segment is [...tenant]
optional catch-all segment should be [[...tenant]]

can you share your path and what are you trying to achieve?
HighlanderOP
if you've tried sveltekit we can use [[tenant]] where tenant is optional. The use case is:

i'm planning to have a dashboard with multi tenant/brands configuration. So when i visit /dashboard, i get my default tenant, but if i visit /dashboard/my-tenant, i get my-tenant dashboard.

Along with this, i'd also need to handle workspaces so /dashboard/my-tenant/[[workspace]] should give me:
- if workspace is not provided, i get my default workspace ( from session )
- if workspace is provided, i get dashboard for my xyz workspace considering my url is /dashboard/my-tenant/xyz
and yes, this is possible with sveltekit, i've tried once. So it's not like "impossible in this world" thing. IDK why nextjs is unable to do this
waiting since nextjs 13 but all this time, no work on this
@chisto dynamic route is `[tenant]` catch-all segment is `[...tenant]` optional catch-all segment should be `[[...tenant]]` can you share your path and what are you trying to achieve?
HighlanderOP
if i do so then i won't be able to handle rest of the route as [[...tenant]] or [...tenant] wont have rest of the page.
so no /dashboard/[[...tenant]]/settings/page.tsx or /dashboard/[...tenant]/settings/page.tsx, so there's no use of catch-all setup
HighlanderOP
@Asiatic Lion hi
this is what i want
i've also found that vercel is using ~, so if ~ is my tenant from url, i can get the active tenant from session
but i feel this is kinda hacky\
and, i'm not sure but if i do so then i'd need to add this logic everywhere:
if tenant==="~":
fetch from session
else:
get from tenant slug provided from the url
sorry for python syntax, lol
is there any news about [[optionalParam]] support on nextjs?
Asiatic Lion
just to confirm you want /dashboard, /dashboard:tenant and /dashboard/:tenant/:workspace to all work with session fallback when :tentant or :workspace is missing?
HighlanderOP
yes.
and i know i can do redirects from /dashboard/page.tsx, but i wonder if it's good or nt
not*
Asiatic Lion
what is the issue with [[...segments]]
HighlanderOP
if i use this, then i can't do this right:
/dashboard/[[...segments]]/another-page/page.tsx
[Error [TurbopackInternalError]: Invalid segment Static("test"), catch all segment must be the last segment modifying the path (segments: [Static("dashboard"), OptionalCatchAll("tenant")])

Debug info:
- Execution of get_entrypoints_with_issues_operation failed
- Execution of EntrypointsOperation::new failed
- Execution of Project::entrypoints failed
- Execution of AppProject::routes failed
- Execution of directory_tree_to_entrypoints_internal failed
- Execution of directory_tree_to_entrypoints_internal failed
- Execution of directory_tree_to_entrypoints_internal failed
- Execution of directory_tree_to_loader_tree failed
- Invalid segment Static("test"), catch all segment must be the last segment modifying the path (segments: [Static("dashboard"), OptionalCatchAll("tenant")])]
Asiatic Lion
true that is a limitation but you could use client side routing or middleware to rewrite paths like /dashboard > /dashboard/default-tenant/default-workspace
middleware solution will do the trick as far as i am aware
then you can structure it like
/dashboard/[tenant]/[workspace]/settings/page.tsx
HighlanderOP
i followed vercel routing style:
currently what i did is:
when i goto /dashboard ( only this route )
then i just redirect to /dashboard/~/ and inside /dashboard/[tenant]/...other pages, i've created a fn that gets current tenant details.
if the slug is '~` then i get tenantId from session and return tenant detail
else i get tenant details from slug and return the detail
is it good?
@Asiatic Lion then you can structure it like /dashboard/[tenant]/[workspace]/settings/page.tsx
HighlanderOP
yes, this is what i'm doing on folder structure + the logic i've explained above
Asiatic Lion
honestly, that’s a solid and pragmatic approach
only tradeoff is you'll need to check for ~ in every route that uses [tenant] but if that fits your needs then it is good to do
HighlanderOP
i just stole idea from vercel website 😅
Asiatic Lion
smart
and if you do want to remove ~ you can use middleware
@Asiatic Lion only tradeoff is you'll need to check for ~ in every route that uses [tenant] but if that fits your needs then it is good to do
HighlanderOP
yes, that's why i feel a bit wierd, but i think "if it works, dont touch"
@Asiatic Lion and if you do want to remove ~ you can use middleware
HighlanderOP
wow, i didn't think about this, thanks
Asiatic Lion
don't fix something that doesn't need fixed 😂
HighlanderOP
then i'd get exactly what i need
thanks, i didn't consider middleware and rewriting, lol. This just completes the last piece of my puzzle
thanks..
Asiatic Lion
happy to help goodluck
HighlanderOP
@Asiatic Lion hi again.. not that there's issue using
/dashboard/~/another-page

But both redirects and rewrite doesn't allow me to map
/dashboard/~/:path/
to /dashboard/:path
Do you have any suggestions
Asiatic Lion
ah wait gotcha - yeah, ~ works fine for logic, but the problem is you can’t rewrite /dashboard/~/something to /dashboard/some-tenant/something

but you can handle ~ logic inside your route handlers (like you’re doing now)

or go all in on middleware, where you read session, rewrite the full path early, and route as if ~ never existed
let me double check though I could be mistaken
HighlanderOP
yes, i just don't like using ~ in my url, so it's either /dashboard/tenant or /dashboard with default tenant
HighlanderOP
Hii 👋 @Asiatic Lion did you get some ideas on this?