Skip to content

Instantly share code, notes, and snippets.

View paulcollett's full-sized avatar

Paul Collett paulcollett

View GitHub Profile
@paulcollett
paulcollett / fetch-keep-alive.md
Last active March 6, 2025 02:13
Fetch "keepalive" Javascript Web feature detection

Fetch "keepalive" Javascript Web feature detection

fetch()'s keepalive is a new option parameter in Web API spec to replace the Navigator.sendBeacon() API for usecases where requests should outlive the current page, like tracking clicks.

Current Support

This new fetch option is supported in all modern browsers except Firefox (at time of writing, Firefox 110):

https://caniuse.com/mdn-api_request_keepalive

// There seems to be a few algorithms floating around for brightness/luminance detection.
// One uses NTSC `(299*R + 587*G + 114*B)` which is incorrect for web colors (sRGB) see
// `rgbLuminance` for "better" coefficients (seems subjective but agreed apon). To be more
// accurate you need to also convert RGB from sRGB color space (which gives more spectrum to lighter colors)
// to linear rgb which normalizes colors across the spectrum - better for processing.
// see https://stackoverflow.com/questions/596216/formula-to-determine-perceived-brightness-of-rgb-color
// convert sRGB to linear RGB values
// - channel ** 2.218 same as Math.pow((channel + 0.055) / 1.055, 2.4)
// - i've seen this simplified to be just `(channel / 255) ** 2.21`