Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialise nextFocusRevalidatedAt on mount #2857

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/index/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,15 @@ export const useSWRHandler = <Data = any, Error = any>(

const softRevalidate = revalidate.bind(UNDEFINED, WITH_DEDUPE)

let nextFocusRevalidatedAt = 0

if (getConfig().revalidateOnFocus) {
const initNow = Date.now()
nextFocusRevalidatedAt = initNow + getConfig().focusThrottleInterval
}

// Expose revalidators to global event listeners. So we can trigger
// revalidation from the outside.
let nextFocusRevalidatedAt = 0
const onRevalidate = (
type: RevalidateEvent,
opts: {
Expand Down
6 changes: 4 additions & 2 deletions test/use-swr-cache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ describe('useSWR - cache provider', () => {
let value = 0
function Page() {
const { data } = useSWR(key, () => value++, {
dedupingInterval: 0
dedupingInterval: 0,
focusThrottleInterval: 0
})
return <>{String(data)}</>
}
Expand Down Expand Up @@ -402,7 +403,8 @@ describe('useSWR - global cache', () => {
let value = 0
function Page() {
const { data } = useSWR(key, () => value++, {
dedupingInterval: 0
dedupingInterval: 0,
focusThrottleInterval: 0
})
return <>{String(data)}</>
}
Expand Down
24 changes: 12 additions & 12 deletions test/use-swr-focus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('useSWR - focus', () => {
const key = createKey()
function Page() {
const { data } = useSWR(key, () => value++, {
dedupingInterval: 0
dedupingInterval: 0,
focusThrottleInterval: 0
})
return <div>data: {data}</div>
}
Expand Down Expand Up @@ -131,19 +132,17 @@ describe('useSWR - focus', () => {
await screen.findByText('data: 0')

await waitForNextTick()
// trigger revalidation
await focusWindow()
// still in throttling interval
await act(() => sleep(20))
// should be throttled
await focusWindow()
await screen.findByText('data: 1')
await screen.findByText('data: 0')
// wait for focusThrottleInterval
await act(() => sleep(100))

// trigger revalidation again
await focusWindow()
await screen.findByText('data: 2')
await screen.findByText('data: 1')
})

it('focusThrottleInterval should be stateful', async () => {
Expand All @@ -168,17 +167,17 @@ describe('useSWR - focus', () => {
await screen.findByText('data: 0')

await waitForNextTick()
// trigger revalidation
// trigger revalidation, won't revalidate as within 50ms
await focusWindow()
// wait for throttle interval
await act(() => sleep(100))
// trigger revalidation
// trigger revalidation, will revalidate as 50ms passed
await focusWindow()
await screen.findByText('data: 2')
await screen.findByText('data: 1')

await waitForNextTick()
// increase focusThrottleInterval
fireEvent.click(screen.getByText('data: 2'))
fireEvent.click(screen.getByText('data: 1'))
// wait for throttle interval
await act(() => sleep(100))
// trigger revalidation
Expand All @@ -187,15 +186,15 @@ describe('useSWR - focus', () => {
await act(() => sleep(100))
// should be throttled
await focusWindow()
await screen.findByText('data: 3')
await screen.findByText('data: 2')

// wait for throttle interval
await act(() => sleep(150))
// trigger revalidation
await focusWindow()
// wait for throttle intervals
await act(() => sleep(150))
await screen.findByText('data: 4')
await screen.findByText('data: 3')
})

it('should revalidate on focus even with custom cache', async () => {
Expand All @@ -205,7 +204,8 @@ describe('useSWR - focus', () => {
function Page() {
const { data } = useSWR(key, () => value++, {
revalidateOnFocus: true,
dedupingInterval: 0
dedupingInterval: 0,
focusThrottleInterval: 0
})
return <div>data: {data}</div>
}
Expand Down
Loading