Skip to content

Commit

Permalink
Revert "First page of useSWRInfinite should reuse the cache from useS…
Browse files Browse the repository at this point in the history
…WR (#799)"

This reverts commit 62d5cfa.
  • Loading branch information
shuding authored Jan 12, 2021
1 parent 3121592 commit 7971b66
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/use-swr-infinite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import SWRConfigContext from './swr-config-context'
import useSWR from './use-swr'

import { keyType, fetcherFn, ConfigInterface, responseInterface } from './types'

type KeyLoader<Data = any> = (
index: number,
previousPageData: Data | null
Expand Down Expand Up @@ -152,7 +151,7 @@ function useSWRInfinite<Data = any, Error = any>(
const shouldRevalidatePage =
revalidateAll ||
force ||
(typeof force === 'undefined' && i === 0 && originalData) ||
(typeof force === 'undefined' && i === 0) ||
(originalData && !config.compare(originalData[i], pageData)) ||
typeof pageData === 'undefined'

Expand Down
28 changes: 3 additions & 25 deletions test/use-swr-infinite.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
act
} from '@testing-library/react'

import { useSWRInfinite, mutate } from '../src'
import { useSWRInfinite } from '../src'

describe('useSWRInfinite', () => {
it('should render the first page component', async () => {
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('useSWRInfinite', () => {
let pageData = ['apple', 'banana', 'pineapple']

function Page() {
const { data, mutate: boundMutate } = useSWRInfinite<string, string>(
const { data, mutate } = useSWRInfinite<string, string>(
index => [`pagetest-3`, index],
async (_, index) => {
await new Promise(res => setTimeout(res, 100))
Expand All @@ -74,7 +74,7 @@ describe('useSWRInfinite', () => {
<div
onClick={() => {
// reload the entire list
boundMutate()
mutate()
}}
>
{data}
Expand Down Expand Up @@ -441,26 +441,4 @@ describe('useSWRInfinite', () => {
expect(setSize).toEqual(setters[0])
}
})

it('should share initial cache from `useSWR`', async () => {
const cachedData = new Date().toISOString()
mutate('shared-cache-0', cachedData)

function Page() {
const { data } = useSWRInfinite<string, string>(
index => `shared-cache-${index}`,
async () => {
await new Promise(res => setTimeout(res, 200))
return cachedData
}
)

return <div>{data}</div>
}
const { container } = render(<Page />)
expect(container.textContent).toMatchInlineSnapshot(`""`)
// after a rerender we should already have the cached data rendered
await act(() => new Promise(res => setTimeout(res, 10)))
expect(container.textContent).toMatchInlineSnapshot(`"${cachedData}"`)
})
})

0 comments on commit 7971b66

Please sign in to comment.