Skip to content

fix: revalidateOnFocus not working on '< iOS 14' #889

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

Merged
merged 3 commits into from
Jan 12, 2021

Conversation

joshuakimDwan
Copy link
Contributor

I found that revalidateOnFocus is not working on iOS 13.
To fix, window.addEventListener('visibilityevent', callback) should be replaced by document.addEventListener('visibilityevent', callback)

Ref: https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event
image

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 11, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit b74ad97:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration

@joshuakimDwan joshuakimDwan requested a review from huozhi January 11, 2021 14:50
src/use-swr.ts Outdated
)
const hasWindow = typeof window !== 'undefined'
const hasDocument = typeof document !== 'undefined'
if (hasWindow) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the 2 conditions doesn't have to be nested?

if (hasDocument) {
   // focus revalidators
}
if (hasWindow) {
   // other revalidators
}

Copy link
Contributor Author

@joshuakimDwan joshuakimDwan Jan 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does RN environment also have document object(like the web)?
I think that if RN doesn't have document object we should use window.addEventListener

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope. it has window object but not document.
I double check again locally with a RN app. it shows that window doesn't have addEventListener. and we need to check addEventListener. looks like your first commit also works well and it's shorter

I'll approve it on my side if can be reverted to the previous commit. sorry for my mistake and thanks for the question.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha. Is this logic ONLY for web, not RN?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, swr supports browser runtime by default. since react is been ported to different rumtime, people are trying adopt swr along with react apps. I was wondering if it's possible to make swr more flexible to be adopted among runtimes. seems not easy.

@shuding shuding merged commit e5ead06 into vercel:master Jan 12, 2021
@mikevercoelen
Copy link

After merging this fix, it's now crashing when debugging react native and giving:

document is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants