Modern frameworks are good at images. Next.js has <Image>, Nuxt has
<NuxtImg>, and Astro has astro:assets. Each of them writes a
correct srcset, reserves space so the layout does not jump, and lazy-loads what
is below the fold. Where they differ is in who makes the pixels. Out of the box that
is either your build (Astro's default service and static exports generate every size at build
time) or your server (Next.js runs an optimiser on the same machine that renders your pages).
Both get uncomfortable as a site grows. Build-time generation multiplies build minutes by the
number of images times the number of widths. A server-side optimiser spends CPU you are paying
for on resizing JPEGs instead of rendering pages, and on serverless hosting it is billed per
invocation. This article shows how to keep each framework's component exactly as it is and
hand the pixels to an image CDN instead, with the @zawcloud/images
package.
What changes, and what does not
The framework still decides which widths to ask for and writes the
srcset. The only thing that changes is the URL each candidate points at. Instead
of /_next/image?url=… or a hashed file in dist/, it is a delivery URL
on your account's hostname:
https://a3f9c21b.zwcld.net/photos/hero.jpg?w=1200&q=75
The first browser to request a given width causes the edge to fetch the original from your origin, resize it, pick AVIF or WebP for that browser, and cache it. Nothing is generated at build time, and your application server never touches image bytes.
The hostname identifies your account, so no API key goes into your front-end code. The URLs are safe to build in the browser, in a server component, or in a static page. That matters, because anything in a client bundle is public.
npm install @zawcloud/images
Next.js
Next.js supports a custom loader: a function that receives src,
width and quality and returns a URL. Point the config at a loader
file:
// next.config.js
module.exports = {
images: {
loader: 'custom',
loaderFile: './zawcloud-loader.js',
},
};
// zawcloud-loader.js
import { createLoader } from '@zawcloud/images/next';
export default createLoader({ baseUrl: 'https://a3f9c21b.zwcld.net' });
Your components do not change. <Image src="/photos/hero.jpg" width={1200}
height={800} sizes="100vw" alt="" /> now produces a srcset whose every
entry is served from the edge. To set the hostname per environment instead, set
NEXT_PUBLIC_ZAWCLOUD_URL and re-export the package's default loader. A
quality on an individual <Image> still wins over the default.
This also makes the built-in optimiser's own concerns go away: no sharp in your
deployment, no remotePatterns allow-list to maintain, and no optimiser cache on
a disk that serverless platforms throw away. The longer story is in
next/image without the built-in
optimiser.
Nuxt Image
@nuxt/image has providers. Register the package as one:
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['@nuxt/image'],
image: {
provider: 'zawcloud',
providers: {
zawcloud: {
provider: '@zawcloud/images/nuxt',
options: { baseURL: 'https://a3f9c21b.zwcld.net' },
},
},
},
});
<NuxtImg src="/photos/hero.jpg" width="1200" sizes="sm:100vw md:50vw" fit="cover" />
Nuxt's fit names are translated to the edge's: contain letterboxes, so it becomes
pad, with the background modifier as bg;
inside becomes contain; and outside becomes
cover. Any other modifier is passed to the edge under its own name, so
:modifiers="{ blur: 20 }" or { preset: 'card' } works without a
special case.
Astro
Astro lets you replace its image service. Swap it in the config, and
<Image> and <Picture> generate delivery URLs instead of
processing files during the build:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
image: {
service: {
entrypoint: '@zawcloud/images/astro',
config: { baseUrl: 'https://a3f9c21b.zwcld.net' },
},
},
});
Astro's named qualities (low, mid, high,
max) are mapped to numbers, and SVGs pass through untouched. One thing to know:
an image you import (import hero from './hero.jpg') gets a built path
like /_astro/hero.a1b2c3.jpg. The edge can fetch that only if your account's
origin is your deployed site. Otherwise, reference images by string path on your image
origin, which is usually simpler anyway: images can then change without a rebuild.
Anywhere else
The package has no dependencies and no build step, and its core is one function. It works in SvelteKit, Remix, a Rails or Django template helper ported to JavaScript, or a React Native app:
import { imageUrl } from '@zawcloud/images';
imageUrl('/photos/hero.jpg', { width: 800, fit: 'cover', params: { blur: 10 } }, {
baseUrl: 'https://a3f9c21b.zwcld.net',
});
Options are always written in the same order, so one image at one size is always the same
URL, and every cache along the way can reuse it. A value the edge would refuse, such as a
quality of 0 or fit: 'stretch', throws in your code instead of becoming a
broken image in production.
Decisions worth making once
Which widths
Next.js asks for widths from deviceSizes and imageSizes, and its
defaults are a long list. Every width is a separate rendition per image. Trimming the list
to five or six values that match your breakpoints keeps the cache small and the hit rate
high. Then put the same list in permitted output sizes on the console's
Limits tab, so anything else is refused instead of generated. See
Responsive images: the widths
you actually need.
Format
Leave it alone. The package leaves f out of the URL by default, which means
f=auto on the edge: AVIF where the browser supports it, WebP where it does not,
and the original format otherwise. Setting format explicitly only makes sense
when you know something the Accept header does not, such as needing a PNG for a
download.
Presets for design decisions
When a design decision such as "cards are 4:3, cropped to fill, WebP at quality 70" lives
in a component, changing it means a deploy. Put it in a named preset on the
console and pass preset: 'card'. The edge expands the preset into ordinary
parameters, and any explicit option still overrides it, so the framework's chosen width
always applies.
Placeholders
Frameworks support a blurred placeholder, but generating one usually means reading the image
at build time. Every delivery URL also accepts ?thumbhash=true,
?blurhash=true or ?info=true, which return JSON with a compact
placeholder, dimensions and dominant colours. Fetch it when content is published, store it
next to the image path in your CMS, and pass it to the component as
blurDataURL or a background colour. See
Two attributes and thirty bytes stop your layout
jumping.
Checking it works
- Open a page and look at an image request in the browser's network panel.
- The URL should be on your delivery hostname, not
/_next/imageor/_astro/. - The response
Content-Typeshould beimage/aviforimage/webpin a current browser. - The response should carry
Cache-Control: public, …, immutableand anETag, so the browser keeps it and revalidates cheaply.
Each framework has its own page with copy-and-paste snippets: Next.js, Nuxt and Astro. Signed in, the console's Framework Integrations tab shows the same snippets with your own hostname already filled in.