@ogygia/image
Deployment
The encoder makes your image files on a live server, sharp() at the app's own endpoint or cloudflare() and vercel() through the platform's image CDN. A prerendered page's files are always made by the build, so a static site needs nothing more.
On this page
- Node and static hosts
- Runtimes without sharp
- Cloudflare Workers
- Links are not signed
- Cloudflare’s files differ a little
- Vercel
- Set Vercel’s widths and qualities
- What the build checks
- Remote images on Vercel
- Encoder support
- Why dev refuses more than production
- Encoder support
- Why dev refuses more than production
A prerendered page is the easy case: the build makes every file it shows, with sharp, and any static host serves them. Pages rendered on request need someone to make files at run time. That’s the encoder, image({ encoder }):
| Encoder | Who makes a live page’s files | Runs on |
|---|---|---|
sharp() | the app’s own image endpoint, with sharp (the default) | a Node server, dev |
cloudflare() | Cloudflare’s Image Transformations links, or the Worker’s Images binding | Cloudflare Workers |
vercel() | Vercel’s Image Optimization links, for plain resizes | Vercel, usually listed with sharp() after it |
All three come from @ogygia/image/vite. The dev server always makes files with sharp, but refuses what your encoders can’t make on request, on every page (Why dev refuses more than production).
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { image, sharp } from '@ogygia/image/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
ogygia(),
image({ encoder: sharp() }),
sveltekit()
]
});Node and static hosts
sharp() is the default. The app’s image endpoint makes every live file, and the build every file a prerendered page names. List sharp in your app’s dependencies for a server build (Where sharp must be listed). With jumpstart(), also list it in external, and @resvg/resvg-js too if you draw social cards: both load native addons.
It makes AVIF at effort 2 (of 0 to 9): about twice as fast as 4, for files about 0.2% larger. To spend more time on smaller files, raise it with sharp({ effort: { avif } }), a whole number from 0 to 9, else image_option_invalid. It applies to every file sharp makes: the build’s, the dev server’s and the endpoint’s.
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { image, sharp } from '@ogygia/image/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
ogygia(),
image({
encoder: sharp({ effort: { avif: 6 } })
}),
sveltekit()
]
});Runtimes without sharp
sharp is a native Node module. A runtime that can’t load it (an edge or Workers runtime, or an install that skipped sharp’s binary) answers each image request with a 500 and says image_no_encoder once. Prerendered pages and warmed presets are made by the build as static files, so they work there.
The build knows an edge adapter by its package: Cloudflare’s, Deno’s, or one whose name has -edge. For those:
- On Cloudflare (adapter-cloudflare, adapter-cloudflare-workers), list
cloudflare(). - On any other edge adapter, write
encoder: sharp()to say that only prerendered pages show images. The build then warns once withimage_no_encoderinstead of failing. - Listing no encoder on an edge adapter fails the build with
image_option_invalid, so you choose.
adapter-netlify with edge: true and adapter-vercel with runtime: 'edge' aren’t seen as edge builds, so they neither fail nor warn; choose the encoder yourself there. adapter-vercel’s deprecated runtime: 'edge' can’t build an ogygia server today anyway: its bundle can’t import Node’s crypto and fs, which ogygia’s server uses.
Cloudflare Workers
On adapter-cloudflare, list cloudflare(). A prerendered page’s files are still made by the build, with sharp, as static files, so sharp is needed to build only and devDependencies is enough. A page the Worker renders on request names its files one of two ways:
- Links (
links: true, the default): each file is an Image Transformations link,/cdn-cgi/image/width=640,height=360,fit=cover,quality=60,format=avif/_app/immutable/assets/hero.Ab12.jpg, made and kept by Cloudflare’s CDN. Turn transformations on for the zone. The options come in one fixed order, so a file has one URL, and an island names the same links in the browser, with no key.wrangler devdoesn’t serve/cdn-cgi/image, so these files load only once deployed. - The Images binding (
links: false): the Worker’s own image endpoint makes each file with its Images binding and keeps it in the Worker’s cache (caches.default), sentpublic, max-age=31536000, immutable. Use it where the zone has no transformations.wrangler devruns it locally.
With links on, the few files a link can’t name (a PNG made from another format) still go to the endpoint and its binding, so add the binding either way.
| Option | Default | What it does |
|---|---|---|
links | true | Name files as Image Transformations links. false: the endpoint makes every file with the Images binding. |
binding | 'IMAGES' | The Images binding’s name in the Worker’s wrangler config. Letters, digits, _ and $, not starting with a digit. |
Add the binding to the wrangler.jsonc your adapter-cloudflare app already has, beside its main, assets and compatibility_date:
A Worker with no such binding answers each file it would make 500 and says image_binding_missing once. cloudflare() on any adapter but adapter-cloudflare or adapter-cloudflare-workers fails the server build with image_option_invalid: its links and binding exist only on Cloudflare. adapter-auto counts as another adapter, since it can’t be handed a platform’s config: list @sveltejs/adapter-cloudflare itself.
Links are not signed
Once transformations are on, anyone can ask /cdn-cgi/image for any of your originals at any size, quality or option, and Cloudflare bills each one it makes. Keep the zone’s transformation sources at their default (this zone only), never “any origin”. For a door no one else can open, use links: false: the Worker’s endpoint signs every file.
A remoteImage() with links is /cdn-cgi/image/<options>/https://images.cms.example.com/…, so the zone must allow that origin: in the dashboard, under Images, Transformations, list the CMS’s host as a source. Allowing any origin lets anyone resize any image through your zone.
Cloudflare’s files differ a little
Cloudflare and sharp blur, sharpen and scale differently, so their files differ a little. An animated image keeps its frames, but Cloudflare delivers one past 50 megapixels over all its frames untransformed. AVIF past 1200 pixels a side comes back as WebP or JPEG. A remote source is cropped with fit=crop, so it’s never enlarged. The Worker’s bundle holds no sharp.
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { cloudflare, image } from '@ogygia/image/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
ogygia(),
image({
encoder: cloudflare({ links: false })
}),
sveltekit()
]
});{
"name": "my-app",
"main": ".svelte-kit/cloudflare/_worker.js",
"compatibility_date": "2025-01-01",
"compatibility_flags": ["nodejs_compat"],
"assets": { "binding": "ASSETS", "directory": ".svelte-kit/cloudflare" },
"images": { "binding": "IMAGES" }
}Vercel
On adapter-vercel, vercel() names each file of a page rendered on request as a link to Vercel’s Image Optimization, /_vercel/image?url=%2F_app%2Fimmutable%2Fassets%2Fhero.Ab12.jpg&w=640&q=60, made and kept by Vercel’s CDN. An island names the same links in the browser. A prerendered page’s files are still made by the build, with sharp, as static files.
Vercel only resizes. It keeps the source’s shape, makes only the widths its images.sizes lists and the qualities its images.qualities lists, and picks AVIF or WebP from the browser’s Accept header. It can’t crop (an aspect, a height with a width), place a crop (position), apply an operation (ops), name PNG or JPEG, or resize a GIF, a TIFF or an animated image (it sends those as they are). So list sharp() after it: the first encoder that can make a file makes it, here Vercel’s link for a plain resize and the app’s own sharp endpoint for the rest.
Keep the encoder in one file so svelte.config.js and vite.config.ts read the same one, and hand its images to the adapter.
Set Vercel’s widths and qualities
| Option | Default | What it does |
|---|---|---|
sizes | 16, 32, 48, 64, 96, 128, 256, 384, then the default breakpoints (320 to 2048) | The widths Vercel may make, 1 to 99999. Every width a page asks for is snapped up to one of these, or to the image’s own width: every file of the app, sharp’s crops and the prerendered files too, so one ladder serves all. Keep the small widths when you list your own: without 48 and 96, a 48-pixel fixed avatar is made 256 wide. |
qualities | [60, 80] (AVIF’s and WebP’s defaults) | The qualities Vercel may make, 1 to 100. A plugin, preset or source quality must be one of these, or the build fails with image_option_invalid. |
VERCEL.images is the adapter’s images config: { sizes, qualities, formats: ['image/avif', 'image/webp'], domains: [], localPatterns: [{ pathname: '^/_app/immutable/assets/.+$', search: '' }] }.
What the build checks
Once adapter-vercel has written .vercel/output/config.json, the build reads its images and fails with image_vercel_config, printing the exact value to pass, when:
- a width, quality or format the app names is missing;
- a full-URL
paths.assetshas a host missing fromdomainsorremotePatterns; - a host of
image({ remote: { hosts } })is missing fromremotePatterns; imagesopens more than the app needs: noqualities(any quality),localPatternsunset or wider than the app’s originals (the optimizer could read any path of the deployment, a page or an endpoint included), orremotePatternsthat reach a hosthostsdoesn’t list;- the adapter isn’t adapter-vercel (adapter-auto included):
/_vercel/imageexists only on Vercel.
With another paths.base or appDir, the message gives the localPatterns to pass.
Remote images on Vercel
A remoteImage() link is /_vercel/image?url=https%3A%2F%2Fimages.cms.example.com%2F…, and Vercel fetches only from the hosts its images.remotePatterns lists. vercel() can’t know your hosts, so add one entry per host; the build checks that they reach every host in hosts and no other.
import { sharp, vercel } from '@ogygia/image/vite';
export const VERCEL = vercel({
sizes: [48, 96, 256, 640, 960, 1280, 1920]
});
export const encoder = [VERCEL, sharp()];import adapter from '@sveltejs/adapter-vercel';
import { VERCEL } from './image.config.ts';
export default {
kit: { adapter: adapter({ images: VERCEL.images }) }
};import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { image } from '@ogygia/image/vite';
import { defineConfig } from 'vite';
import { encoder } from './image.config.ts';
export default defineConfig({
plugins: [ogygia(), image({ encoder }), sveltekit()]
});import adapter from '@sveltejs/adapter-vercel';
import { VERCEL } from './image.config.ts';
export default {
kit: {
adapter: adapter({
images: {
...VERCEL.images,
+++ remotePatterns: [
{
protocol: 'https',
hostname: '^images\\.cms\\.example\\.com
### Links are not signed {#vercel-links}
Anyone can ask `/_vercel/image` for any of your originals at any width in `sizes` and any quality in `qualities`, and Vercel bills each one it makes. That's why the defaults are tight: list only the widths and qualities your pages use. For a door no one else can open, leave `vercel()` out: the sharp endpoint signs every file.
### Use `vercel()` without sharp {#vercel-alone}
With `vercel()` alone (no `sharp()` after it), the server holds no encoder, and the function's bundle no sharp; sharp is needed to build only.
- A page rendered on request that asks for a file Vercel can't make fails to render with [`image_encoder_cannot`](/e/image_encoder_cannot), naming the need. In dev every page does ([see above](#dev)).
- No moving image offers a reduced-motion still ([`image_still_left_out`](/e/image_still_left_out)), on prerendered pages too ([#2290](https://github.com/ogygiajs/ogygia/issues/2290)).
- A share card rendered on request is still drawn by the function at its own size, but one that shows an image fails with [`image_card_source`](/e/image_card_source), since only sharp fits a card's images.
- A share card shown through `<Img>` or `getImage()` on a live page asks for resized AVIF and WebP files, which nothing can make: each answers `500` with [`image_settle_failed`](/e/image_settle_failed). It works in dev, where sharp makes them ([#2290](https://github.com/ogygiajs/ogygia/issues/2290)). Use the card's `src` as it is, or prerender the page.
Each width keeps its two `<source>` rows (AVIF at quality 60, WebP at 80): the browser picks the row, Vercel the format. `vercel dev` doesn't run the optimizer, so these files load only once deployed.
## Encoder lists {#lists}
`encoder` takes one encoder or a list in order of priority: the first that can make a file makes it. A list holds:
- one platform at most (`cloudflare()` or `vercel()`);
- at most one encoder that makes files at the app's endpoint (`sharp()`, or `cloudflare()` with its binding);
- `sharp()` last, since it makes every file and nothing after it would be asked.
Each encoder at most once. Any other list fails the build with [`image_option_invalid`](/e/image_option_invalid): `[sharp(), vercel()]`, `[cloudflare(), sharp()]`, `[cloudflare(), vercel()]`.
```ts title=image.config.ts
import { sharp, vercel } from '@ogygia/image/vite';
export const encoder = [vercel(), sharp()]; // fine
// [sharp(), vercel()] vercel() is never asked
// [cloudflare(), sharp()] two encoders make files at the endpointEncoder support
A page rendered on request that asks an encoder for something it can’t make, with nothing after it that can, fails to render with image_encoder_cannot, naming the encoder and the need. Pick another position or operation, prerender the page, or warm the preset: the build makes prerendered and warmed files with sharp. That fixes production only: dev still fails (below).
| A file that needs | sharp | Cloudflare (links and binding) | Vercel |
|---|---|---|---|
| a resize | yes | yes | yes, to a width in sizes |
| a box, cropped to cover | yes | fit=cover (fit=crop for a remote source: never enlarged) | no |
position center | yes | yes (the default) | yes (nothing is cut) |
position top, bottom, left, right | yes | gravity= | no |
position: 'attention' | yes | gravity=auto | no |
position: 'entropy' | yes | no | no |
rotate 90, 180, 270 | yes | rotate= | no |
rotate by any other angle | yes | no | no |
flip, flop | yes | flip=v, flip=h | no |
grayscale | yes | saturation=0 | no |
modulate brightness, saturation | yes | brightness=, saturation= | no |
blur, sharpen | yes | blur= (twice the sigma), sharpen=1 | no |
normalize, negate, tint, modulate hue, lightness | yes | no | no |
gamma | yes | no (Cloudflare’s gamma is an exposure curve) | no |
| AVIF, WebP | yes | format= (AVIF past 1200 pixels a side comes back as WebP or JPEG) | from the Accept header |
| JPEG | yes | format=jpeg | no |
| PNG from a PNG source | yes | yes (a link names no format=, so the PNG source’s own is kept) | no |
| PNG from any other source | yes | the binding only (a link’s format= takes no png) | no |
| an AVIF source | yes | no (Cloudflare reads AVIF on an Enterprise plan only) | yes |
| a GIF or TIFF source | yes | yes | no (sent as it is) |
| an animated source | yes | yes | no (sent as it is) |
| a moving image’s first frame | yes | anim=false | no |
Why dev refuses more than production
The dev server makes every file with sharp, since no platform CDN runs there, but it checks each file against your encoder list as if the page rendered on request. So a crop, operation or format your encoders can’t make fails with image_encoder_cannot on every page in dev, even a page you prerender or a preset you warm, where production is fine because the build makes those files with sharp (#2290).
Until that changes, the page works in dev only when the list can make the file. On Vercel, [vercel(), sharp()] does. With cloudflare(), which takes no sharp() after it, only a crop or operation Cloudflare can make works in dev.
} ] +++ } })
} };
### Links are not signed {#vercel-links}
Anyone can ask `/_vercel/image` for any of your originals at any width in `sizes` and any quality in `qualities`, and Vercel bills each one it makes. That's why the defaults are tight: list only the widths and qualities your pages use. For a door no one else can open, leave `vercel()` out: the sharp endpoint signs every file.
### Use `vercel()` without sharp {#vercel-alone}
With `vercel()` alone (no `sharp()` after it), the server holds no encoder, and the function's bundle no sharp; sharp is needed to build only.
- A page rendered on request that asks for a file Vercel can't make fails to render with [`image_encoder_cannot`](/e/image_encoder_cannot), naming the need. In dev every page does ([see above](#dev)).
- No moving image offers a reduced-motion still ([`image_still_left_out`](/e/image_still_left_out)), on prerendered pages too ([#2290](https://github.com/ogygiajs/ogygia/issues/2290)).
- A share card rendered on request is still drawn by the function at its own size, but one that shows an image fails with [`image_card_source`](/e/image_card_source), since only sharp fits a card's images.
- A share card shown through `<Img>` or `getImage()` on a live page asks for resized AVIF and WebP files, which nothing can make: each answers `500` with [`image_settle_failed`](/e/image_settle_failed). It works in dev, where sharp makes them ([#2290](https://github.com/ogygiajs/ogygia/issues/2290)). Use the card's `src` as it is, or prerender the page.
Each width keeps its two `<source>` rows (AVIF at quality 60, WebP at 80): the browser picks the row, Vercel the format. `vercel dev` doesn't run the optimizer, so these files load only once deployed.
## Encoder lists {#lists}
`encoder` takes one encoder or a list in order of priority: the first that can make a file makes it. A list holds:
- one platform at most (`cloudflare()` or `vercel()`);
- at most one encoder that makes files at the app's endpoint (`sharp()`, or `cloudflare()` with its binding);
- `sharp()` last, since it makes every file and nothing after it would be asked.
Each encoder at most once. Any other list fails the build with [`image_option_invalid`](/e/image_option_invalid): `[sharp(), vercel()]`, `[cloudflare(), sharp()]`, `[cloudflare(), vercel()]`.
```ts title=image.config.ts
import { sharp, vercel } from '@ogygia/image/vite';
export const encoder = [vercel(), sharp()]; // fine
// [sharp(), vercel()] vercel() is never asked
// [cloudflare(), sharp()] two encoders make files at the endpointEncoder support
A page rendered on request that asks an encoder for something it can’t make, with nothing after it that can, fails to render with image_encoder_cannot, naming the encoder and the need. Pick another position or operation, prerender the page, or warm the preset: the build makes prerendered and warmed files with sharp. That fixes production only: dev still fails (below).
| A file that needs | sharp | Cloudflare (links and binding) | Vercel |
|---|---|---|---|
| a resize | yes | yes | yes, to a width in sizes |
| a box, cropped to cover | yes | fit=cover (fit=crop for a remote source: never enlarged) | no |
position center | yes | yes (the default) | yes (nothing is cut) |
position top, bottom, left, right | yes | gravity= | no |
position: 'attention' | yes | gravity=auto | no |
position: 'entropy' | yes | no | no |
rotate 90, 180, 270 | yes | rotate= | no |
rotate by any other angle | yes | no | no |
flip, flop | yes | flip=v, flip=h | no |
grayscale | yes | saturation=0 | no |
modulate brightness, saturation | yes | brightness=, saturation= | no |
blur, sharpen | yes | blur= (twice the sigma), sharpen=1 | no |
normalize, negate, tint, modulate hue, lightness | yes | no | no |
gamma | yes | no (Cloudflare’s gamma is an exposure curve) | no |
| AVIF, WebP | yes | format= (AVIF past 1200 pixels a side comes back as WebP or JPEG) | from the Accept header |
| JPEG | yes | format=jpeg | no |
| PNG from a PNG source | yes | yes (a link names no format=, so the PNG source’s own is kept) | no |
| PNG from any other source | yes | the binding only (a link’s format= takes no png) | no |
| an AVIF source | yes | no (Cloudflare reads AVIF on an Enterprise plan only) | yes |
| a GIF or TIFF source | yes | yes | no (sent as it is) |
| an animated source | yes | yes | no (sent as it is) |
| a moving image’s first frame | yes | anim=false | no |
Why dev refuses more than production
The dev server makes every file with sharp, since no platform CDN runs there, but it checks each file against your encoder list as if the page rendered on request. So a crop, operation or format your encoders can’t make fails with image_encoder_cannot on every page in dev, even a page you prerender or a preset you warm, where production is fine because the build makes those files with sharp (#2290).
Until that changes, the page works in dev only when the list can make the file. On Vercel, [vercel(), sharp()] does. With cloudflare(), which takes no sharp() after it, only a crop or operation Cloudflare can make works in dev.