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 3 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
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
Collaborator 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