@ogygia/image
Image files
On a live server and in dev, each image file is a signed URL the image endpoint makes on the first request and keeps; on a prerendered page, the build makes it as a static file named by what it holds. This page covers those URLs, warm, the caches, the headers and the limits.
On this page
A page can name dozens of image files, and none of them exists until someone makes it. Making a file costs time and memory, so you want each one made once, kept, cached by every browser and CDN for as long as possible, and never confused with a file made from an older version of the image. You also don’t want strangers asking your server for sizes no page uses.
ogygia gets there by naming each file after a recipe: the source’s id, the width and height, the format, and any quality, crop position, still frame or pixel operations. The recipe is the file’s name. Whoever makes the file reads the original, checks it’s still the same source, and encodes it.
What changes is who makes the file and what URL names it:
| Where the page renders | Who makes each file | URL |
|---|---|---|
| dev | the dev server, on the first ask | signed, under /__ogygia__/i/ |
| a live server | the image endpoint, on the first ask | signed, under /__ogygia__/i/ |
| a live server, a warmed preset | the build | static, under _app/immutable/og/ |
| a prerender | the build | static, under _app/immutable/og/ |
a live server with cloudflare() or vercel() | the platform’s CDN, for what its links can name; the endpoint for the rest (every file with cloudflare({ links: false })); vercel() alone has no endpoint, so the rest fails with image_encoder_cannot | the platform’s link, or signed (Deployment) |
<base>/__ogygia__/i/<signature>/<id>-<w>x<h>[-q<quality>][-p<position>][-s][-o<ops>]/<original path>.<ext>
<assets or base>/_app/immutable/og/<id>-<w>x<h>[-q<quality>][-p<position>][-s][-o<ops>].<ext>Signed URLs
On a live server and in dev, each file URL is a recipe the server signed with the app’s key: the same key that signs ogygia’s regions, handle({ secret }) or OGYGIA_SECRET (OGYGIA_SECRET). Nobody can ask the endpoint for a size no page asked for. A changed or unsigned URL is refused with a 403 that says why (x-ogygia-deny: sig) and names the fingerprint of the key it checked with (x-ogygia-key), and the page keeps the placeholder.
The endpoint lives at /__ogygia__/i/ under Kit’s paths.base, whatever endpoint ogygia’s handle uses. It can’t sit under Kit’s appDir (/_app/…): Kit answers a missing file there with a 404 before any hook runs.
Without OGYGIA_SECRET, each server build signs with a random key of its own, so a server-rendered page’s file URLs change with each deploy and a CDN keeps the old ones for nothing. Set the secret to keep them stable. A prerendered page’s files need no secret.
- Only
GETandHEAD. Any other method answers405. - A query string is ignored. A file URL asked with one (
…/hero.jpg.avif?v=2) answers a permanent redirect (308) to the bare URL, so a CDN keeps one copy and nobody can make the server encode again by changing the query. - A path that names no recipe, or an original the endpoint may not read, answers
404.
The same source, or a 404
Every file name starts with the source’s id, a hash of the image’s bytes. A changed image gets a new id, so new URLs, and a CDN can keep the old ones forever.
Whoever makes a file (the dev server, the build, a live server) checks the original it reads against that id first. A page rendered before you edited hero.jpg, asking for the old file, gets a 404 with image_source_changed, never the new pixels under the old name. Reload the page and it names the new files. On a live server, a descriptor whose id and src name two different files (one built by hand, or stored in a CMS) gets the same answer: make descriptors with the macro.
Where the endpoint keeps files
The endpoint makes a file the first time it’s asked for, keeps it on disk, and sends it with cache-control: public, max-age=31536000, immutable. All asks for one file share one encode.
- In dev and during a build, files are kept in the build cache,
node_modules/.ogygia/image/files/, at most 2 GB. A CI run that keeps that folder makes nothing twice. - A live server keeps them under the system’s temporary folder, in
ogygia-image-<uid>/, a folder only its user can open, at most 512 MB while it runs. If another user’s folder, or a link, sits there, the server keeps nothing and saysimage_store_unsafe: such a folder could hand out files it chose under URLs browsers keep forever.
Past either size, the least recently asked files go first. Files nobody asked for in a month go when the server starts. Each process counts its own files against the size (a build and its prerender worker each keep their own count). Files are kept per encoder version, libvips version and AVIF effort, so an upgrade or a new setting never serves a file made the old way.
A file the disk can’t keep (a full or read-only temporary folder) is still answered, and the server says so once with image_store_write_failed. Until the disk can write again, it keeps up to 64 MB of the files it made in memory.
How many files are made at once
A few encodes run at once: half the machine’s cores, between 1 and 4 (4 with Cloudflare’s Images binding). On a live server at most 64 more asks wait, each for at most 30 seconds. Past that, an ask answers 503 with retry-after: 5 and image_busy, which no cache keeps; the browser keeps the placeholder, and the next visit gets the file. remoteImage() fetches have a line of their own of the same shape, with 16 at once. The build and the dev server queue every ask, since their pages name each file once.
To keep the first visitors from waiting, warm the presets your server-rendered pages use, or put a CDN in front: it asks once per file.
A file that can’t be made answers 500 with image_settle_failed, and the page keeps its placeholder. A server that can’t load its encoder answers 500 with image_no_encoder. Every answer that refuses (403, 404, 405, 500, 503) is sent with cache-control: no-store, so no cache keeps it.
GET /__ogygia__/i/Hq3…/Xk2jQ81mZaPq0Lw7-640x360/_app/immutable/assets/hero.Bx1.jpg.avif
200 content-type: image/avif
cache-control: public, max-age=31536000, immutableBuild output
The files of a prerendered page, and of a warmed preset, are made by the build and written as static files under Kit’s _app/immutable/og/, at paths.assets when you set it and paths.base otherwise. Every adapter and static host serves that folder as files kept forever.
Each name is the recipe alone: the source’s content hash, the size, and any crop or quality. Nothing in it is signed, so it needs no OGYGIA_SECRET. Two builds of the same images name the same files, a deploy re-uploads only the images that changed, and HTML cached elsewhere (a feed reader, a CDN) keeps working across deploys.
- Each file starts the moment the page renders, several side by side. When the prerender ends, the build writes every file a prerendered page named, in its markup or through
getImage()in a CSS background, with Kit’s crawler on or off. A site built withadapter-statichas every image file it uses, and serves them with no server at all. - A file that can’t be made fails the build with
image_unsettled, rather than ship a page with a missing image. - An SPA fallback page (
adapter-static({ fallback: '404.html' })) renders after the build wrote its files, so it names them as they are, or the original where no prerendered page asked for that file. - Pages rendered on a live server keep signed URLs, except a warmed preset’s.
Image URLs on a prerendered page
While a page prerenders, each image URL starts with the build’s mark, //<random>.ogygia.invalid. Kit’s crawler would otherwise ask for a file under _app/ that isn’t there yet, and Kit answers that with a 404 before any hook runs. Once the files exist, the build removes the mark from every prerendered file.
So use each URL as <Img> or getImage() gives it, or make it absolute with new URL(src, page.url) and a real kit.prerender.origin (Make a URL absolute). A URL changed any other way (escaped, or encoded into a query string) can’t be found, and fails the build with image_mark_left. A prerendered redirect to an image URL can keep the mark in Kit’s redirect list (#2240).
Under vite build --watch on SvelteKit 3, a rebuild prerenders straight from Kit’s watcher, after every plugin hook, so its pages can only name files a build already made. A page that names a new one stops the rebuild with image_watch_rebuild: run vite build again, or warm the image. Build bookkeeping messages lists the rest of the watch-mode messages.
<img
src="/_app/immutable/og/Xk2jQ81mZaPq0Lw7-1280x720.webp"
srcset="
/_app/immutable/og/Xk2jQ81mZaPq0Lw7-640x360.webp 640w,
…
"
…
/>Warming with warm
A prerendered page needs nothing more: the build makes every file it shows. A page rendered on a live server asks on its first visit, and that visitor waits while the files are made. warm makes a preset’s files at build instead.
Pass a preset name, or a list of them, to the macro. The build writes those files under _app/immutable/og/, like a prerendered page’s, and <Img of={hero} preset="cover"> names them directly, on the server and in an island alike. Every twin’s files are made too.
- A prop that changes the files (
<Img of={hero} preset="cover" width={300}>: a layout, size, crop orart) asks for other files, which the endpoint makes as usual.sizes,priority,placeholderandmorphchange no file, so they keep the warmed ones. - A name the config doesn’t declare fails the build with
image_warm_unknown. - In dev every file comes from the endpoint, warmed or not.
- On an edge runtime, or with an encoder that can’t make a crop or operation on request, warmed files are made by the build with sharp, so they work in production, though dev still refuses them (Why dev refuses more than production).
export const hero = import.meta.og.image('./images/hero.jpg', {
warm: 'cover'
});
export const team = import.meta.og.image('./images/team.jpg', {
warm: ['cover', 'thumb']
});Cache headers
ogygia writes no header file. Each adapter already sends Kit’s _app/immutable/ files with a header that keeps them forever, and the build’s files sit there. What each one sends (the last column matters when a page asks for a file the server doesn’t have, as when a deploy is half rolled out):
| Adapter | Who sends it | A file the build made | A file under _app/immutable/ that isn’t there |
|---|---|---|---|
adapter-node | the adapter’s file server | public,max-age=31536000,immutable | 404, public, max-age=0, must-revalidate |
adapter-cloudflare | Kit’s rule, appended to _headers | public, immutable, max-age=31536000 | 404, with the same rule (under wrangler dev): see below |
adapter-vercel | a route in .vercel/output/config.json | public, immutable, max-age=31536000 | 404, no-store |
adapter-netlify | Kit’s rule (_headers in version 6, the Frameworks API config in 7) | public, immutable, max-age=31536000 | the host’s 404 |
adapter-static | your host | the rule below, if your host takes one | the host’s 404 |
A file the image endpoint makes is sent with public, max-age=31536000, immutable on every adapter.
Give a static host the rule
With adapter-static, give the host the rule yourself. On Cloudflare Pages and Netlify, put it in static/_headers; the adapter copies it to the site’s root. On Vercel, put the same rule in vercel.json. GitHub Pages takes no headers and sends max-age=600: a browser checks the file again after ten minutes and gets the same bytes, since a file’s name is what it holds.
A 404 kept for a year on adapter-cloudflare
On adapter-cloudflare, a missing file under _app/immutable/ comes back 404 with the keep-forever header: Kit’s Worker hands every such path to the static assets, and the _headers rule covers the 404 too. A browser may then keep that 404 for a year.
It happens when a page from a new deploy reaches an old one’s assets (a gradual deployment, or two versions live at once): that browser shows a broken image until its cache is cleared. Kit’s own scripts share this, but Kit reloads the page when its version changes, and an image doesn’t. No _headers line can fix it, since a rule can’t tell a 404 from a file, so it waits on the adapter. Until then, deploy all at once (no gradual rollout) on Cloudflare Workers.
/_app/immutable/*
Cache-Control: public, immutable, max-age=31536000{
"headers": [
{
"source": "/_app/immutable/(.*)",
"headers": [
{
"key": "Cache-Control",
"value": "public, immutable, max-age=31536000"
}
]
}
]
}