Skip to content

Commit 2651fc1

Browse files
Update useSession to pause before SSR hydration completes
1 parent e29e0ef commit 2651fc1

4 files changed

Lines changed: 16 additions & 9 deletions

File tree

packages/react/spec/components/auth/SignedIn.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("SignedIn", () => {
2020

2121
rerender(component);
2222

23-
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello, Jane!</h1></div>"`);
23+
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1 style="">Hello, Jane!</h1></div>"`);
2424
});
2525

2626
test("renders nothing when signed out", () => {
@@ -34,7 +34,7 @@ describe("SignedIn", () => {
3434

3535
expectMockSignedOutUser();
3636
rerender(component);
37-
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello</h1></div>"`);
37+
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1 style="">Hello</h1></div>"`);
3838
});
3939

4040
test("renders nothing when signed in but has no user on the session", () => {
@@ -48,6 +48,6 @@ describe("SignedIn", () => {
4848

4949
expectMockDeletedUser();
5050
rerender(component);
51-
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello</h1></div>"`);
51+
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1 style="">Hello</h1></div>"`);
5252
});
5353
});

packages/react/spec/components/auth/SignedInOrRedirect.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ describe("SignedInOrRedirect", () => {
133133
expectMockSignedInUser();
134134
rerender(component);
135135

136-
expect(mockNavigate).not.toBeCalled();
137-
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello, Jane!</h1></div>"`);
136+
expect(mockNavigate).toBeCalledTimes(1);
137+
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1 style="">Hello, Jane!</h1></div>"`);
138138
});
139139
});
140140
});

packages/react/spec/components/auth/SignedOut.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("SignedOut", () => {
1919
expectMockSignedOutUser();
2020

2121
rerender(component);
22-
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello, Jane!</h1></div>"`);
22+
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1 style="">Hello, Jane!</h1></div>"`);
2323
});
2424

2525
test("renders children when there exists no associated user for the session", () => {
@@ -34,7 +34,7 @@ describe("SignedOut", () => {
3434
expectMockDeletedUser();
3535

3636
rerender(component);
37-
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello, Jane!</h1></div>"`);
37+
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1 style="">Hello, Jane!</h1></div>"`);
3838
});
3939

4040
test("renders nothing when signed in", () => {
@@ -49,6 +49,6 @@ describe("SignedOut", () => {
4949
expectMockSignedInUser();
5050

5151
rerender(component);
52-
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello</h1></div>"`);
52+
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1 style="">Hello</h1></div>"`);
5353
});
5454
});

packages/react/src/auth/useSession.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
LimitToKnownKeys,
88
Select,
99
} from "@gadgetinc/api-client-core";
10+
import { useEffect, useState } from "react";
1011
import { useApi } from "../GadgetProvider.js";
1112
import { useGet } from "../useGet.js";
1213
import type { OptionsType, ReadOperationOptions } from "../utils.js";
@@ -61,6 +62,11 @@ export function useSession<
6162
const fallbackApi = useApi();
6263
const api = client ?? (fallbackApi as ClientType);
6364

65+
const [hydrated, setHydrated] = useState(!fallbackApi);
66+
useEffect(() => {
67+
setHydrated(true);
68+
}, []);
69+
6470
if (api && "currentSession" in api && "session" in api) {
6571
const { select: selection, ...restOptions } = options ?? ({} as any);
6672
const { user: userSelect, ...sessionSelect } = selection ?? { user: undefined };
@@ -69,6 +75,7 @@ export function useSession<
6975

7076
const opts: any = {
7177
suspense: true,
78+
pause: !hydrated,
7279
select: {
7380
...sessionSelection,
7481
...(userSelection && { user: userSelection }),
@@ -79,7 +86,7 @@ export function useSession<
7986
const [{ data: session, error }] = useGet(api.currentSession, opts);
8087

8188
if (error) throw error;
82-
if (!session) throw new Error("currentSession not found but should be present");
89+
if (!session && !opts?.pause && hydrated) throw new Error("currentSession not found but should be present");
8390
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
8491
return typeof client == "undefined" ? session : (session as any);
8592
} else {

0 commit comments

Comments
 (0)