|
| 1 | +--- |
| 2 | +title: Images |
| 3 | +lead: Use <code>@pracht/image</code> for responsive image markup, reserved layout space, and deployment-specific optimization loaders. |
| 4 | +breadcrumb: Images |
| 5 | +prev: |
| 6 | + href: /docs/styling |
| 7 | + title: Styling |
| 8 | +next: |
| 9 | + href: /docs/cli |
| 10 | + title: CLI |
| 11 | +--- |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +```sh |
| 16 | +pnpm add @pracht/image |
| 17 | + |
| 18 | +# Only needed when you use the built-in Node optimization endpoint. |
| 19 | +pnpm add sharp |
| 20 | +``` |
| 21 | + |
| 22 | +`@pracht/image` is split into a framework-agnostic component entry and a Node endpoint entry. Import the component from `@pracht/image`; import the optimization handler from `@pracht/image/node`. |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Render an Image |
| 27 | + |
| 28 | +```tsx [src/routes/gallery.tsx] |
| 29 | +import { Image } from "@pracht/image"; |
| 30 | + |
| 31 | +export function Component() { |
| 32 | + return ( |
| 33 | + <Image |
| 34 | + src="/banner.jpg" |
| 35 | + alt="Pracht banner" |
| 36 | + width={1200} |
| 37 | + height={280} |
| 38 | + sizes="(max-width: 1200px) 100vw, 1200px" |
| 39 | + priority |
| 40 | + /> |
| 41 | + ); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +The component renders plain `<img>` markup, so it works during SSR and SSG without adding client runtime. `loading="lazy"` and `decoding="async"` are the defaults. Use `priority` for above-the-fold images; it switches the image to eager loading and adds `fetchpriority="high"`. |
| 46 | + |
| 47 | +Always provide meaningful `alt` text, or `alt=""` for decorative images. |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Reserve Layout Space |
| 52 | + |
| 53 | +Images need either intrinsic dimensions or `fill`: |
| 54 | + |
| 55 | +```tsx |
| 56 | +<Image src="/card.jpg" alt="Product preview" width={640} height={360} /> |
| 57 | +``` |
| 58 | + |
| 59 | +For background-style images, use `fill` inside a positioned parent: |
| 60 | + |
| 61 | +```tsx |
| 62 | +<div style={{ position: "relative", height: "18rem" }}> |
| 63 | + <Image |
| 64 | + src="/hero.jpg" |
| 65 | + alt="Pracht docs hero" |
| 66 | + fill |
| 67 | + sizes="100vw" |
| 68 | + style={{ objectFit: "cover" }} |
| 69 | + /> |
| 70 | +</div> |
| 71 | +``` |
| 72 | + |
| 73 | +`fill` images stretch with `position: absolute; inset: 0`. The parent controls the rendered size, so give the parent a stable height or aspect ratio. |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## Mount the Default Endpoint |
| 78 | + |
| 79 | +The default loader points at `/api/_pracht/image`. Add an API route at that path to resize and encode same-origin source images with `sharp`: |
| 80 | + |
| 81 | +```ts [src/api/_pracht/image.ts] |
| 82 | +import { createImageHandler } from "@pracht/image/node"; |
| 83 | + |
| 84 | +export const GET = createImageHandler(); |
| 85 | +``` |
| 86 | + |
| 87 | +This endpoint works in `pracht dev`, adapter-node, and Node-compatible runtimes. It returns immutable cache headers, varies on `Accept`, and negotiates modern output formats such as WebP. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Configure Loaders |
| 92 | + |
| 93 | +Loaders turn `{ src, width, quality }` into a URL. Configure one globally when your deployment platform should serve image variants: |
| 94 | + |
| 95 | +```ts [src/routes.ts] |
| 96 | +import { cloudflareLoader, configureImage } from "@pracht/image"; |
| 97 | + |
| 98 | +configureImage({ |
| 99 | + loader: cloudflareLoader, |
| 100 | + quality: 75, |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +| Loader | Best For | |
| 105 | +| ------ | -------- | |
| 106 | +| `defaultLoader` | The `/api/_pracht/image` endpoint | |
| 107 | +| `cloudflareLoader` | Cloudflare Image Resizing | |
| 108 | +| `vercelLoader` | Vercel Image Optimization | |
| 109 | +| `passthroughLoader` | Static hosts without an image service | |
| 110 | + |
| 111 | +You can also pass a `loader` prop to a single `<Image>` when one image needs different handling. |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Remote Images |
| 116 | + |
| 117 | +The Node endpoint accepts same-origin URLs by default. Allow remote hosts explicitly: |
| 118 | + |
| 119 | +```ts [src/api/_pracht/image.ts] |
| 120 | +import { createImageHandler } from "@pracht/image/node"; |
| 121 | + |
| 122 | +export const GET = createImageHandler({ |
| 123 | + remotePatterns: [ |
| 124 | + { protocol: "https", hostname: "images.example.com", pathname: "/uploads" }, |
| 125 | + ], |
| 126 | +}); |
| 127 | +``` |
| 128 | + |
| 129 | +Remote allowlists are rechecked after redirects. Widths are also restricted to configured breakpoints, which keeps attackers from filling your cache with arbitrary image variants. |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +## Platform Notes |
| 134 | + |
| 135 | +| Target | Recommendation | |
| 136 | +| ------ | -------------- | |
| 137 | +| Node | Mount `createImageHandler()` and use the default loader | |
| 138 | +| Cloudflare Workers | Use `cloudflareLoader`; `sharp` does not run in Workers | |
| 139 | +| Vercel | Use `vercelLoader` and keep Vercel image sizes aligned with your Pracht breakpoints | |
| 140 | +| Static hosting | Use `passthroughLoader` so images render without an optimization backend | |
| 141 | + |
| 142 | +See the `examples/basic` gallery route for a complete endpoint plus component example. |
0 commit comments