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: no hydration when new promise comes in #8383

Merged
merged 35 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
050c9d5
add failing repro test
juliusmarminge Dec 2, 2024
524f5ff
update assertinos
juliusmarminge Dec 2, 2024
cf37452
add logg
juliusmarminge Dec 2, 2024
d82cfb2
ehm - maybe fix?
juliusmarminge Dec 2, 2024
6eaf6fc
rm -only
juliusmarminge Dec 2, 2024
8c7ccf2
make example
juliusmarminge Dec 2, 2024
4efa642
upd
juliusmarminge Dec 2, 2024
f32f6e3
ad debug logs
juliusmarminge Dec 2, 2024
77362c4
more debugging
juliusmarminge Dec 2, 2024
5fa1a33
push
juliusmarminge Dec 3, 2024
7eec72d
maybe???
juliusmarminge Dec 3, 2024
edda7ba
rm log
juliusmarminge Dec 3, 2024
85ee5bc
revert
juliusmarminge Dec 3, 2024
4d9645b
Merge branch 'main' into julius/hydrate-promise
TkDodo Jan 10, 2025
a6d28f5
Merge branch 'main' into julius/hydrate-promise
TkDodo Jan 10, 2025
fb114b9
Merge branch 'main' into julius/hydrate-promise
juliusmarminge Jan 12, 2025
c5eccf5
fix: ?
TkDodo Jan 12, 2025
6dbdf34
fix: check for pending status again
TkDodo Jan 12, 2025
3c1b221
fix: clear serverQueryClient between "requests"
TkDodo Jan 12, 2025
c691765
Merge branch 'main' into julius/hydrate-promise
TkDodo Jan 15, 2025
b7d9755
chore: remove logs
TkDodo Jan 15, 2025
9d3b116
rethrow next build error
juliusmarminge Jan 26, 2025
9ec623a
Merge branch 'main' into julius/hydrate-promise
juliusmarminge Jan 26, 2025
9ebf869
kick off ci again
juliusmarminge Jan 26, 2025
7584928
add `shouldRedactError` option
juliusmarminge Jan 26, 2025
cef39db
Merge branch 'main' into julius/hydrate-promise
juliusmarminge Feb 15, 2025
a346f39
pluralize
juliusmarminge Feb 15, 2025
8448f7b
docs
juliusmarminge Feb 15, 2025
71a4453
chore: more memory
TkDodo Feb 16, 2025
9f555a2
Merge branch 'main' into julius/hydrate-promise
TkDodo Feb 16, 2025
4239947
chore: revert more memory
TkDodo Feb 16, 2025
9ef516c
don't compare statuses if they don't exist
juliusmarminge Feb 17, 2025
a1d1c91
ci: apply automated fixes
autofix-ci[bot] Feb 17, 2025
4c1dc96
Merge branch 'main' into julius/hydrate-promise
TkDodo Feb 17, 2025
8c2023e
lint
juliusmarminge Feb 17, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions integrations/react-next-15/app/_action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use server'

import { revalidatePath } from 'next/cache'
import { countRef } from './make-query-client'

export async function queryExampleAction() {
await Promise.resolve()
countRef.current++
revalidatePath('/', 'page')
return undefined
}
8 changes: 6 additions & 2 deletions integrations/react-next-15/app/client-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ export function ClientComponent() {
const query = useQuery({
queryKey: ['data'],
queryFn: async () => {
await new Promise((r) => setTimeout(r, 1000))
const { count } = await (
await fetch('http://localhost:3000/count')
).json()

return {
text: 'data from client',
date: Temporal.PlainDate.from('2023-01-01'),
count,
}
},
})
Expand All @@ -26,7 +30,7 @@ export function ClientComponent() {

return (
<div>
{query.data.text} - {query.data.date.toJSON()}
{query.data.text} - {query.data.date.toJSON()} - {query.data.count}
</div>
)
}
5 changes: 5 additions & 0 deletions integrations/react-next-15/app/count/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { countRef } from '../make-query-client'

export const GET = () => {
return Response.json({ count: countRef.current })
}
21 changes: 16 additions & 5 deletions integrations/react-next-15/app/make-query-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const plainDate = {
test: (v) => v instanceof Temporal.PlainDate,
} satisfies TsonType<Temporal.PlainDate, string>

export const countRef = {
current: 0,
}

export const tson = createTson({
types: [plainDate],
})
Expand All @@ -22,16 +26,23 @@ export function makeQueryClient() {
* Called when the query is rebuilt from a prefetched
* promise, before the query data is put into the cache.
*/
deserializeData: tson.deserialize,
deserializeData: (data) => {
return tson.deserialize(data)
},
},
queries: {
staleTime: 60 * 1000,
},
dehydrate: {
serializeData: tson.serialize,
shouldDehydrateQuery: (query) =>
defaultShouldDehydrateQuery(query) ||
query.state.status === 'pending',
serializeData: (data) => {
return tson.serialize(data)
},
shouldDehydrateQuery: (query) => {
return (
defaultShouldDehydrateQuery(query) ||
query.state.status === 'pending'
)
},
},
},
})
Expand Down
25 changes: 18 additions & 7 deletions integrations/react-next-15/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import { headers } from 'next/headers'
import React from 'react'
import { HydrationBoundary, dehydrate } from '@tanstack/react-query'
import { Temporal } from '@js-temporal/polyfill'
import { ClientComponent } from './client-component'
import { makeQueryClient, tson } from './make-query-client'
import { makeQueryClient } from './make-query-client'
import { queryExampleAction } from './_action'

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

export default async function Home() {
export default function Home() {
const queryClient = makeQueryClient()

void queryClient.prefetchQuery({
queryClient.prefetchQuery({
queryKey: ['data'],
queryFn: async () => {
await sleep(2000)
const { count } = await (
await fetch('http://localhost:3000/count', {
headers: await headers(),
})
).json()

return {
text: 'data from server',
date: Temporal.PlainDate.from('2024-01-01'),
count,
}
},
})

const state = dehydrate(queryClient)

return (
<main>
<HydrationBoundary state={dehydrate(queryClient)}>
<HydrationBoundary state={state}>
<ClientComponent />
</HydrationBoundary>
<form action={queryExampleAction}>
<button type="submit">Increment</button>
</form>
</main>
)
}
31 changes: 27 additions & 4 deletions integrations/react-next-15/app/providers.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
// In Next.js, this file would be called: app/providers.tsx
'use client'
import { QueryClientProvider } from '@tanstack/react-query'

// Since QueryClientProvider relies on useContext under the hood, we have to put 'use client' on top
import { QueryClientProvider, isServer } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import * as React from 'react'
import type { QueryClient } from '@tanstack/react-query'
import { makeQueryClient } from '@/app/make-query-client'

let browserQueryClient: QueryClient | undefined = undefined

function getQueryClient() {
if (isServer) {
// Server: always make a new query client
return makeQueryClient()
} else {
// Browser: make a new query client if we don't already have one
// This is very important, so we don't re-make a new client if React
// suspends during the initial render. This may not be needed if we
// have a suspense boundary BELOW the creation of the query client
if (!browserQueryClient) browserQueryClient = makeQueryClient()
return browserQueryClient
}
}

export default function Providers({ children }: { children: React.ReactNode }) {
const [queryClient] = React.useState(() => makeQueryClient())
// NOTE: Avoid useState when initializing the query client if you don't
// have a suspense boundary between this and the code that may
// suspend because React will throw away the client on the initial
// render if it suspends and there is no boundary
const queryClient = getQueryClient()

return (
<QueryClientProvider client={queryClient}>
{children}
<ReactQueryDevtools />
{<ReactQueryDevtools />}
</QueryClientProvider>
)
}
3 changes: 2 additions & 1 deletion packages/query-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"test:lib": "vitest",
"test:lib:dev": "pnpm run test:lib --watch",
"test:build": "publint --strict && attw --pack",
"build": "tsup"
"build": "tsup",
"build:dev": "tsup --watch"
},
"type": "module",
"types": "build/legacy/index.d.ts",
Expand Down
76 changes: 76 additions & 0 deletions packages/query-core/src/__tests__/hydration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1066,4 +1066,80 @@ describe('dehydration and rehydration', () => {
clientQueryClient.clear()
serverQueryClient.clear()
})

test('should overwrite data when a new promise is streamed in', async () => {
const serializeDataMock = vi.fn((data: any) => data)
const deserializeDataMock = vi.fn((data: any) => data)

const countRef = { current: 0 }
// --- server ---
const serverQueryClient = createQueryClient({
defaultOptions: {
dehydrate: {
shouldDehydrateQuery: () => true,
serializeData: serializeDataMock,
},
},
})

const query = {
queryKey: ['data'],
queryFn: async () => {
await sleep(10)
return countRef.current
},
}

const promise = serverQueryClient.prefetchQuery(query)

let dehydrated = dehydrate(serverQueryClient)

// --- client ---

const clientQueryClient = createQueryClient({
defaultOptions: {
hydrate: {
deserializeData: deserializeDataMock,
},
},
})

hydrate(clientQueryClient, dehydrated)

await promise
await waitFor(() =>
expect(clientQueryClient.getQueryData(query.queryKey)).toBe(0),
)

expect(serializeDataMock).toHaveBeenCalledTimes(1)
expect(serializeDataMock).toHaveBeenCalledWith(0)

expect(deserializeDataMock).toHaveBeenCalledTimes(1)
expect(deserializeDataMock).toHaveBeenCalledWith(0)

// --- server ---
countRef.current++
serverQueryClient.clear()
const promise2 = serverQueryClient.prefetchQuery(query)

dehydrated = dehydrate(serverQueryClient)

// --- client ---

hydrate(clientQueryClient, dehydrated)

await promise2
await waitFor(() =>
expect(clientQueryClient.getQueryData(query.queryKey)).toBe(1),
)

expect(serializeDataMock).toHaveBeenCalledTimes(2)
expect(serializeDataMock).toHaveBeenCalledWith(1)

expect(deserializeDataMock).toHaveBeenCalledTimes(2)
expect(deserializeDataMock).toHaveBeenCalledWith(1)

clientQueryClient.clear()
serverQueryClient.clear()
})
})
3 changes: 3 additions & 0 deletions packages/query-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ function dehydrateQuery(
queryHash: query.queryHash,
...(query.state.status === 'pending' && {
promise: query.promise?.then(serializeData).catch((error) => {
if ('digest' in error && error.digest === 'DYNAMIC_SERVER_USAGE') {
return Promise.reject(error)
}
if (process.env.NODE_ENV !== 'production') {
console.error(
`A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`,
Expand Down
1 change: 1 addition & 0 deletions packages/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"test:build": "publint --strict && attw --pack",
"build": "pnpm build:tsup && pnpm build:codemods",
"build:tsup": "tsup",
"build:tsup:dev": "tsup --watch",
"build:codemods": "cpy ../query-codemods/* ./build/codemods"
},
"type": "module",
Expand Down
5 changes: 4 additions & 1 deletion packages/react-query/src/HydrationBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export const HydrationBoundary = ({
} else {
const hydrationIsNewer =
dehydratedQuery.state.dataUpdatedAt >
existingQuery.state.dataUpdatedAt
existingQuery.state.dataUpdatedAt ||
// @ts-expect-error
dehydratedQuery.promise?.status !== existingQuery.promise?.status
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this might actually be working

Copy link
Collaborator

@Ephem Ephem Jan 10, 2025

Choose a reason for hiding this comment

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

What if we prefetch a query without await in a page that is static or cached, either fully or partially, and then client navigate to it with newer data in the cache? My guess is that there are/can be situations where we get a promise that will resolve to data that is actually older than the one we have on the client already?

Maybe the entirely correct way to do this would be to inspect the data the query resolves to before determining whether to update the cache with that data? Implementation-wise that's a lot tricker though unfortunately. :(

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe the entirely correct way to do this would be to inspect the data the query resolves to

this is something only users could verify though. Without query meta-data like dataUpdatedAt, react-query doesn’t know the age of the data. Are you suggesting we provide a way for users to extract a timestamp (age) from the promise data in some sort of callback option?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it? If we are using the fetch timestamp from the server to determine this when hydrating data, we can surely do that for promises too, just after they resolved? I'm sure it might be a big painful thing to implement, but is there some inherent thing about user/library land that blocks us from doing this in the library using the same logic?

To be clear, if we already have this query in the cache, this might require us to wait for the promise outside of the cache and only put the data in. Or even worse, to support fetching states properly, it might requires us to have some sort of "possibleUpdatePromise" or something that would not commit it's result to the cache if it's older.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@juliusmarminge the tests failures are happening because the tests hang indefinitely. Even locally, vitest never terminates. If I remove this line of code, everything is back to normal :/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How do you run the tests locally? When I run vitest from the query-core directory all tests pass?

Copy link
Collaborator

Choose a reason for hiding this comment

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

it’s the react-query tests that are failing. So go to packages/react-query and do pnpm run test:lib there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh - I wonder if that's cause we're not testing with the same type of promises that RSCs are sending down 🥹🥹

Can look later tonight when I get home

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed: 9ef516c


const queryAlreadyQueued = hydrationQueue?.find(
(query) => query.queryHash === dehydratedQuery.queryHash,
)
Expand Down
Loading