Is beforeLoad expected to run on the server and the client?
I have a setup with a dynamic redirect based on locally stored user preferences (i.e. the server does not know about the redirect). My understanding was, that beforeLoad, like load is isomorphic and runs again on the client during hydration. However, in my testing it seems like, that beforeLoad only runs on the client, if the navigation was initiated on the client. If an URL is loaded for the first time, it looks like beforeLoad only runs on the server. Is this expected? And if yes, how can I put logic that has to run on the client before the actual page gets rendered?
This is a related question for loader: https://discord.com/channels/719702312431386674/1367003233523990538
5 Replies
metropolitan-bronze•4mo ago
same discussion as here https://discord.com/channels/719702312431386674/1368313111295164627/1368635230919065650
Is this expected?yes
And if yes, how can I put logic that has to run on the client before the actual page gets rendered?depends. what does this logic do?
quickest-silverOP•4mo ago
Thanks for the response! This wasn't clear to me.
What I am trying to do: I have project management app and I am storing the last subpage for each project the user was on in local storage (e.g. /proj1/dashboard). When the user goes to /proj1 the next time, I want to redirect them to /proj1/dashboard. As I am storing the last page info client side the loader has to run on the client as well for this to work
metropolitan-bronze•4mo ago
beforeLoad would not have been the correct place to write into local storage really
as it also is executed upon preloads
there are a few ways of solving this
you could define a
hydrate
method in createRouter
to execute logic upon hydrationquickest-silverOP•4mo ago
Just to clarify, I was only reading from local storage and was writing as a side effect in a component.
I will look into hydrate to trigger the side effect! Bigger picture it feels like that this is a missing primitive to have something that always only runs on the client. Basically to have a way to mark a specific beforeLoad or load function to always run on the client, or to definitely run twice (once on the server and once after hydration)
Thank you for your help!
metropolitan-bronze•4mo ago
there is a method that runs on the client, also upon hydration
it's called
context()
we need to document this but for now, this method runs before beforeLoad and loader