@sv-use/core
Version:
A collection of Svelte 5 utilities.
28 lines (27 loc) • 765 B
JavaScript
import { BROWSER } from 'esm-env';
/**
* Provides information about the connection a device is using to communicate with the network.
* @see https://svelte-librarian.github.io/sv-use/docs/core/get-network
*/
export function getNetwork() {
const _isSupported = $derived.by(() => navigator && 'connection' in navigator);
let _current = $state({
downlink: 0,
downlinkMax: 0,
effectiveType: 'slow-2g',
rtt: 0,
saveData: false,
type: 'unknown'
});
if (BROWSER && _isSupported) {
_current = { ..._current, ...navigator.connection };
}
return {
get isSupported() {
return _isSupported;
},
get current() {
return _current;
}
};
}