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

feat: add fallbackData option #12

Merged
merged 3 commits into from
Aug 9, 2022
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
1 change: 1 addition & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ const { data, error, isValidating, mutate } = useSWR(key, fetcher, options)
- `revalidateOnReconnect = true` - Automatically revalidate when the browser regains a network connection
- `revalidateIfStale = true` - Automatically revalidate if there is stale data
- `dedupingInterval = 2000` - dedupe requests with the same key in this time span in milliseconds
- `fallbackData` - initial data to be returned (note: This is per-composable, but also be passed in global config)
- `onSuccess(data, key, config)` - callback function when a request finishes successfully
- `onError(err, key, config)` - callback function when a request returns an error
3 changes: 2 additions & 1 deletion lib/composables/swr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const useSWR = <Data = any, Error = any>(
revalidateOnReconnect,
revalidateIfStale,
dedupingInterval,
fallbackData,
onSuccess,
onError,
} = mergedConfig;
Expand All @@ -31,7 +32,7 @@ export const useSWR = <Data = any, Error = any>(

const error = valueInCache.value ? toRef(valueInCache.value, 'error') : ref<Error>();
const isValidating = valueInCache.value ? toRef(valueInCache.value, 'isValidating') : ref(true);
const data = valueInCache.value ? toRef(valueInCache.value, 'data') : ref();
const data = valueInCache.value ? toRef(valueInCache.value, 'data') : ref(fallbackData);
const fetchedIn = valueInCache.value ? toRef(valueInCache.value, 'fetchedIn') : ref(new Date());

const fetchData = async () => {
Expand Down
37 changes: 37 additions & 0 deletions lib/composables/swr/swr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,41 @@ describe('useSWR', () => {
expect.objectContaining(mergedConfig),
);
});

it('should return fallbackData as initial value', () => {
const fallbackData = 'fallback';

const { data } = useInjectedSetup(
() => configureGlobalSWR({ cacheProvider }),
() => useSWR(defaultKey, defaultFetcher, { fallbackData }),
);

expect(data.value).toBe(fallbackData);
});

it('should return global fallbackData as initial value', () => {
const fallbackData = 'fallback';

const { data } = useInjectedSetup(
() => configureGlobalSWR({ cacheProvider, fallbackData }),
() => useSWR(defaultKey, defaultFetcher),
);

expect(data.value).toBe(fallbackData);
});

it('should return stale data fallbackData and stale data are present', async () => {
const fallbackData = 'fallback';
const cahedData = 'cached value';

setDataToCache(defaultKey, { data: cahedData });

const { data } = useInjectedSetup(
() => configureGlobalSWR({ cacheProvider, fallbackData }),
() => useSWR(defaultKey, defaultFetcher),
);

await flushPromises();
expect(data.value).toBe(cahedData);
});
});
5 changes: 5 additions & 0 deletions lib/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export type SWRConfig<Data = any, Err = any> = {
*/
dedupingInterval: number;

/**
* initial data to be returned (note: ***This is per-composable***)
*/
fallbackData?: Data;

/**
* called when a request finishes successfully
*/
Expand Down