A Payload site, ready to clone

Responsive images

Every image in several widths, in WebP: phones download small files, without any JavaScript.

What it does

When an image is uploaded, the Media library keeps the original and makes copies of it 480, 768, 1024, 1536 and 2048 pixels wide, in proportion and in WebP, a format much lighter than JPEG or PNG. An image narrower than a width gets no copy at it: nothing is enlarged. SVG files stay as they are.

Every image on the site then lists those copies in its srcset, with a sizes attribute that says how wide the image is on the page. The browser picks the smallest file that fills that space on its screen: a phone gets a 480 or 768 pixel WebP, a large screen a bigger one. This happens in the image with text block, the video poster, the posts lists, a post’s image, images in rich text and the templates of generated collections. The original stays the fallback for browsers that don’t read srcset, and the site logo and the feeds keep plain images.

See it in the demo

Open this page on a phone, or narrow the window, and look at the image below in the browser’s developer tools: in Network, its file is a .webp of 480 or 768 pixels; on a wide screen, a larger one. The same goes for the cards under News and every post’s image. In the admin, an image in Media lists its sizes.

For developers

The widths and the WebP quality are in src/lib/responsiveImage.ts, the sizes in src/collections/Media.ts. responsiveImage(media) gives a template its src, srcSet, width and height, and IMAGE_SIZES the sizes attribute for each place (apex-block-base 0.15.0 has the same helper for blocks, @omitsis/apex-block-base/image). Images uploaded before the sizes existed get them with pnpm media:regenerate (--dry-run lists them first); the originals are kept. The README section "Responsive images" says how much storage grows.

Placeholder: rounded tiles in soft blue and orange
A caption and a credit, from the image’s Media item: edit them in Media, and every block showing the image follows.Image: APEX demo, drawn by pnpm seed:demo

This image, in four widths

The original is a 1200 pixel JPEG. The page offers its 480, 768 and 1024 pixel WebP copies too, and your browser chose one to fit this column.

One block, every feature page

This strip is a reusable block: written once and placed at the end of every feature page. Edit it under Reusable blocks and all of them change.

All features