Replies: 1 comment
-
This is also an issue with pre-loading. For example, if you have a sidebar and a user hovers over a link, it will run the My current workaround is by using Promise.withResolvers to get the user info in the InnerApp as described in #1668. In your async beforeLoad({ context }) {
const auth = context.auth;
// Wait for the auth state to get resolved inside of InnerApp in main.tsx
await auth.promise;
}, This way you can do your auth checking logic in the InnerApp like // main.tsx -> InnerApp
// This only gets called once and persists between navigation (unless you refresh the page)
useEffect(() => {
const user = getMyUserInfo() // returns User | null
auth.resolve(user);
}, []); Then for protected routes, you can do the following // routes/_dashboard/route.tsx
beforeLoad: async ({ context: { auth } }) => {
const user = await auth.promise;
if (!user) {
throw redirect({
to: "/login",
});
}
}, The |
Beta Was this translation helpful? Give feedback.
-
I have
Inside my
__root.tsx
file I have abeforeLoad
that gets my user info and adds it to context.However, this beforeLoad file is called every time a user navigates to
/a
,/b
or any other route. I would expect the layouts to work like react components, where only the children "rerender" (or reload) when a user navigates around on the page.Is there any way to have the app act like that? For most use cases I only need to grab the data in "beforeLoad" once, not every single time my user navigates around on the page...
Beta Was this translation helpful? Give feedback.
All reactions