Image optimization
Render stable responsive images and opt into tightly controlled server optimization for local or remote assets.
Describe the image layout
The Image component emits an img element with responsive optimizer URLs during SSR. Supply accurate dimensions to reserve space, a meaningful alt description, widths that match the design, and sizes that explain the rendered width to the browser.
Create src/components/ProjectHero.tsx
tsximport { Image } from "@tavojs/core";
import { Page } from "@tavojs/ui";
export function ProjectHero() {
return (
<Page>
<Image
src="/images/project-dashboard.png"
alt="Project dashboard showing active work"
width={1280}
height={720}
widths={[640, 960, 1280]}
sizes="(min-width: 60rem) 60rem, 100vw"
priority
/>
</Page>
);
}
Serve local assets from public
Files under public have root-relative browser URLs. Keep the source path inside that directory so the optimizer cannot read arbitrary server files. Use unoptimized when the source is already transformed or the deployment has no image optimizer.
Allow remote sources narrowly
Remote optimization is disabled until the application enables it and supplies an HTTPS allowlist. Restrict hostnames and paths to the product's actual media origins.
Merge into tavo.config.ts
tsimport { defineConfig } from "@tavojs/core/config";
export default defineConfig({
ssr: {
images: {
allowRemote: true,
remotePatterns: [
{
protocol: "https:",
hostname: "media.example.com",
pathname: "/projects/",
},
],
},
},
});
Install the production transformer when needed
The SSR optimizer uses the optional sharp dependency for transformations. Install it in the application that runs the server, then verify the /_tavo/image endpoint in production preview and on the selected platform.
Run in Terminal
bashnpm install sharp
npm run build
npm run preview:ssr