@ogygia/image
Remote images
An image on another host works like a local file once you list its host. A URL you know at build is fetched into the build cache; a URL a CMS gives you at request time is remoteImage(), fetched by the image endpoint on the first ask and kept for remote.maxAge.
On this page
Not every image lives in your repository. A photo might sit on your own CDN, and a CMS hands you a cover’s URL only when the page is asked for. Both work with <Img> once you list the hosts your app takes images from. Nothing is fetched from a host you didn’t list.
There are two kinds of remote image, and they differ in when the URL is known:
| You know the URL… | Write | Fetched by | Kept |
|---|---|---|---|
| at build | import.meta.og.image('https://…') | the build, into its cache, asked again each build | forever, named by its bytes |
| only at request (a CMS entry) | remoteImage(url, { width, height }) | the image endpoint on the first ask, or the build | remote.maxAge (default one day) at the image endpoint, or the CDN’s own rules with cloudflare() links or vercel(); a prerendered page writes the file once, named by its bytes |
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { image } from '@ogygia/image/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
ogygia(),
image({
remote: {
hosts: ['images.example.com', '*.cdn.example.com']
}
}),
sveltekit()
]
});Allowed hosts
image({ remote: { hosts } }) is the list, and every other host is refused. An entry is a host name, with a port when it isn’t the default:
'images.example.com'takes that host only.'*.cdn.example.com'takes every name below it, one level or more:a.cdn.example.comanda.b.cdn.example.com, notcdn.example.comitself.'http://127.0.0.1:4100'takes plain HTTP from that host, for a test server or an intranet, and only HTTP: list the host again withouthttp://to take HTTPS from it too. An entry withouthttp://takeshttps://only.
An entry takes no path and no address range. *. never goes over a suffix anyone can host under, such as *.co.uk, *.github.io, *.vercel.app, *.workers.dev, *.s3.amazonaws.com, *.googleapis.com or a shared image CDN’s name (with or without DNS’s trailing dot): list your own name below it (*.mysite.github.io). An entry that names a host itself takes every path on it, so a storage host that serves every tenant’s files under one name lets every tenant’s images through. List your own bucket’s or account’s name where the service gives you one. A bad entry fails the build with image_option_invalid.
The other keys set the limits every remote fetch runs under:
| Option | Default | What it does |
|---|---|---|
hosts | required | The hosts above. At least one. |
maxBytes | 20_000_000 (20 MB) | The most bytes one image may have. The body is counted as it arrives and dropped past it. |
timeout | 10_000 (10 s) | How long one fetch may take, in milliseconds, its redirects and body included. |
maxAge | 86_400 (one day) | How long, in seconds, a file made from a remoteImage() is kept. 1 or more. |
What every fetch checks
The build and the image endpoint fetch the same way:
- Redirects are followed by ogygia itself, at most 3, and each one’s host must be listed too, so a listed host can’t hand over a file from one that isn’t.
- The answer must be a
200(or a304that keeps the build’s copy), uncompressed (nocontent-encodingbutidentity), with an imagecontent-type, and bytes that really are a PNG, JPEG, WebP, AVIF, GIF or TIFF. Anything else is refused, a206or a gzipped image included. - Private addresses. A host an entry names itself (
'cms.intranet.example','http://127.0.0.1:4100') is trusted wherever it points, so an intranet’s server or a test server works. A host taken only by a*.entry is a name someone below the listed one chose, so it’s fetched from only when every address it resolves to is public: never a loopback, private, link-local (a cloud’s metadata address), shared or reserved one. The check runs on the address the connection is made to, so a name can’t answer a public address first and a private one after. To fetch from a private host, list it by name.
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { image } from '@ogygia/image/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
ogygia(),
image({
remote: {
hosts: [
'images.example.com',
'*.cdn.example.com',
'http://127.0.0.1:4100'
],
maxBytes: 10_000_000,
timeout: 5_000,
maxAge: 3_600
}
}),
sveltekit()
]
});Build-time images
Give the macro a URL on a listed host. The build fetches it once into its cache, and from there it’s a file like any other: its id is the hash of its bytes, its original ships as one of the app’s assets, it gets a placeholder, warm works, and a prerendered page’s files are made by the build. Every option a local file takes works on it, and a local dark or art file can go with it.
A URL on any other host fails the build with image_remote_host, naming the line to add. A glob stays local: a remote image is one URL.
In markdown, a body image or a frontmatter URL on a listed host becomes a descriptor the same way (Images in markdown); one on any other host stays a plain <img> or string.
The build cache and offline builds
Each image is fetched once into node_modules/.ogygia/image/remote/, with its etag and last-modified. Every build asks again with them, once per image, and a 304 keeps the copy, so an unchanged image isn’t downloaded again. Keep that folder between CI runs, like the rest of the build cache. The dev server asks once per start.
- The host can’t be reached, or answers an error, and the build has a copy: it uses the copy and warns with
image_remote_stale. An offline build still works, but may show an old image. - No copy: the build fails with
image_remote_failed, saying why: the status, the timeout, too large, too many redirects, or not an image.
<script lang="ts">
import { Img } from '@ogygia/image';
const hero = import.meta.og.image(
'https://images.example.com/a/hero.jpg',
{ warm: 'cover' }
);
</script>
<Img of={hero} preset="cover" alt="The harbor at dusk" />node_modules/.ogygia/image/remote/
3f9a…c1.jpg the bytes
3f9a…c1.json its URL, etag and last-modifiedremoteImage()
A CMS page learns its images when it’s asked for. remoteImage(url, options) makes a descriptor of one in a server load, with no build step:
| Option | What it is |
|---|---|
width | The image’s own width, in pixels. Required, a whole number from 1 to 99999. |
height | The image’s own height. Required, the same. width × height is at most 268,402,689. |
alt | Alt text the image carries, read back as descriptor.alt. |
version | A string or number that changes when the bytes behind the URL do (a CMS revision). |
width and height are the image’s own: the page reserves its box from them, so ask the CMS for them. A file is never made larger than the image really is (sharp and Cloudflare only shrink a remote source), but the srcset is planned from the size you state, so a size larger than the real one lists wide files that are no sharper, only bigger to download.
The URL must be https:// (or http:// from a host listed with it), with no user or password in it. Bad input fails with image_remote_invalid. The format is read from the URL’s extension (JPEG when it names none). The descriptor has no placeholder, and its id is r and a hash of the URL and version. Whether the host is listed is checked where the image is fetched, not here.
remoteImage() never reads the file, so it can’t know a GIF or WebP moves. A moving image from a CMS gets no still for visitors who ask for less motion, and it’s laid out in your formats like a photo, not as animated WebP. Prefer a still image from the CMS, or name a moving one at build with import.meta.og.image(url), which reads the file.
How the files are made
- On a live server and in dev, the image endpoint fetches the URL the first time a file is asked for, under
hosts,maxBytes,timeoutand the redirect rule, and makes the file. The URL rides in the file’s signed URL (…/.remote/<the URL, base64url>.avif), so nobody can make the endpoint fetch another one. All the files of one URL asked at once share one fetch. At most 16 fetches run at once; more wait in a line like the endpoint’s encodes (Limits). - On a prerendered page, the build fetches it the same way and into the same cache as a build-time remote image, and makes its files as static files named by the bytes it got, not by the URL. A URL whose bytes change gets new file names in the next build. A build with no
remoteset fails withimage_unsettled, naming the host to list. - With
cloudflare()links, a file is/cdn-cgi/image/<options>/https://images.cms.example.com/…, made by Cloudflare’s CDN from the URL. The zone must allow that origin (Cloudflare). Withcloudflare({ links: false }), the Worker’s endpoint fetches the URL itself and makes the file with the Images binding. - With
vercel(), a file is/_vercel/image?url=https%3A%2F%2Fimages.cms.example.com%2F…, and Vercel fetches only from the hosts itsimages.remotePatternslists (Vercel). - Rendered only in the browser, a
remoteImage()shows its original URL: the browser can’t check the host against the list, and a link would have a CDN fetch it.
Files that expire
The bytes behind a URL can change, so a file the image endpoint makes from one is never kept forever. (A prerendered page is different: the build fetches the URL once and writes the file beside the page.) It’s kept, and sent to be kept, until the end of the current maxAge window: cache-control: public, max-age=<seconds left>, stale-while-revalidate=<maxAge>, never immutable. After that, the next ask fetches the URL again.
Each URL’s windows start at a point of their own, so a server’s remote files don’t all expire at once. A CMS whose URLs change with each revision can pass that revision as version: another version is another source, with files of its own.
When the host fails
A refusal is never kept by a browser or a CDN (no-store), and the server says it once per URL while it keeps failing, with image_remote_refused. A URL that failed isn’t fetched again for 30 seconds; its files get the same answer meanwhile.
| Status | When |
|---|---|
404 | The host isn’t listed (also one listed when the page rendered and taken out since), a redirect leaves the list, or a *. host resolves to a private address. |
413 | The image is past maxBytes. |
504 | The fetch took longer than timeout. |
502 | The host answered an error status or no answer, or sent something that isn’t a PNG, JPEG, WebP, AVIF, GIF or TIFF image or doesn’t decode as one (a file cut short included). |
When a 502 or 504 comes just after a window ended, a server that still keeps the file of the window before answers that file instead, sent to be kept 30 seconds. A host that’s down for a while doesn’t take its images away at once; one window later, with no newer file, the refusal is answered.
A failure of the server’s own while it makes a file, such as running out of memory, isn’t the URL’s: that one request answers 500 with image_settle_failed, and the next one tries again.
import { error } from '@sveltejs/kit';
import { remoteImage } from '@ogygia/image';
import { cms } from '$lib/server/cms';
export async function load({ params }) {
const entry = await cms.entry(params.slug);
if (!entry) error(404, 'Not found');
return {
title: entry.title,
cover: remoteImage(entry.cover.url, {
width: entry.cover.width,
height: entry.cover.height,
alt: entry.cover.alt,
version: entry.cover.updatedAt
})
};
}<script lang="ts">
import { Img } from '@ogygia/image';
let { data } = $props();
</script>
<Img
of={data.cover}
preset="cover"
alt={data.cover.alt ?? ''}
/>