Skip to content

Commit c6fd4d2

Browse files
committed
Ensure current user is updated
For some reason the localStorage set's in shouldRevalidate are not commited before the clientLoader is run. So run the sessionValid function in both, to ensure currentUser is updated. This does lead to some extra processing, but acceptable cost for now.
1 parent fe766b3 commit c6fd4d2

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

apps/cyberstorm-remix/app/root.tsx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ export async function clientLoader() {
141141
publicEnvVariables.VITE_API_URL,
142142
publicEnvVariables.VITE_COOKIE_DOMAIN
143143
);
144+
145+
// We need to run this here too in addition to the, shouldRevalidate function,
146+
// as for some reason the commits to localStorage are not done before the the clientLoader is run
147+
sessionTools.sessionValid(
148+
publicEnvVariables.VITE_API_URL,
149+
publicEnvVariables.VITE_COOKIE_DOMAIN
150+
);
144151
const currentUser = await sessionTools.getSessionCurrentUser();
145152
const config = sessionTools.getConfig(publicEnvVariables.VITE_API_URL);
146153
return {
@@ -158,22 +165,23 @@ export type RootLoadersType = typeof loader | typeof clientLoader;
158165
export function shouldRevalidate({
159166
defaultShouldRevalidate,
160167
}: ShouldRevalidateFunctionArgs) {
161-
if (defaultShouldRevalidate) return true;
162168
const publicEnvVariables = getPublicEnvVariables([
163169
"VITE_API_URL",
164170
"VITE_COOKIE_DOMAIN",
165171
]);
166-
if (
167-
!sessionValid(
168-
new StorageManager(SESSION_STORAGE_KEY),
169-
publicEnvVariables.VITE_API_URL || "",
170-
publicEnvVariables.VITE_COOKIE_DOMAIN || ""
171-
)
172-
)
173-
return true;
174-
return getSessionStale(new NamespacedStorageManager(SESSION_STORAGE_KEY));
172+
sessionValid(
173+
new StorageManager(SESSION_STORAGE_KEY),
174+
publicEnvVariables.VITE_API_URL || "",
175+
publicEnvVariables.VITE_COOKIE_DOMAIN || ""
176+
);
177+
const sessionIsStale = getSessionStale(
178+
new NamespacedStorageManager(SESSION_STORAGE_KEY)
179+
);
180+
return sessionIsStale || defaultShouldRevalidate;
175181
}
176182

183+
clientLoader.hydrate = true;
184+
177185
export function HydrateFallback() {
178186
return <div style={{ padding: "32px" }}>Loading...</div>;
179187
}

0 commit comments

Comments
 (0)