-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
enhance: fallback to setTimeout when request idle callback is not available #473
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I would just name the polyfill as requestIdleCallback
, doing it will replace the window
version with the polyfill for the module or define as the window
version.
@sergiodxa is it safe to override the original window.requestIdleCallback? |
We are not directly overriding it, we are creating a function with the same name only inside the scope of the module, if you use the window object you will still have access to the original. |
I think here is more like a fallback instead of polyfill it since global polyfill inside library will bring side effects to the application who is using it. I should rename it. another thing I'm not sure is that: is it safe to only use this fix sounds like won't work for RN based on the issue they mentioned (rIC polyfill inside RN doesn't work for iOS now). if use setTimeout only, the issue could be resolved. which solution do you prefer here? keep the fallback like this and leave the polyfill to platform or we can use setTimeout to replace iRC @shuding |
This will not fix the RN issue at all, it will only add support on old browsers and environments where rIC is not available, if it's available and it doesn't work this has no way to know it. |
no this PR won't fix the issue indeed..I think we have few ways to do it:
thus this PR becomes an enhancement only change 😂 |
I think we could let the user provide the polyfill, what do you think @huozhi @shuding? This way they could either define |
// delay revalidate if there's cache | ||
// to not block the rendering | ||
window['requestIdleCallback'](softRevalidate) | ||
rIC(softRevalidate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if rIC
is null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useIsomorphicLayoutEffect
will execute the callback on client side only. when IS_SERVER
is true it won't execute.
check the following tip under https://reactjs.org/docs/hooks-reference.html#uselayouteffect
If you use server rendering, keep in mind that neither useLayoutEffect nor useEffect can run until the JavaScript is downloaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I just looked at the diff, not at the context around it 🙈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useIsomorphicLayoutEffect
will execute the callback on client side only. whenIS_SERVER
is true it won't execute.check the following tip under https://reactjs.org/docs/hooks-reference.html#uselayouteffect
If you use server rendering, keep in mind that neither useLayoutEffect nor useEffect can run until the JavaScript is downloaded.
I use SWR in WeChat mini program runtime, which window
is undefined and rIC
is null, but useIsomorphicLayoutEffect
will execute. Get an error said rIC is not a function
, search the word rIC
lead me to this pull request.
Maybe if(typeof latestKeyedData !== 'undefined' && rIC))
is safer than if(typeof latestKeyedData !== 'undefined' ))
?
leverage the existing simple fallback code from https://github.com/vercel/swr/pull/446/files