Skip to content

Skip revalidation for RSC fallback data by default #4119

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/_internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export interface PublicConfiguration<
* @link https://swr.vercel.app/docs/revalidation#disable-automatic-revalidations
*/
revalidateIfStale: boolean
/**
* automatically revalidate when data comes from RSC fallback
* @defaultValue false
*/
revalidateOnRSCFallback?: boolean
/**
* retry when fetcher has an error
* @defaultValue true
Expand Down
1 change: 1 addition & 0 deletions src/_internal/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const defaultConfig: FullConfiguration = mergeObjects(
revalidateOnReconnect: true,
revalidateIfStale: true,
shouldRetryOnError: true,
revalidateOnRSCFallback: false,

// timeouts
errorRetryInterval: slowConnection ? 10000 : 5000,
Expand Down
3 changes: 3 additions & 0 deletions src/index/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ export const useSWRHandler = <Data = any, Error = any>(
// If it's paused, we skip revalidation.
if (getConfig().isPaused()) return false

// If RSC fallback data on initial render, use revalidateOnRSCFallback config
if (!isUndefined(fallback) && !isUndefined(data) && !IS_SERVER && isInitialMount) return getConfig().revalidateOnRSCFallback ?? false;

// Under suspense mode, it will always fetch on render if there is no
// stale data so no need to revalidate immediately mount it again.
// If data exists, only revalidate if `revalidateIfStale` is true.
Expand Down
47 changes: 47 additions & 0 deletions test/rsc-fallback.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import React from 'react'
import useSWR, { SWRConfig } from 'swr'
import { sleep } from './utils'

describe('RSC Fallback', () => {
it('should not revalidate on mount with RSC fallback data by default', async () => {
const fetchFn = jest.fn(() => 'updated data')

function Page() {
const { data } = useSWR('key', fetchFn, {
fallbackData: 'RSC data'
})
return <div>Data: {data}</div>
}

render(<Page />)

expect(screen.getByText('Data: RSC data')).toBeInTheDocument()
expect(fetchFn).not.toHaveBeenCalled()

await sleep(50)

expect(fetchFn).not.toHaveBeenCalled()
})

it('should revalidate on mount with RSC fallback data when revalidateOnRSCFallback is true', async () => {
const fetchFn = jest.fn(() => 'updated data')

function Page() {
const { data } = useSWR('key', fetchFn, {
fallbackData: 'RSC data',
revalidateOnRSCFallback: true
})
return <div>Data: {data}</div>
}

render(<Page />)

expect(screen.getByText('Data: RSC data')).toBeInTheDocument()
expect(fetchFn).toHaveBeenCalledTimes(1)

await sleep(50)

expect(screen.getByText('Data: updated data')).toBeInTheDocument()
})
})