@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
| Astro | ogygia |
|---|---|
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 schema | v.custom<ImageDescriptor>(isImage), with a ./ or ../ path (Check it in the schema) |
image() in a JSON collection’s schema | a 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') |
 in .md / .mdx | the same, with content({ markdown }) and image() listed |
| images encoded at build, content-hashed names | a 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.remotePatterns | image({ remote: { hosts } }) (below) |
Differences
- Frontmatter paths need
./or../. Astro’simage()reads any path; here a barecover.pngstays a string and fails theisImagecheck. Astro posts usually write./cover.pngalready. - Images live under
src/. A file outside it fails the build withimage_outside_src, and every raster image undersrc/is published (Keep images undersrc/). - 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
formatsdon’t apply to them, so a GIF cover’sog:imageis an animated WebP too. <Img>takes a fixed set of attributes:class,id,style,title,crossorigin,referrerpolicyanddraggable. Adata-*oraria-*attribute isn’t passed through; wrap the image instead.- MDX components aren’t part of
@ogygia/content’s markdown. Rename.mdxposts to.md, or to.svx, which takes Svelte components. Delete each post’s image wiring (import BlogImage from …andexport const components = { img: BlogImage }):![]()images render through<Img>on their own. Any other MDX component (a<Tweet id="…">) needs a Svelte component in a.svxpost, or a plain link. - Collections live in
.server.tsfiles, 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']ishosts: ['images.example.com'].- A
remotePatternsentry{ 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
pathnamepattern 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()
]
});