Check if the code runs in CSR or CSR + hydration
Is there a way to check if the code runs during hydration or just normal CSR, the same way you are able to check if you are on the server or clinet using
isServer
or export conditions?
This is mostly about authoring libraries, where you want to run different logic depending on if the env is SSR / CRS / CRS + hydration.
it's related to the issue here: https://github.com/solidjs-community/solid-primitives/issues/31029 Replies
iirc internallly it is
sharedConfig.context
for just CSR, yes you only check if it is isServer
to check if the component is currently hydrating, an internal check for sharedConfig.context
but I might be wrongthx will try that
btw your usePrefersDark in solid-use has the same issue as the one in solid-primitives:
https://stackblitz.com/edit/github-tzjxcu-qtg74p?file=src/components/Test.tsx
StackBlitz
solid-use astro prefers dark - StackBlitz
Run official live example code for Astro Framework Solid, created by Withastro on StackBlitz
(Sorry, realized that I misunderstood while testing)
what was wrong?
I got interested 😅
With the solution?
yeah, with the resouirce
Well it still had the same problem with hydration
ultimately I had
But you end up with the same result anyway
does onHuydrated override the resource value?
ah you changed it to a signal
Weird; adding ssrLoadFrom: "initial" seems to fix that hydration issue
Razboy20
StackBlitz
solid-use astro prefers dark (forked) - StackBlitz
Run official live example code for Astro Framework Solid, created by Withastro on StackBlitz
where you have
same stuff I guess
ah true
it's funny that
ssrLoadFrom: 'initial'
changes anything
when I thought it only prevented to fetch on the serveryeah, I wonder why it triggers tracking
I'm guessing it's because without ssrLoadFrom, the initialvalue isn't actually being used
and possibly initialvalue is not equality checked?
it maybe a bug, the initial value shouldn't be used on the client anyway - it would come from serialized blob, right?
if you have ssrLoadFrom, then initialValue is just serialized/hydrated on the client if you don't refetch
only if you have that enabled?
I thought that without it it would use the value fetched on the server.
I guess thats depending on if it's async / sync rendering
with Astro it's only sync, so the fetch won't be used
it won't even happen on the server
ah
also interesting
You can use the new ssrLoadFrom option for this. Instead of using the default "server" value, you can pass "initial" and the resource will use initialValue as if it were the result of the first fetch for both SSR and hydration.
also here is a solution that doesn't use resource
https://stackblitz.com/edit/github-tzjxcu-xofarw?file=src%2Fcomponents%2FTest.tsx
Damian Tarnawski
StackBlitz
solid-primitives hydration prefers dark - StackBlitz
Run official live example code for Astro Framework Solid, created by Withastro on StackBlitz
resource is fun
but it does way more then I need
I agree
In the opposite line of thought, without using resources, how would you generate a random number serverside, then keep it on the client without making a new one?
e.g. with resources:
it's a tricky one, since this one tackles a bit of resumability. I'm writing a primitive right now to make this happen
that is, a solid-use primitve
use-ful!
xD
Here's the previous idea: https://discord.com/channels/722131463138705510/780502110772658196/1070721305634152488
Also, just wanted to let you know that I submitted a pr to solid-headless for transitions
yes I just saw, I'll take a look later
👍
Interesting, looking forward to seeing the result