utils/saveData.js

/**
 * Checks to see if we should take steps to mitigate bandwidth usage
 * Relies on the NetworkInformation API which is has limited availability:
 * https://caniuse.com/#search=navigator.connection
 *
 * @module saveData
 * @return {boolean} - true if user has a slow connection or has asked us to limit data usage
 */
const saveData = () => {
	if ( navigator && navigator.connection ) {
		// effective connection speed is 3g or lower
		if ( /\slow-2g|2g|3g/.test( navigator.connection.effectiveType ) ) {
			return true;
		}

		// Save-Data network header is set to true
		if ( true === navigator.connection.saveData ) {
			return true;
		}
	}

	return false;
};

export default saveData;