-
Notifications
You must be signed in to change notification settings - Fork 8
Update useSession to pause before SSR hydration completes
#880
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import type { | |
| LimitToKnownKeys, | ||
| Select, | ||
| } from "@gadgetinc/api-client-core"; | ||
| import { useEffect, useState } from "react"; | ||
| import { useApi } from "../GadgetProvider.js"; | ||
| import { useGet } from "../useGet.js"; | ||
| import type { OptionsType, ReadOperationOptions } from "../utils.js"; | ||
|
|
@@ -61,6 +62,11 @@ export function useSession< | |
| const fallbackApi = useApi(); | ||
| const api = client ?? (fallbackApi as ClientType); | ||
|
|
||
| const [hydrated, setHydrated] = useState(!fallbackApi); | ||
| useEffect(() => { | ||
| setHydrated(true); | ||
| }, []); | ||
|
|
||
| if (api && "currentSession" in api && "session" in api) { | ||
| const { select: selection, ...restOptions } = options ?? ({} as any); | ||
| const { user: userSelect, ...sessionSelect } = selection ?? { user: undefined }; | ||
|
|
@@ -69,6 +75,7 @@ export function useSession< | |
|
|
||
| const opts: any = { | ||
| suspense: true, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this have anything to do with this suspense?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok so after some reading I think the issue is that we have a I think maybe the better solution is to move the suspense boundary closer to where the |
||
| pause: !hydrated, | ||
| select: { | ||
| ...sessionSelection, | ||
| ...(userSelection && { user: userSelection }), | ||
|
|
@@ -79,7 +86,7 @@ export function useSession< | |
| const [{ data: session, error }] = useGet(api.currentSession, opts); | ||
|
|
||
| if (error) throw error; | ||
| if (!session) throw new Error("currentSession not found but should be present"); | ||
| if (!session && !opts?.pause && hydrated) throw new Error("currentSession not found but should be present"); | ||
| // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion | ||
| return typeof client == "undefined" ? session : (session as any); | ||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should all of our hooks pause until hydrated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm - The would feel more correct to have more consistent behaviour.
useSessionhere relies onuseGetwhich could get the samepause until hydratedbehaviour, but thisuseSessionhook would need it too to prevent throwing theno currentSession errorbefore hydration