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

enhance: fallback to setTimeout when request idle callback is not available #473

Merged
merged 3 commits into from
Jul 25, 2020

Conversation

huozhi
Copy link
Member

@huozhi huozhi commented Jun 18, 2020

leverage the existing simple fallback code from https://github.com/vercel/swr/pull/446/files

@huozhi huozhi changed the title enhance: polyfill request idle callback (#471) enhance: polyfill request idle callback Jun 18, 2020
Copy link
Contributor

@sergiodxa sergiodxa left a 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.

@shuding
Copy link
Member

shuding commented Jun 19, 2020

@sergiodxa is it safe to override the original window.requestIdleCallback?

@sergiodxa
Copy link
Contributor

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.

@huozhi
Copy link
Member Author

huozhi commented Jun 19, 2020

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 setTimeout fallback instead of rIC? insce rIC is kind of browser specific that other javascript runtime might not have. but react could have different renderers in different javascript runtime (like project ink for terminal). want to understand more about why using rIC here.

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

@huozhi huozhi changed the title enhance: polyfill request idle callback enhance: fallback to setTimeout when request idle callback is not available Jun 19, 2020
@sergiodxa
Copy link
Contributor

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.

@huozhi
Copy link
Member Author

huozhi commented Jun 20, 2020

no this PR won't fix the issue indeed..I think we have few ways to do it:

  1. if it's a polyfill issue on RN side, wait RN to fix it
  2. user knows it might be a polyfill issue on RN side, user overrides the polyfill on their RN app with javascript (window.requestIdeCallback = fn => setTimeout(fn, 1))
  3. we use setTimeout to replace the rIC, then even the polyfill have problem it can still work (so I'm not sure if this is good to do so)

thus this PR becomes an enhancement only change 😂

@sergiodxa
Copy link
Contributor

I think we could let the user provide the polyfill, what do you think @huozhi @shuding? This way they could either define window.requestIdeCallback = fn => setTimeout(fn, 1) or use a package, and if they are using another library expecting rIC to exists we are not going to make them polyfill it twice.

// delay revalidate if there's cache
// to not block the rendering
window['requestIdleCallback'](softRevalidate)
rIC(softRevalidate)
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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 🙈

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.

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' ))?

@shuding shuding merged commit 2a16045 into vercel:master Jul 25, 2020
@huozhi huozhi deleted the ric-polyfill branch July 25, 2020 17:45
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.

5 participants