Skip to content

@ogygia/image

Migrating from Astro

How each part of Astro's astro:assets maps to @ogygia/image, from imports and <Picture> to collection schemas and remote patterns, and the few things that work differently.

On this page

If you’re moving a site from Astro, most of astro:assets has a direct twin here. Images become descriptors from a macro instead of imports, <Image> and <Picture> become one <Img>, and widths become presets. Markdown images keep working as they are.

<script lang="ts">
  import { Img } from '@ogygia/image';

  // Astro: import hero from './hero.jpg';
  const hero = import.meta.og.image('./hero.jpg');
</script>

<!-- Astro: <Picture src={hero} formats={['avif', 'webp']} alt="…" /> -->
<Img of={hero} preset="cover" alt="The harbor at dusk" />

Mapping

Astroogygia
import hero from './hero.jpg'const hero = import.meta.og.image('./hero.jpg')
<Image src={hero} alt="…" /><Img of={hero} alt="…" />
<Picture formats={['avif', 'webp']} /><Img>: AVIF and WebP are the default
widths={[600, 1200]}, sizes="…"a preset, or layout and width; sizes only when a layout can’t say it
layout="full-width" / "constrained" / "fixed"layout="fullWidth" / "constrained" / "fixed"
loading="eager" + fetchpriority="high"priority
getImage({ src })getImage(descriptor, { … })
image() in a markdown collection’s schemav.custom<ImageDescriptor>(isImage), with a ./ or ../ path (Check it in the schema)
image() in a JSON collection’s schemaa v.string() field, looked up in an import.meta.og.image.glob() (Images a JSON collection names)
import.meta.glob('./*.jpg', { eager: true })import.meta.og.image.glob('./*.jpg')
![alt](./photo.jpg) in .md / .mdxthe same, with content({ markdown }) and image() listed
images encoded at build, content-hashed namesa prerendered site’s files made at build, named by their recipe
GIFs styled narrower (figure.gif { width: 33vw })a preset for them, named by image({ markdown: { animated: 'motion' } })
image.domains, image.remotePatternsimage({ remote: { hosts } }) (below)

Differences

  • Frontmatter paths need ./ or ../. Astro’s image() reads any path; here a bare cover.png stays a string and fails the isImage check. Astro posts usually write ./cover.png already.
  • Images live under src/. A file outside it fails the build with image_outside_src, and every raster image under src/ is published (Keep images under src/).
  • A static site’s image URLs stay the same across builds, like Astro’s _astro/ files, with no secret to set. They sit under _app/immutable/og/ and are named by the source’s content and the recipe, not the file’s name.
  • Animated GIFs become animated WebP, never AVIF. There’s no option to turn that off, and a preset’s formats don’t apply to them, so a GIF cover’s og:image is an animated WebP too.
  • <Img> takes a fixed set of attributes: class, id, style, title, crossorigin, referrerpolicy and draggable. A data-* or aria-* attribute isn’t passed through; wrap the image instead.
  • MDX components aren’t part of @ogygia/content’s markdown. Rename .mdx posts to .md, or to .svx, which takes Svelte components. Delete each post’s image wiring (import BlogImage from … and export const components = { img: BlogImage }): ![]() images render through <Img> on their own. Any other MDX component (a <Tweet id="…">) needs a Svelte component in a .svx post, or a plain link.
  • Collections live in .server.ts files, and pages load them in +page.server.ts, not a universal +page.ts, which can’t import a server module. An awaited body and a descriptor cross to the page.

Remote images

Astro’s image.domains and image.remotePatterns become one list, image({ remote: { hosts } }) (List the hosts you take images from):

  • domains: ['images.example.com'] is hosts: ['images.example.com'].
  • A remotePatterns entry { protocol: 'https', hostname: '**.example.com' } is '*.example.com': one level or more, like Astro’s **..
  • Astro’s *. (exactly one level) has no twin here: list each name, or use *..
  • { protocol: 'http', hostname: 'localhost', port: '4100' } is 'http://localhost:4100'.
  • A pathname pattern has no twin: a host is listed whole.

Astro fetches a remote image at build and doesn’t know its size until then. Here the build fetches it once into its cache and asks again on each build, so the page always reserves its box. An image a CMS hands you at request time is remoteImage().

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',
          '*.example.com',
          'http://localhost:4100'
        ]
      }
    }),
    sveltekit()
  ]
});