Skip to content

Commit 2e4e941

Browse files
Ephemtannerlinsley
authored andcommitted
fix(queryCache): caches from makeQueryCache should be unfrozen/cache by default in node (#749)
Fix so the default behaviour matches the one described in the Readme. Add tests for the above behaviour. Closes #706
1 parent c227b2c commit 2e4e941

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/core/queryCache.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
import { defaultConfigRef } from './config'
99
import { makeQuery } from './query'
1010

11-
export const queryCache = makeQueryCache()
11+
export const queryCache = makeQueryCache({ frozen: isServer })
1212

1313
export const queryCaches = [queryCache]
1414

15-
export function makeQueryCache({ frozen = isServer, defaultConfig } = {}) {
15+
export function makeQueryCache({ frozen = false, defaultConfig } = {}) {
1616
// A frozen cache does not add new queries to the cache
1717
const globalListeners = []
1818

src/react/tests/ssr.test.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,44 @@ import {
88
usePaginatedQuery,
99
ReactQueryCacheProvider,
1010
queryCache,
11-
queryCaches,
1211
makeQueryCache,
1312
useQuery,
1413
} from '../index'
1514
import { sleep } from './utils'
1615

1716
describe('Server Side Rendering', () => {
18-
describe('global cache', () => {
19-
afterEach(() => {
20-
queryCaches.forEach(cache => cache.clear({ notify: false }))
21-
})
17+
// A frozen cache does not cache any data. This is the default
18+
// for the global cache in a node-environment, since it's
19+
// otherwise easy to cache data between separate requests,
20+
// which is a security risk.
21+
//
22+
// See https://github.com/tannerlinsley/react-query/issues/70
23+
it('global cache should be frozen by default', async () => {
24+
const fetchFn = () => Promise.resolve('data')
25+
const data = await queryCache.prefetchQuery('key', fetchFn)
26+
27+
expect(data).toBe('data')
28+
expect(queryCache.getQuery('key')).toBeFalsy()
29+
30+
queryCache.clear({ notify: false })
31+
})
32+
33+
// When consumers of the library create a cache explicitly by
34+
// calling makeQueryCache, they take on the responsibility of
35+
// not using that cache to cache data between requests or do so
36+
// in a safe way.
37+
it('created caches should be unfrozen by default', async () => {
38+
const queryCache = makeQueryCache()
39+
const fetchFn = () => Promise.resolve('data')
40+
const data = await queryCache.prefetchQuery('key', fetchFn)
41+
42+
expect(data).toBe('data')
43+
expect(queryCache.getQuery('key')).toBeTruthy()
44+
})
2245

46+
describe('frozen cache', () => {
2347
it('should not trigger fetch', () => {
48+
const queryCache = makeQueryCache({ frozen: true })
2449
const queryFn = jest.fn()
2550

2651
function Page() {
@@ -35,14 +60,19 @@ describe('Server Side Rendering', () => {
3560
)
3661
}
3762

38-
const markup = renderToString(<Page />)
63+
const markup = renderToString(
64+
<ReactQueryCacheProvider queryCache={queryCache}>
65+
<Page />
66+
</ReactQueryCacheProvider>
67+
)
3968

4069
expect(markup).toContain('status loading')
4170
expect(queryFn).toHaveBeenCalledTimes(0)
4271
})
4372

44-
// See https://github.com/tannerlinsley/react-query/issues/70
4573
it('should not add initialData to the cache', () => {
74+
const queryCache = makeQueryCache({ frozen: true })
75+
4676
function Page() {
4777
const [page, setPage] = React.useState(1)
4878
const { resolvedData } = usePaginatedQuery(
@@ -54,10 +84,10 @@ describe('Server Side Rendering', () => {
5484
)
5585

5686
return (
57-
<div>
87+
<ReactQueryCacheProvider queryCache={queryCache}>
5888
<h1 data-testid="title">{resolvedData}</h1>
5989
<button onClick={() => setPage(page + 1)}>next</button>
60-
</div>
90+
</ReactQueryCacheProvider>
6191
)
6292
}
6393

@@ -67,6 +97,7 @@ describe('Server Side Rendering', () => {
6797
})
6898

6999
it('should not add prefetched data to the cache', async () => {
100+
const queryCache = makeQueryCache({ frozen: true })
70101
const fetchFn = () => Promise.resolve('data')
71102
const data = await queryCache.prefetchQuery('key', fetchFn)
72103

0 commit comments

Comments
 (0)