@ogygia/image
Absolute URLs
An og:image tag or an RSS feed needs an absolute URL in a format every reader shows. Make the URL that <Img> or getImage() gives you absolute against the page's URL, set Kit's prerender origin, and ask for JPEG or PNG.
On this page
<Img> and getImage() give root-relative URLs (/_app/immutable/og/…), which is right for a page. A crawler that reads your og:image, or a feed reader that shows a post’s images, needs the whole address, and most of them only show PNG and JPEG. Three things make that work: new URL(src, page.url), a real kit.prerender.origin, and a share-friendly format.
<script lang="ts">
import { page } from '$app/state';
import { getImage } from '@ogygia/image';
let { data } = $props();
const share = $derived(
new URL(
getImage(data.cover, { width: 1200, formats: ['jpeg'] })
.src,
page.url
).href
);
</script>
<svelte:head>
<meta property="og:image" content={share} />
</svelte:head>Usage
Make the URL absolute against the page’s URL with new URL(src, page.url). That works on a live page and on a prerendered one alike, with one setting.
While a page prerenders, page.url names Kit’s prerender origin, which defaults to http://sveltekit-prerender. Set kit.prerender.origin to your site’s origin. With Kit’s default, the build can’t know where the file will be, and fails with image_mark_left. A URL changed any other way (escaped, or encoded into a share link’s query string) fails the same way: encode it in the browser, not in the page. Image URLs on a prerendered page explains why.
import adapter from '@sveltejs/adapter-static';
export default {
kit: {
adapter: adapter(),
prerender: { origin: 'https://example.com' }
}
};Share formats
Link previews show PNG and JPEG; most show no AVIF or WebP. A share card’s src is already a PNG. For anything else, name the format: getImage(of, { formats: ['jpeg'] }), or a preset with formats. Use ['jpeg'] for a photo and ['png'] for an image with transparency: JPEG has no alpha, so transparent areas take whatever color the hidden pixels hold, usually black. An animated image is always animated WebP, whatever formats says, so pick a still image for a share tag.
In dev, a page whose og:image or twitter:image names an AVIF or WebP file says image_share_format.
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: {
share: {
width: 1200,
height: 630,
formats: ['jpeg']
}
}
}),
sveltekit()
]
});RSS feeds
A feed is a prerendered +server.ts. Awaiting a post’s body renders it, and its html is the post as HTML. Make its image URLs absolute against the request’s URL, and give each item its cover as an enclosure. On a live server the same code names the signed URLs at the request’s origin.
The enclosure’s type follows the file’s format: formats: ['jpeg'] names a JPEG, but an animated cover is always animated WebP, so read the type from the file’s extension. Its length is 0, which RSS readers take as “not known”: the file may not be made yet when the feed renders. Link the feed from a layout (<link rel="alternate" type="application/rss+xml" href="/feed.xml">) so Kit’s crawler prerenders it. A site can emit a feed for you.
import { getImage } from '@ogygia/image';
import { blog } from '$lib/blog.server';
export const prerender = true;
const URL_ATTR_RE = /\b(src|srcset)="([^"]*)"/g;
function absolute(value: string, at: URL): string {
return value
.split(', ')
.map((candidate) => {
const [url = '', ...rest] = candidate.split(' ');
return [
new URL(url.replaceAll('&', '&'), at).href,
...rest
].join(' ');
})
.join(', ');
}
const escape = (text: string) =>
text.replaceAll('&', '&').replaceAll('<', '<');
const TYPES: Record<string, string> = {
jpg: 'image/jpeg',
webp: 'image/webp',
png: 'image/png'
};
export async function GET({ url }) {
const items: string[] = [];
for (const ref of await blog.refs()) {
const post = (await blog.get(ref.id))!;
const body = await post.body;
const html = (
body?.kind === 'inline' ? (body.html ?? '') : ''
).replace(
URL_ATTR_RE,
(_, attr: string, value: string) =>
`${attr}="${absolute(value, url)}"`
);
const cover = new URL(
getImage(post.data.cover, {
width: 1200,
formats: ['jpeg']
}).src,
url
).href;
const title = `<title>${escape(post.data.title)}</title>`;
const link = `<link>${new URL(`/blog/${ref.id}`, url).href}</link>`;
const type =
TYPES[cover.split('.').pop() ?? ''] ??
'application/octet-stream';
const enclosure = `<enclosure url="${cover}" type="${type}" length="0"/>`;
items.push(
`<item>${title}${link}${enclosure}<description>${escape(html)}</description></item>`
);
}
return new Response(
`<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>A blog</title>${items.join('')}</channel></rss>`,
{ headers: { 'content-type': 'application/xml' } }
);
}