Skip to content

Commit

Permalink
fix(queryCache): caches from makeQueryCache should be unfrozen/cache …
Browse files Browse the repository at this point in the history
…by default in node (TanStack#749)

Fix so the default behaviour matches the one described in the Readme.
Add tests for the above behaviour.

Closes TanStack#706
  • Loading branch information
Ephem authored and tannerlinsley committed Jul 17, 2020
1 parent c227b2c commit 2e4e941
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/core/queryCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
import { defaultConfigRef } from './config'
import { makeQuery } from './query'

export const queryCache = makeQueryCache()
export const queryCache = makeQueryCache({ frozen: isServer })

export const queryCaches = [queryCache]

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

Expand Down
49 changes: 40 additions & 9 deletions src/react/tests/ssr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,44 @@ import {
usePaginatedQuery,
ReactQueryCacheProvider,
queryCache,
queryCaches,
makeQueryCache,
useQuery,
} from '../index'
import { sleep } from './utils'

describe('Server Side Rendering', () => {
describe('global cache', () => {
afterEach(() => {
queryCaches.forEach(cache => cache.clear({ notify: false }))
})
// A frozen cache does not cache any data. This is the default
// for the global cache in a node-environment, since it's
// otherwise easy to cache data between separate requests,
// which is a security risk.
//
// See https://github.com/tannerlinsley/react-query/issues/70
it('global cache should be frozen by default', async () => {
const fetchFn = () => Promise.resolve('data')
const data = await queryCache.prefetchQuery('key', fetchFn)

expect(data).toBe('data')
expect(queryCache.getQuery('key')).toBeFalsy()

queryCache.clear({ notify: false })
})

// When consumers of the library create a cache explicitly by
// calling makeQueryCache, they take on the responsibility of
// not using that cache to cache data between requests or do so
// in a safe way.
it('created caches should be unfrozen by default', async () => {
const queryCache = makeQueryCache()
const fetchFn = () => Promise.resolve('data')
const data = await queryCache.prefetchQuery('key', fetchFn)

expect(data).toBe('data')
expect(queryCache.getQuery('key')).toBeTruthy()
})

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

function Page() {
Expand All @@ -35,14 +60,19 @@ describe('Server Side Rendering', () => {
)
}

const markup = renderToString(<Page />)
const markup = renderToString(
<ReactQueryCacheProvider queryCache={queryCache}>
<Page />
</ReactQueryCacheProvider>
)

expect(markup).toContain('status loading')
expect(queryFn).toHaveBeenCalledTimes(0)
})

// See https://github.com/tannerlinsley/react-query/issues/70
it('should not add initialData to the cache', () => {
const queryCache = makeQueryCache({ frozen: true })

function Page() {
const [page, setPage] = React.useState(1)
const { resolvedData } = usePaginatedQuery(
Expand All @@ -54,10 +84,10 @@ describe('Server Side Rendering', () => {
)

return (
<div>
<ReactQueryCacheProvider queryCache={queryCache}>
<h1 data-testid="title">{resolvedData}</h1>
<button onClick={() => setPage(page + 1)}>next</button>
</div>
</ReactQueryCacheProvider>
)
}

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

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

Expand Down

0 comments on commit 2e4e941

Please sign in to comment.