Built on top of this Stack Overflow answer.
Breaking change
A default export has been added now to ensure that the package can be integrated smoothly when using it with ES6 imports. Example 3 has been added to indicate the same
<script type="module">
import isFastNet from 'https://cdn.jsdelivr.net/npm/isfastnet@2.0.0'
isFastNet((value) => {
let speed = value ? 'fast':'slow';
console.log('Internet is ' + speed);
})
</script>
<script>
import isFastNet from 'isFastNet'
isFastNet((data) => {
/**
* data = {
* "isFast": Boolean,
* "averageLatency": Number,
* "threshold": Number,
* "latencyReadings": [Number]
* }
*/
console.log('Received latency data: ' + JSON.stringify(data));
}, { verbose: true })
</script>
<script type="module">
import isFastNet from 'https://cdn.jsdelivr.net/npm/isfastnet@2.0.0'
isFastNet((data) => {
/**
* data = {
* "isFast": Boolean,
* "averageLatency": Number,
* "threshold": Number,
* "latencyReadings": [Number]
* }
*/
console.log('Received latency data: ' + JSON.stringify(data));
}, { verbose: true })
</script>
Do a normal ES6 import
import isFastNet from 'isFastNet'
Add isfastnet to the transpile option in nuxt.config.js
...
build: {
transpile: ['isfastnet']
}
...
isFastNet((value) => {
if(value){
// Internet is fast
}
else{
// Internet is slow
}
}, {
timesToTest: 5, // optional, number of times to load the image default is 5
threshold: 200, // optional, threshold in ms after which internet speed is considered slow
image: "http://www.google.com/images/phd/px.gif", // optional, url of the tiny image to load, keep this on a CDN
allowEarlyExit: true, // optional, if the first request takes greater than threshold*3 ms then the function exits with false
verbose: false // optional, if set, it returns an object with all calculated latency data. Overrides allowEarlyExit option (See Example "With verbose option" for usage)
})