Skip to content

Update useSession to pause before SSR hydration completes#880

Closed
MillanWangGadget wants to merge 1 commit into
mainfrom
mill/fixUseSignOutHydrationIssue
Closed

Update useSession to pause before SSR hydration completes#880
MillanWangGadget wants to merge 1 commit into
mainfrom
mill/fixUseSignOutHydrationIssue

Conversation

@MillanWangGadget

Copy link
Copy Markdown
Contributor
  • UPDATE
    • RRv7 apps have been getting bad hydration errors on the default Gadget web app template. This was caused by hooks that used useSession such as useSignOut being used before the hydration completes
    • This PR prevents those errors by pausing the session fetching until the hydration is complete.

Prerelease

"@gadgetinc/react": "https://codeload.github.com/gadget-inc/js-clients/tar.gz/@gadgetinc/react-v0.21.4-gitpkg-a041586f",

🎩 Instructions

please 🎩:

  1. In an SSR app, go into authenticated areas and refresh repeatedly. Observe that there is sometimes a hydration error
  2. Put this prerelease into your apps and go into authenticated areas. Refresh repeatedly to observe that there are not more hydration issues

@MillanWangGadget MillanWangGadget force-pushed the mill/fixUseSignOutHydrationIssue branch from e96b533 to 2651fc1 Compare July 29, 2025 01:16
const fallbackApi = useApi();
const api = client ?? (fallbackApi as ClientType);

const [hydrated, setHydrated] = useState(!fallbackApi);

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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. useSession here relies on useGet which could get the same pause until hydrated behaviour, but this useSession hook would need it too to prevent throwing the no currentSession error before hydration

@@ -69,6 +75,7 @@ export function useSession<

const opts: any = {
suspense: true,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this have anything to do with this suspense?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 <Suspense> boundary at the top level of the app and what happens on hydration is the useSession hook suspends which suspends up to the top level boundary; if before that suspense resolves some other state changes then we will get this hydration error

I think maybe the better solution is to move the suspense boundary closer to where the useSession is called

@infiton

infiton commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

can you think of a way to test this?

@infiton

infiton commented Jul 29, 2025

Copy link
Copy Markdown
Contributor
CleanShot 2025-07-28 at 22 32 12@2x

this is the one you're fixing right?

@MillanWangGadget

Copy link
Copy Markdown
Contributor Author

can you think of a way to test this?

I'm gonna investigate this. I've been testing this by putting the prerelease into prod apps, and repeatedly refreshing the page

this is the one you're fixing right?

Yes. I've only observed that on prod web apps in logged in areas because of useSignOut which relies on useUser -> useSession

@MillanWangGadget

Copy link
Copy Markdown
Contributor Author

Closing this in favour of hiding suppressing the hydration error in the same way that we do for other hydration errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants