Skip to content
Source
Sign inSign up
TanStack/query

HydrationBoundary takes unknown as the type as `unknown` instead of `DehydratedState` #9323

Send to my agent
Closedpaperclover(GitHub profile, opens in a new tab) opened this issue 4 comments

Describe the bug

https://github.com/TanStack/query/blob/5dd11a3c88ddd06bacc17082e5afdec56a2fe95f/packages/react-query/src/HydrationBoundary.tsx#L14

For a library with so much type safety, this is a bit surprising to see.

A refactor to a utility function code using TanStack React Query accidentally passed { state: DehydratedState, ... } instead of the actual dehydrated state, which silently broke some instances of data prefetching on a production Next.js application.

Your minimal, reproducible example

N/A

Steps to reproduce

import { HydrationBoundary } from "@tanstack/react-query";

const mistaken = { oh_no: true };
const element = <HydrationBoundary state={mistaken}>hello</HydrationBoundary>;

result: no error!


the real world situation that ran into this was we had a helper function serverSidePrefetchQueries which helps us avoid some of Next.js pitfalls regarding server components. the usage looked like this:

const state = await serverSidePrefetchQueries([
    queryThingA,
    queryThingB,
});

return <HydrationBoundary state={state}>{client}</HydrationBoundary>

but then the return signature of our helper changed, and these pre-fetches silently stopped working.

Expected behavior

should not pass type checking

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

  • OS: macOS 15
  • Browser: N/A

Tanstack Query adapter

react-query

TanStack Query version

v4, but issue on v5 as well

TypeScript version

N/A

Additional context

No response

  1. TkDodocommented

    @Ephem do you know why state isn’t typed as DehydratedState ? We make that assumption in code anyways 🤔

  2. TkDodo added labelstypes

  3. I don’t remember any good reason no. I think we might not have had DehydratedState in the beginning? (On my phone on shaky vacation internet so can’t check)

    I think the internal code should try to be defensive and treat this as unknown though, but public type seems like it should for sure be DehydratedState.

  4. TkDodocommented

    @paperclover wanna make a PR to fix this?

  5. @TkDodo opened. I realized while writing that a second issue is that the property is optional, which seems silly because not providing that property makes the component always a no-op. Of course, kept ability to explicitly pass null since that does have a use case.

  6. TkDodo closed this

Sign in to join this conversation.