Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clerk-react,tanstack): TanStack Start loaders running twice and hydration issues due to Clerk resolved #4838

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/green-files-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/tanstack-start': patch
---

TanStack Start loaders will not run twice due to Clerk handler
5 changes: 5 additions & 0 deletions .changeset/perfect-turtles-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-react': patch
---

ClerkContextProvider uses React.startTransition for state updates fixing hydration issues for SSR
12 changes: 10 additions & 2 deletions packages/react/src/contexts/ClerkContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export function ClerkContextProvider(props: ClerkContextProvider) {
});

React.useEffect(() => {
return clerk.addListener(e => setState({ ...e }));
return clerk.addListener((e: any) => {
React.startTransition(() => {
setState({ ...e });
});
});
}, []);

const derivedState = deriveState(clerkLoaded, state, initialState);
Expand Down Expand Up @@ -100,7 +104,11 @@ const useLoadedIsomorphicClerk = (options: IsomorphicClerkOptions) => {
}, [options.localization]);

React.useEffect(() => {
isomorphicClerk.addOnLoaded(() => setLoaded(true));
isomorphicClerk.addOnLoaded(() => {
React.startTransition(() => {
setLoaded(true);
});
});
}, []);

React.useEffect(() => {
Expand Down
2 changes: 0 additions & 2 deletions packages/tanstack-start/src/server/middlewareHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export function createClerkHandler<TRouter extends AnyRouter>(
router.update({
context: { ...router.options.context, ...clerkInitialState },
});

await router.load();
} catch (error) {
if (error instanceof Response) {
// returning the response
Expand Down
Loading