Getting the full domain/url server side
I have a multi tenant SaaS where clients CNAME their own domains to my app. I need to get the domain / url they're using in order to get the correct data from the DB server side. How can I do this?
7 Replies
Server side you can try
getRequestEvent
and checking if the Host
header is forwarded with the request
https://docs.solidjs.com/reference/server-utilities/get-request-eventNice, that can get me the domain i.e.
myclientsdomain.com
, but not the full url it seems. The URL/pathname references in the request are all /_server
... which makes sense, but there's no reference to the actual URL that was entered in the browser to get there. i.e. myclientsdomain.com/whichpage/
There's also
event.nativeEvent?.web.url.href
; though the presence of web
isn't guaranteed on nativeEvent
(which is the original H3Event
).
H3 Event ObjectEvent Object - h3
Event object carries an incoming request and context.
oooo now we're cookin.
Only if available
. Hrmm, I'm guessing that means it's only available if the originating request was from a web request vs something internal? Not like it would be unavailable in the next version of H3, or every second thursday or something
Hrmm, event.nativeEvent?.web?.url?.href
does get me the right url initially on the first page load, but any other page navigation it just returns _server
for everything.I don't know your exact setup but I think the rest of the path should be available through the router like useLocation
Given this implementation I guess it's a “best efforts” approach given the information present in the request.
GitHub
h3/src/utils/request.ts at c04c458810e34eb15c1647e1369e7d7ef19f567d...
⚡️ Minimal H(TTP) framework built for high performance and portability - unjs/h3
I'm gettting the full
http://localhost:3000/_server
for a server action. If you are loading route data rather than doing a full page load you are only fetching from a server function.
useLocation
will give you the full client side route but of course you can only get that under the Router
component boundary.