T
TanStack3w ago
like-gold

How to pass client data to loader/beforeLoad without URL params?

Hello! Is there a way to pass data from the client to the router (loader or beforeLoad) without using URL params? Right now my URLs look like this: /org/$orgId/projects/... The client should be able to switch between organizations using an org selector and each browser tab can work with a different org. In addition, upon tab reload orgId should be preserved. I’d like to remove the /org/$orgId part from my URLs but I currently rely on it to preload data in some routes (server loaders). I tried with sessionStorage but obviously I cannot access it at server side. I tried router state but it is not kept on page reload. Any ideas for cleaner ways to handle this scenario?? Thank you!
10 Replies
rare-sapphire
rare-sapphire3w ago
hum, this a bit weird of a behaviour honestly, any special reason why you need to hide de org from the url? even with session ( server side ) its tricky because once you open a second tab it will override
sunny-green
sunny-green3w ago
URL params this is still the simplest and most transparent option
like-gold
like-goldOP3w ago
actually there is not a "logical" reason but the URL is getting more and more ids in one single URL. and because an organization is a tenant, I thought it could be a good candidate to hide somewhere else other than the URL
sunny-green
sunny-green3w ago
but you can keep all the ids in some sort of session, and just pass the session name instead of ids in query params (or part of the url) like url /dashboard/x12a82f and x12a82f stands for any set like orgId=123, productId=345, companyId=456 and anything you want in your local storage
like-gold
like-goldOP3w ago
thank you for your response @RSB! would it be possible to match them with their associated routes? for example: /org/$orgId/branches/$branchId/projects/$projectId is a valid path so is /org/$orgId/branches/$branchId or /org/$orgId/branches/$branchId/assets ? I'm using a file/folder routing approach.
rare-sapphire
rare-sapphire3w ago
Like create your own serializer, but yeah, no one looks at url besides us devs 😄 Your URL looks just fine btw, its big but its required, also its transportable, send that to a colegue and your good to go Its often not wroth to be honest, you will get more pain for doing serializes, then unserialize, figure out, etc
sunny-green
sunny-green3w ago
totally agree w nikus
rare-sapphire
rare-sapphire3w ago
You can also make url a bit shorter with alias: o/$orgId/b/$branchId/p/$projectId would look like /o/pedro/1/p/2
like-gold
like-goldOP3w ago
thank you for your insights @nikus and @RSB I'll keep the ids in URLs 👍
sunny-green
sunny-green3w ago
The only reason for creating hashes is general limitation of 8,192 bytes in URLs for some servers 😅

Did you find this page helpful?