Open
Description
Summary
The web lacks a programmatic way to detect network activity and schedule low priority or pre-emptive network tasks. I'd like to propose an API – networkIdleCallback
(say), that would allow one to schedule tasks that execute when the network activity in the current browsing context falls below a certain threshold.
Existing APIs and how they compare
- Resource Hints: Prefetching or preloading is limited to the loading of resources, and isn't easy to schedule or predict when the load is triggered.
- Network Information API: More specifically, the
downlink
property gives an estimation of the effective bandwidth, though I don't think the value is updated near real time.
Use cases
- Scheduling non-critical analytics to be flushed to the server at idle time.
- In single page applications – where page lifecycle methods like
DOMContentLoaded
andonLoad
aren't called for route changes, and hence as a programmatic substitute for load low-priority resources. - A companion to the
Cache
API – for preloading data and/or resources and saving them in the service worker cache when the host page is idle.
Possible API
A perhaps intuitive way to model this would be to emulate the requestIdleCallback
API –
const callbackId =
networkIdleCallback(() => {
// load resources or execute action when network is idle
}, { timeout: 1000 })
cancelNetworkIdleCallback(callbackId)
Other usage examples
With requestIdleCallback
Fire analytics when both CPU and network are free–
function fireEvents(listOfEvents) {
requestIdleCallback(() => {
const normalizedEvents = someHeavyCompute(listOfEvents)
networkIdleCallback(() =>
someEventFlusher(normalizedEvents),
{ timeout: 1000 }
)
}, { timeout: 500 })
}
Prior Art
- networkIdleCallback – A ponyfill I made for this proposed API sometime back that uses service workers to detect network idle state.
- network-connection-class – A library for Android by facebook that gives a moving average of connection speed.
Metadata
Metadata
Assignees
Labels
No labels