How to ideally protecting routes with Layout
I was using Solid Start with auth js, everything fine.
But when i look the server log, it was called twice.
What i expect the server fn will call once and use cache for the rest.
Here's my code:
But if this common behavior, it is unnecessary?
2 Replies
But when i look the server log, it was called twice.
query
is a client side mechanism, not a server side one. So the short term cache effect only works client side. On the server things tend to be scoped by request.
When authorize()
is called in getPosts
and getPostsById
it's just going to run again.In fact I wouldn't be comfortable with the in
getPosts
and getPostById
. To start I'd go with something like
Then I'd use locals to associate the session with the RequestEvent
.
And even that will not eliminate repeated execution across requests.
authorize()
, getPostById
, and getPosts()
are still three separate requests which run independently on the server. The preload
just runs them as soon as the cursor hovers over the link to navigate to the route. It's the results of authorize()
, getPostById
, and getPosts()
that query
caches (for a short time) on the client side (i.e. with no impact on the server side).Thank you for making it more clear with the better understanding.
Unfortuntly i've tried and because im using
getSession()
from @auth/core/..
, so didnt really get the benefit from the locals
. ( Might be using the useSession()
from vinxi soon ).
I already used the "no" wrapper query authorize()
, but for testing purpose i found interesting case (for me) when calling both getPostById()
& getPosts()
with the query (🧙♂️) idea at the same time, the log run once n that feels strange.
But i got the point, sorry too much editing (my bad)