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

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
78 changes: 78 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,82 @@ describe('dehydration and rehydration', () => {
clientQueryClient.clear()
serverQueryClient.clear()
})

test.only('should overwrite data when a new promise is streamed in', async () => {
const serializeDataMock = 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)
console.log('queryFn', countRef.current)
return countRef.current
},
}

const promise = serverQueryClient.prefetchQuery(query)

const dehydrated = dehydrate(serverQueryClient)

// --- client ---
const deserializeDataMock = vi.fn((data: any) => data)
const clientQueryClient = createQueryClient({
defaultOptions: {
hydrate: {
deserializeData: deserializeDataMock,
},
},
})

hydrate(clientQueryClient, dehydrated)

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

console.log('serialize mock', serializeDataMock.mock.calls)

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

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

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

const dehydrated2 = dehydrate(serverQueryClient)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

assume this is a server fn calling revalidatePath() so the page re-renders


// --- client ---

hydrate(clientQueryClient, dehydrated2)

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

console.log('serialize mock', serializeDataMock.mock.calls)

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 @@ -177,6 +177,9 @@ export function hydrate(
const data =
state.data === undefined ? state.data : deserializeData(state.data)

console.log('data', data)
console.log('query', query)

// Do not hydrate if an existing query exists with newer data
if (query) {
if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
Expand Down
Loading