From 3bc8acbc6135f32fcb17d55f935c35772a151999 Mon Sep 17 00:00:00 2001 From: "A.J. May" Date: Mon, 11 May 2020 21:20:51 -0400 Subject: [PATCH] fix: cache initial data It appears some time ago the `initialData` configuration was used as a fallback. When #211 was merged, this behavior changed to be used with SSR like in the next.js example in the README. Issue #230 explains this was the expectation. I'm using SSR, so im fine with the new behavior. Since `initialData` is now not quickly revalidated, another issue (#308) has been raised. Since `initialData` is not cached, and the mutate w/ callback grabs the curerent data from the cache, when `initialData` is used, `undefined` is returned. fixes #308 --- src/use-swr.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/use-swr.ts b/src/use-swr.ts index e885ed4b80..5145c1e7ed 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -167,7 +167,9 @@ function useSWR( fn = config.fetcher } - const initialData = cache.get(key) || config.initialData + if (config.initialData) cache.set(key, config.initialData, false) + + const initialData = cache.get(key) const initialError = cache.get(keyErr) // if a state is accessed (data, error or isValidating), @@ -366,7 +368,7 @@ function useSWR( // and trigger a revalidation const currentHookData = stateRef.current.data - const latestKeyedData = cache.get(key) || config.initialData + const latestKeyedData = cache.get(key) // update the state if the key changed (not the inital render) or cache updated if (