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

Replace call to requestAnimationFrame in render function to update page content even if tab has lost focus #920

Closed
jadetr opened this issue May 4, 2023 · 6 comments

Comments

@jadetr
Copy link

jadetr commented May 4, 2023

Here is the case we are facing:

  • we use Turbo.visit method to fetch a page's content that hold a TurboStream socket connection
  • If we switch the browser tab (Tab with Rails app loses focus) before the whole page is loaded. The JS is blocked in Turbo's render method at line: this.frame = requestAnimationFrame(() => resolve())
  • The requestAnimationFrame doesn't fire callback when the tab has lost focus
  • The result is that the new page'S content is never rendered and the TurboStream socket is not connected.
  • Few minutes later, we go back into our app browser Tab, the requestAnimationFrame fire, the page is rendered and the TurboStream socket is connected.

Problem:

  • When we go back into the tab, The page show outdated HTML content that should have been updated by the TurboStream socket.
@jadetr jadetr changed the title Remove call to requestAnimationFrame from render to update page content even if tab has lost focus Replace call to requestAnimationFrame in render function to update page content even if tab has lost focus May 5, 2023
@seanpdoyle seanpdoyle mentioned this issue Oct 5, 2023
2 tasks
@afcapel
Copy link
Collaborator

afcapel commented Oct 8, 2023

@jadetr is this happening in a particular browser, or is it a common implementation? The spec says that browser should only clear the callback list if the document is visible and the callback has been processed.

@michelson
Copy link
Contributor

@afcapel we are facing this issue on a chat application please see the video examples at hotwired/turbo-rails#338

@michelson
Copy link
Contributor

michelson commented Oct 20, 2023

Indeed we have replaced ALL nextAnimationFrame for the nextMicroTask and it works, not sure which one did the trick tho. I've added a screen-recording with the working solution at hotwired/turbo-rails#338

@jadetr
Copy link
Author

jadetr commented Oct 20, 2023

@afcapel sorry for now replying before. I didn't test it in other browser and I had to implement a workaround when I've hit this bug few months ago. In other terms, I cannot easily test it on other browser now. @michelson seems to have a working solution, great!

@brunoprietog
Copy link
Collaborator

I guess we can close this since #1042 was merged

@afcapel
Copy link
Collaborator

afcapel commented Nov 2, 2023

Fixed in #1042

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

No branches or pull requests

4 participants