Skip to content

Commit

Permalink
fix: cache initial data
Browse files Browse the repository at this point in the history
It appears some time ago the `initialData` configuration was used as a fallback.  When vercel#211 was merged, this behavior changed to be used with SSR like in the next.js example in the README.  Issue vercel#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 (vercel#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 vercel#308
  • Loading branch information
aj-may committed May 22, 2020
1 parent 40914f1 commit 307fe40
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ function useSWR<Data = any, Error = any>(
fn = config.fetcher
}

const initialData = cache.get(key) || config.initialData
if (config.initialData && !cache.has(key)) {
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),
Expand Down Expand Up @@ -366,7 +370,7 @@ function useSWR<Data = any, Error = any>(
// 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 (
Expand Down

0 comments on commit 307fe40

Please sign in to comment.