Skip to content

Commit 43417d7

Browse files
committed
Fix broken extractRehydrationInfo
1 parent d8190e3 commit 43417d7

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

packages/toolkit/src/query/core/buildSlice.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,12 @@ export function buildSlice({
519519
providedTags as FullTagDescription<string>[]
520520
}
521521
},
522-
prepare:
523-
prepareAutoBatched<
524-
Array<{
525-
queryCacheKey: QueryCacheKey
526-
providedTags: readonly FullTagDescription<string>[]
527-
}>
528-
>(),
522+
prepare: prepareAutoBatched<
523+
Array<{
524+
queryCacheKey: QueryCacheKey
525+
providedTags: readonly FullTagDescription<string>[]
526+
}>
527+
>(),
529528
},
530529
},
531530
extraReducers(builder) {
@@ -538,7 +537,9 @@ export function buildSlice({
538537
)
539538
.addMatcher(hasRehydrationInfo, (draft, action) => {
540539
const { provided } = extractRehydrationInfo(action)!
541-
for (const [type, incomingTags] of Object.entries(provided)) {
540+
for (const [type, incomingTags] of Object.entries(
541+
provided.tags ?? {},
542+
)) {
542543
for (const [id, cacheKeys] of Object.entries(incomingTags)) {
543544
const subscribedQueries = ((draft.tags[type] ??= {})[
544545
id || '__internal_without_id'
@@ -549,6 +550,7 @@ export function buildSlice({
549550
if (!alreadySubscribed) {
550551
subscribedQueries.push(queryCacheKey)
551552
}
553+
draft.keys[queryCacheKey] = provided.keys[queryCacheKey]
552554
}
553555
}
554556
}

packages/toolkit/src/query/tests/buildSlice.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { createSlice } from '@reduxjs/toolkit'
1+
import { createSlice, createAction } from '@reduxjs/toolkit'
2+
import type { CombinedState } from '@reduxjs/toolkit/query'
23
import { createApi } from '@reduxjs/toolkit/query'
34
import { delay } from 'msw'
45
import { setupApiStore } from '../../tests/utils/helpers'
56

67
let shouldApiResponseSuccess = true
78

9+
const rehydrateAction = createAction<{ api: CombinedState<any, any, any> }>(
10+
'persist/REHYDRATE',
11+
)
12+
813
const baseQuery = (args?: any) => ({ data: args })
914
const api = createApi({
1015
baseQuery,
@@ -17,6 +22,12 @@ const api = createApi({
1722
providesTags: (result) => (result?.success ? ['SUCCEED'] : ['FAILED']),
1823
}),
1924
}),
25+
extractRehydrationInfo(action, { reducerPath }) {
26+
if (rehydrateAction.match(action)) {
27+
return action.payload?.[reducerPath]
28+
}
29+
return undefined
30+
},
2031
})
2132
const { getUser } = api.endpoints
2233

@@ -114,6 +125,20 @@ describe('buildSlice', () => {
114125
api.util.selectInvalidatedBy(storeRef.store.getState(), ['FAILED']),
115126
).toHaveLength(1)
116127
})
128+
129+
it('handles extractRehydrationInfo correctly', async () => {
130+
await storeRef.store.dispatch(getUser.initiate(1))
131+
await storeRef.store.dispatch(getUser.initiate(2))
132+
133+
const stateWithUser = storeRef.store.getState()
134+
135+
storeRef.store.dispatch(api.util.resetApiState())
136+
137+
storeRef.store.dispatch(rehydrateAction({ api: stateWithUser.api }))
138+
139+
const rehydratedState = storeRef.store.getState()
140+
expect(rehydratedState).toEqual(stateWithUser)
141+
})
117142
})
118143

119144
describe('`merge` callback', () => {

0 commit comments

Comments
 (0)