Skip to content

@ogygia/image

Images

@ogygia/image turns your image files into responsive images: AVIF and WebP at the widths a layout needs, real crops, a blur placeholder and no layout shift, rendered as plain server HTML. The same package makes markdown images, images from a CMS and share cards.

On this page

A good image on the web is a lot of work. You need the file in two or three formats, at several widths, cut to the shape the page shows. Then a srcset and a sizes that match the layout, a width and height so nothing jumps, lazy loading for everything but the hero, and something to look at while the bytes arrive. Get one of these wrong and the page is slow, blurry or jumpy.

With @ogygia/image you say how an image sits on the page, once in a preset or right on the component, and the package works out the rest: the files, the srcset, the sizes, the width and height, the placeholder and the loading hints. <Img> renders it all as server HTML, a <picture> with its <source>s, and ships no JavaScript.

What you get:

  • Files made when a page asks. The build reads each image once, for its size and a tiny placeholder, and makes no image file. Each file a page needs is a URL. On a live server or in dev, the image endpoint makes it the first time someone asks. For a prerendered page, the build makes it and writes it as a static file.
  • Presets instead of numbers. You name how your site shows images (thumb, cover, avatar) and pick one per image. One source can be a square thumbnail on one page and a wide hero on another.
  • Real crops. A 1/1 thumbnail downloads a square file, not a wide one squeezed into a box.
  • No layout shift. Every image carries its width and height, so the page keeps its space.
  • A blur placeholder of a few hundred bytes paints until the image arrives.
  • No client JavaScript. <Img> writes no script and no inline event handler.
  • Markdown, CMS images and share cards. ![alt](./photo.jpg) in a post, an image whose URL comes from a CMS at request time, and a Svelte component drawn as a PNG for og:image all go through the same path.
<script lang="ts">
  import { Img } from '@ogygia/image';

  const hero = import.meta.og.image('../lib/images/hero.jpg');
</script>

<Img of={hero} preset="cover" alt="The harbor at dusk" />

Install

Install the package and sharp, the encoder. Then list the image() plugin beside ogygia(), before sveltekit(), with the presets your site uses. A preset is a name for how an image is shown; Img component covers what goes in one.

The plugin reads Kit’s paths from sveltekit(). Without it in the list, the build stops with image_no_kit.

Add the types and the server handle

import.meta.og.image is a macro: the build rewrites each call, so there’s nothing to import. Its types come from @ogygia/image/types; add that line beside ogygia’s own.

The image endpoint runs inside ogygia’s server handle, so hooks.server.ts must export it. Every ogygia app already does (Add the server handle). Without it, the first <Img> that renders on the server stops with image_no_server.

Where sharp must be listed

sharp is a peer of the package: your app installs it, so you pick its version and its native binary matches your platform. The build and the dev server measure every image with it, so it must be installed and listed in your app’s own package.json. Otherwise the build or the dev server stops with image_sharp_missing.

Where it’s listed depends on who makes files:

  • A server that makes files on request (adapter-node, adapter-vercel with sharp()) needs sharp in dependencies. A server adapter may bundle every package that isn’t there, and a bundled sharp can’t find its native binary, so every image would answer 500. The build checks this and fails with image_sharp_missing when sharp is only in devDependencies.
  • A fully prerendered site (adapter-static), and an app whose live files come from Cloudflare or Vercel alone, uses sharp only at build. devDependencies is enough.

On a live server, only the image endpoint loads sharp, on the first image request. Deployment covers each host.

pnpm add @ogygia/image sharp
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({
      presets: {
        cover: {
          layout: 'fullWidth',
          aspect: '16/9',
          priority: true
        },
        thumb: { width: 400, aspect: '1/1' },
        avatar: { layout: 'fixed', width: 48, height: 48 }
      }
    }) ,
    sveltekit()
  ]
});
/// <reference types="ogygia/types" />
/// <reference types="@ogygia/image/types" />
import * as ogygia from 'ogygia/server';

export const handle = ogygia.handle();

How it works

The build reads each image once, at compile time. It records the size, makes a 16-pixel-wide blur placeholder, and hashes the bytes into the source’s id. That’s the descriptor: plain data that a page passes to <Img>. No image file is made yet.

When a page renders, <Img> works out which files the layout needs (a width, a format, a crop each) and names each one as a URL. Who makes the file depends on where the page renders:

  • In dev, the browser asks and the dev server makes each file, once. Repeats are a file read.
  • On a live server, each URL is signed with the app’s key, so nobody can ask for a size no page asked for. The image endpoint makes the file on the first request and keeps it.
  • On a prerendered page, the build makes every file the page names and writes it as a static file under _app/immutable/og/. Any host serves it, with no server at all.

Every file name starts with the source’s id. A changed image gets a new id, so new URLs, and every file can be cached forever. Image files has the details: signing, the file store, the build’s files, cache headers and limits.

<!-- a page rendered on a live server -->
<img
  src="/__ogygia__/i/Hq3…/Xk2jQ81mZaPq0Lw7-1280x720/_app/immutable/assets/hero.Bx1.jpg.webp"
  …
/>

<!-- a prerendered page -->
<img
  src="/_app/immutable/og/Xk2jQ81mZaPq0Lw7-1280x720.webp"
  …
/>

Package contents

PageWhat it covers
Img componentimport.meta.og.image(), .glob(), <Img> and its props, presets, layouts, priority, morph, getImage(), islands.
Art directionOther files for the same image, picked by a media query: a dark file, a crop for phones, a still frame of a GIF.
Markdown images![alt](./photo.jpg) in a post, frontmatter covers, images a JSON collection names.
Remote imagesImages on a CDN fetched at build, and remoteImage() for a URL a CMS gives you at request time.
Social cardsimport.meta.og.image.card(): a Svelte component drawn as a PNG for og:image.
Absolute URLsog:image and RSS URLs: absolute, at your site’s origin, in a format every reader shows.
Image filesThe image endpoint, signed URLs, the file store, the build’s static files, warm, cache headers and limits.
DeploymentNode, static hosts, Cloudflare Workers with cloudflare(), Vercel with vercel(), and what each encoder can make.
OptionsEvery plugin option, preset key and source option, formats and quality, pixel operations, and every message code.
Migrating from Astroastro:assets mapped to ogygia, and what differs.
@ogygia/imageThe generated API reference for <Img>, getImage(), isImage(), remoteImage() and the types.
@ogygia/image/viteThe generated API reference for image() and the encoders.

Integrations

  • @ogygia/content. With both plugins listed, a local image in markdown renders through <Img>, and a frontmatter path to an image becomes a descriptor. See Markdown images.
  • SPA router. morph on two images of the same source names them for a view transition, so a thumbnail glides into the post’s hero when you navigate.
  • @ogygia/devtools. The profiler’s page insights flag a largest paint that’s still lazy (the lcp-lazy insight), which priority fixes.