In-Browser Performance Linting With Feature Policies

Tim Kadlec has a simple trick for spotting images that are too large and should be optimized: he toggles custom browser headers using the Modheader extension. Running this in a local environment is useful because the browser raises an error and refuses to display the offending images at all.

The mechanism, as Tim explains, is the Feature Policy header paired with the oversized-images policy. He turns it on with:

Feature-Policy: oversized-images ‘none’;

Tim's reasoning:

By default, if you provide the browser an image in a format it supports, it will display it. It even helpful scales those images so they look great, even if you've provided a massive file. Because of this, it's not immediately obvious when you've provided an image that is larger than the site needs.

The oversized-images policy tells the browser not to allow any images that are more than some predefined factor of their container size. The recommended default threshold is 2x, but you are able to override that if you would like.

The appeal here is putting the browser to work doing linting for us — and it raises the question of what other guard rails the browser could enforce to head off future mistakes.