Skip to content

Instantly share code, notes, and snippets.

@drewminns
Created August 11, 2021 21:11
Show Gist options
  • Save drewminns/3e87ce7e8ede2473334a9442b5bc5c60 to your computer and use it in GitHub Desktop.
Save drewminns/3e87ce7e8ede2473334a9442b5bc5c60 to your computer and use it in GitHub Desktop.
Get browser network data - Won't work in iOS Safari
import { useEffect, useState } from 'react'
export function usePerformanceData() {
const [size, setSize] = useState(0)
useEffect(() => {
if (process.browser) {
const performance = window.performance || {}
if (performance.getEntries) {
const data: any[] = [...performance.getEntries()]
const added = data.reduce((accum, entry) => {
if (entry.initiatorType === 'link' || entry.initiatorType === 'script') {
if (entry.transferSize) {
return accum + entry.transferSize
} else {
return accum
}
} else {
return accum
}
}, 0)
setSize(added)
}
}
})
return size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment