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

createCache with revalidateOnFocus does not work #1275

Closed
origin-master opened this issue Jul 7, 2021 · 0 comments · Fixed by #1283
Closed

createCache with revalidateOnFocus does not work #1275

origin-master opened this issue Jul 7, 2021 · 0 comments · Fixed by #1283
Assignees
Labels
bug Something isn't working

Comments

@origin-master
Copy link
Contributor

Bug report

Description / Observed Behavior

Custom cache settings in 1.0.0-beta.8 disables revalidateOnFocus behaviour; focus and visibilitychange events are not revalidating SWR caches.

Expected Behavior

Revalidate on focus on revalidateOnFocus: true, as non-cache version does.

Repro Steps / Code Example

CodePen: https://codepen.io/origin-master/pen/WNjwgYy

Additional Context

SWR version: 1.0.0-beta.8

This can be a problem...

registerOnFocus called in only here, and it's based on defaultConfig.cache:

swr/src/use-swr.ts

Lines 49 to 62 in c677d04

if (!IS_SERVER) {
const [FOCUS_REVALIDATORS, RECONNECT_REVALIDATORS] = getGlobalState(
defaultConfig.cache
)
const revalidate = (revalidators: Record<string, Revalidator[]>) => {
if (!defaultConfig.isDocumentVisible() || !defaultConfig.isOnline()) return
for (const key in revalidators) {
if (revalidators[key][0]) revalidators[key][0]()
}
}
defaultConfig.registerOnFocus(() => revalidate(FOCUS_REVALIDATORS))
defaultConfig.registerOnReconnect(() => revalidate(RECONNECT_REVALIDATORS))
}

Current API encourages only cache from createCache(...) should be put into SWRConfig, and doesn't cares mutate:

const { cache, mutate } = createCache(new Map());
const Config = () => <SWRConfig value={{ cache }} />
@huozhi huozhi added the bug Something isn't working label Jul 7, 2021
@huozhi huozhi self-assigned this Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants