Skip to content

Commit 124b5d2

Browse files
authored
Breaking: drop the default fetcher (#1304)
1 parent d14e1b8 commit 124b5d2

File tree

5 files changed

+32
-30
lines changed

5 files changed

+32
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const { data, error, isValidating, mutate } = useSWR(key, fetcher, options)
113113
#### Options
114114

115115
- `suspense = false`: enable React Suspense mode [(details)](#suspense-mode)
116-
- `fetcher = window.fetch`: the default fetcher function
116+
- `fetcher`: the function to retrieve the remote data source
117117
- `initialData`: initial data to be returned (note: This is per-hook)
118118
- `revalidateOnMount`: enable or disable automatic revalidation when component is mounted (by default revalidation occurs on mount when initialData is not set, use this flag to force behavior)
119119
- `revalidateOnFocus = true`: auto revalidate when window gets focused

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface Configuration<
1818
shouldRetryOnError: boolean
1919
suspense?: boolean
2020
initialData?: Data
21-
fetcher: Fn
21+
fetcher?: Fn
2222
cache: Cache
2323
middlewares?: Middleware[]
2424

src/utils/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { slowConnection } from './env'
66
import { Configuration, RevalidatorOptions, Revalidator } from '../types'
77
import { UNDEFINED } from './helper'
88

9-
const fetcher = (url: string) => fetch(url).then(res => res.json())
109
const noop = () => {}
1110

1211
// error retry
@@ -56,7 +55,6 @@ const defaultConfig: Configuration = {
5655
loadingTimeout: slowConnection ? 5000 : 3000,
5756

5857
// providers
59-
fetcher,
6058
compare: dequal,
6159
isPaused: () => false,
6260
cache: wrapCache(new Map()),

test/use-swr-integration.test.tsx

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -357,27 +357,4 @@ describe('useSWR', () => {
357357
expect(fetcher).toBeCalled()
358358
await screen.findByText('hello, SWR')
359359
})
360-
361-
it('should use fetch api as default fetcher', async () => {
362-
const users = [{ name: 'bob' }, { name: 'sue' }]
363-
global['fetch'] = () => Promise.resolve()
364-
const mockFetch = body =>
365-
Promise.resolve({ json: () => Promise.resolve(body) } as any)
366-
const fn = jest
367-
.spyOn(window, 'fetch')
368-
.mockImplementation(() => mockFetch(users))
369-
370-
function Users() {
371-
const { data } = useSWR('http://localhost:3000/api/users')
372-
373-
return <div>hello, {data && data.map(u => u.name).join(' and ')}</div>
374-
}
375-
376-
render(<Users />)
377-
screen.getByText('hello,')
378-
expect(fn).toBeCalled()
379-
380-
await screen.findByText('hello, bob and sue')
381-
delete global['fetch']
382-
})
383360
})

test/use-swr-local-mutation.test.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { act, render, screen, fireEvent } from '@testing-library/react'
22
import React, { useEffect, useState } from 'react'
33
import useSWR, { mutate, createCache, SWRConfig } from 'swr'
44
import { serialize } from '../src/utils/serialize'
5-
import { createResponse, sleep, nextTick as waitForNextTick } from './utils'
5+
import { createResponse, sleep, nextTick } from './utils'
66

77
describe('useSWR - local mutation', () => {
88
it('should trigger revalidation programmatically', async () => {
@@ -29,6 +29,33 @@ describe('useSWR - local mutation', () => {
2929
await screen.findByText('data: 1')
3030
})
3131

32+
it('should share local state when no fetcher is specified', async () => {
33+
const useSharedState = (key, initialData) => {
34+
const { data: state, mutate: setState } = useSWR(key, { initialData })
35+
return [state, setState]
36+
}
37+
38+
function Page() {
39+
const [name, setName] = useSharedState('name', 'huozhi')
40+
const [job, setJob] = useSharedState('job', 'gardener')
41+
42+
return (
43+
<span
44+
onClick={() => {
45+
setName('@huozhi')
46+
setJob('chef')
47+
}}
48+
>
49+
{name}:{job}
50+
</span>
51+
)
52+
}
53+
render(<Page />)
54+
const root = screen.getByText('huozhi:gardener')
55+
fireEvent.click(root)
56+
await screen.findByText('@huozhi:chef')
57+
})
58+
3259
it('should trigger revalidation programmatically within a dedupingInterval', async () => {
3360
let value = 0
3461

@@ -144,7 +171,7 @@ describe('useSWR - local mutation', () => {
144171
//mount
145172
await screen.findByText('data: 0')
146173

147-
await waitForNextTick()
174+
await nextTick()
148175
await act(() => {
149176
// mutate and revalidate
150177
return mutate('mutate-promise', createResponse(999), false)
@@ -167,7 +194,7 @@ describe('useSWR - local mutation', () => {
167194
//mount
168195
await screen.findByText('data: 0')
169196

170-
await waitForNextTick()
197+
await nextTick()
171198
await act(() => {
172199
// mutate and revalidate
173200
return mutate('mutate-async-fn', async () => createResponse(999), false)

0 commit comments

Comments
 (0)