Merge pull request #268 from zenby/do-not-request-data-at-background
[fix]: infinite requests if page is not visible
This commit is contained in:
@@ -8,7 +8,15 @@ import { Link } from "gatsby"
|
|||||||
import { act } from "react-dom/test-utils"
|
import { act } from "react-dom/test-utils"
|
||||||
|
|
||||||
const Header = props => {
|
const Header = props => {
|
||||||
|
const shouldRequestStats = () => {
|
||||||
|
const isFirstRequest = stats.starsCount === 0
|
||||||
|
const isVisible = window.document.visibilityState === 'visible'
|
||||||
|
const hasFocus = window.document.hasFocus()
|
||||||
|
return isFirstRequest || isVisible && hasFocus
|
||||||
|
}
|
||||||
|
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
|
if (shouldRequestStats()) {
|
||||||
var response = await axios.get(
|
var response = await axios.get(
|
||||||
"https://api.github.com/repos/rahuldkjain/github-profile-readme-generator"
|
"https://api.github.com/repos/rahuldkjain/github-profile-readme-generator"
|
||||||
)
|
)
|
||||||
@@ -22,6 +30,7 @@ const Header = props => {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const [stats, setstats] = useState({
|
const [stats, setstats] = useState({
|
||||||
starsCount: 0,
|
starsCount: 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user