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

revalidateOnMount not working on react-native #471

Closed
svenlombaert opened this issue Jun 18, 2020 · 9 comments · Fixed by #744
Closed

revalidateOnMount not working on react-native #471

svenlombaert opened this issue Jun 18, 2020 · 9 comments · Fixed by #744

Comments

@svenlombaert
Copy link

svenlombaert commented Jun 18, 2020

Bug report

Description / Observed Behavior

I added revalidateOnMount option to useSWR, but it doesn't seem to revalidate, no requests get fired after I open a screen for the second time.

Expected Behavior

It should revalidate all requests when re-opening screen

Repro Steps / Code Example

I tried to prove the bug, but here I have the complete opposite behaviour. Can't turn the revalidation off 😕

https://snack.expo.io/@svrnlombaert/thoughtful-apple

@svenlombaert svenlombaert changed the title revalidateOnMount not working on react-native revalidateOnMount not working on react-native Jun 18, 2020
@svenlombaert
Copy link
Author

svenlombaert commented Jun 18, 2020

Okay so I was logging a bit more, and I think I know what's happening. RN uses a polyfill for the window object (https://github.com/facebook/react-native/blob/master/Libraries/Core/setUpGlobals.js), and I see that there's a check like this in useSWR:

// trigger a revalidation
        if (config.revalidateOnMount ||
            (!config.initialData && config.revalidateOnMount === undefined)) {
            if (typeof latestKeyedData !== 'undefined' &&
                !IS_SERVER &&
                window['requestIdleCallback']) {
                // delay revalidate if there's cache
                // to not block the rendering
                window['requestIdleCallback'](softRevalidate);
            }
            else {
                softRevalidate();
            }
        }

So problem is is that IS_SERVER is returning false (I think, because RN sets up 'window' by itself) and the softRevalidate gets queued by window.requestIdleCallback but never actually executed.

EDIT:
Seems like I'm running into this issue. Might not be an issue with the library after all!
facebook/react-native#28602

@huozhi
Copy link
Member

huozhi commented Jun 18, 2020

hey @svenlombaert thanks for reporting this!
I've filed a PR #473 for it to polyfill requestIdleCallback on other non-browser but javascript runtime (like RN). I think it might solve your issue.

@svenlombaert
Copy link
Author

Well actually RN polyfills it, it's just not getting executed on their side since v0.62.2 (facebook/react-native#28602). So not sure if anything needs to change to be honest?

You can see requestIdleCallbackright here https://github.com/facebook/react-native/blob/master/Libraries/Core/Timers/JSTimers.js#L295 and they put it on the 'window' right here: https://github.com/facebook/react-native/blob/master/Libraries/Core/setUpTimers.js#L32 using the `polyfillGlobal'.

So since window. requestIdleCallback does exist in RN, that PR won't fix much, I think?

@huozhi
Copy link
Member

huozhi commented Jun 18, 2020

sounds more like a bug on RN side?
I guess there's another way to work around: override window.requestIdleCallback with setTimeout in your RN app. is that doable? like

window.requestIdleCallback = (fn) => setTimeout(fn, 1)

@huozhi
Copy link
Member

huozhi commented Jun 20, 2020

@svenlombaert I just tried your demo, config useSWR with the following options which are borrowed from #450

revalidateOnMount: true,
revalidateOnFocus: false,
revalidateOnReconnect: false,
refreshWhenOffline: false,
refreshWhenHidden: false,
refreshInterval: 0

and test it with an iphone device, it works for me (each toggle on will revalidate the user). but I saw the version of swr is 0.2.0, could you confirm is it still reproducible with the latest version of swr? since revalidateOnMount is added in 0.2.1

@svenlombaert
Copy link
Author

I upgraded to the latest version of swr (0.2.3) and it's still not working. I can confirm the above snack is working when upgrading to 0.2.3, so I'm pretty sure it's the react-native issue we're running into. Feel free to close this issue.

shuding pushed a commit that referenced this issue Jul 25, 2020
…ilable (#473)

* enhance: polyfill irc (#471)

* run timers

* fix tests
@matt-oakes
Copy link

Just to cross post this here, I have found the issue that's causing this and submitted a PR to fix it. It's easy to fix this yourself using patch-package.

@huozhi huozhi linked a pull request Nov 2, 2020 that will close this issue
@AlexanderKaran
Copy link

Hi, is there any update on this? I can see the PR is still open in ReactNative

@huozhi
Copy link
Member

huozhi commented Mar 19, 2021

Close this as it's more like a react native runtime issue

@huozhi huozhi closed this as completed Mar 19, 2021
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 a pull request may close this issue.

4 participants