Created
August 11, 2021 21:11
-
-
Save drewminns/3e87ce7e8ede2473334a9442b5bc5c60 to your computer and use it in GitHub Desktop.
Get browser network data - Won't work in iOS Safari
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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