Can I redirect AND update the router context within beforeLoad?

For context:

My server has the following states: uninitialized & initialized
In the
beforeLoad
of the __root.tsx I use TSQuery to get the serverState and inject it into the router context along with the QueryClient.

What I want is to be able to redirect from there depending on the state.

So if uninitialized, all paths would reroute to /initialization (a single page without sub routes). Same for restarting and disconnected.
If initialized, routing would happen normally with a default redirect to /home from the index.

I thought about doing this in the __root.tsx but I can't both update the context via a return AND throw redirects obviously.
I tried updating the context by just setting fields (eg. context.serverState = serverState) but this doesn't work, I get
undefined
in the
beforeLoad
s of subsequent routes when using serverState.

I also tried wrapping everything in a _redirect pathless route which would do the redirects. This works somewhat, but only on legitimate subroutes (so for instance /fubar would not redirect, it would just show a 404). Also, no redirects on the index route which is a problem.

Any ideas ?
Cheers!
Was this page helpful?