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

Title Does Not Update on Tab Switch with Interval-based Updates #713

Closed
gccornejo441 opened this issue Apr 10, 2024 · 3 comments
Closed

Comments

@gccornejo441
Copy link

Environment

  • Browser: Chrome 90.0.4430.212
  • React version: 17.0.2
  • React Helmet version: 6.1.0

Reproduction Link

Steps to Reproduce

  1. Open the provided CodeSandbox in your browser.
  2. Observe the displayed time in the browser tab.
  3. Switch to another browser tab and observe.
  4. Switch back to the CodeSandbox tab.

Expected Behavior

The browser tab title should continuously update every second to reflect the current time, even when the tab is not in focus.

Actual Behavior

The browser tab title stops updating when the tab is not in focus and only resumes updating when the tab is brought back into focus.

Additional Information

This issue may be related to how modern browsers handle background tabs, potentially throttling JavaScript execution, which affects dynamic updates to the <title> element managed by React Helmet.

Possible Solution

Investigating if React Helmet's internal handling of the <title> tag update could be optimized for background tabs could be a starting point.

@gccornejo441
Copy link
Author

gccornejo441 commented Apr 11, 2024

If you don't plan on using React-Helmet

`import { useEffect, useState } from "react";
import "./styles.css";
import { Helmet } from 'react-helmet';

export default function App() {
const [date, setDate] = useState(new Date());

useEffect(() => {
setInterval(() => setDate(new Date()), 1000);
document.title = date.toLocaleTimeString();

}, [date]);

return (


<h1 style={{color: 'red'}}>The current time is {date.toLocaleTimeString()}

);
}
`

@schontz
Copy link

schontz commented Apr 29, 2024

Intervals are paused and/or run much less frequently when tabs are not in focus. This is by design so that background tabs do not take a lot of resources.

@gccornejo441
Copy link
Author

Intervals are paused and/or run much less frequently when tabs are not in focus. This is by design so that background tabs do not take a lot of resources.

I found a solution to my particular use case. Using a web worker.

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

No branches or pull requests

2 participants