Conditional, Considerate Loading

A recent post on the Polypane blog — no byline, but it is Kilian Valkhof — makes the case for building sites that respect a user's data budget with the prefers-reduced-data media query. No browser supports it yet, per the article, but once they do, CSS authors will be able to make choices that cut data usage. The piece's sample: web fonts load only when the visitor hasn't signaled a preference for low data consumption.

@media (prefers-reduced-data: no-preference) {
  @font-face {
    font-family: 'Inter';
    font-weight: 100 900;
    font-display: swap;
    font-style: normal;
    font-named-instance: 'Regular';
    src: url('Inter-roman.var.woff2') format('woff2');
  }
}

body {
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont,
               Segoe UI, Ubuntu, Roboto, Cantarell, Noto Sans, sans-serif,
               'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
}

The pattern shares its spirit with accessibility work around prefers-reduced-motion, and both queries can be read from JavaScript as well.

In the same vein is Umar Hansa's post on conditional JavaScript, whose premise is that you should only download code when it is appropriate to do so. Hansa lays out plenty of examples, but the core idea is straightforward: the navigator object carries information about the device, the internet connection and user preferences, and pairing that with ES Modules lets you load resources conditionally with fairly little code.

if (navigator.connection.saveData === false) {
    await import('./costly-module.js');
}

For those who want to pursue the broader philosophy, Jeremy Wagner's Responsible JavaScript series is a good starting point.