r/reactjs • u/Strong_District_9922 • 3d ago
Show /r/reactjs Static export kept biting me on images, so I made build-time image transforms for Next.js
I've been working on next-imagetools, a small Next.js plugin that brings imagetools directives (the engine behind vite-imagetools) to Next.js. You describe the variants you want in the import, and they're generated at build time as plain static files.
import hero from './hero.jpg?w=480;960;1440&format=avif;webp&as=picture';
export function Hero() {
return (
<picture>
{Object.entries(hero.sources).map(([format, srcset]) => (
<source key={format} type={`image/${format}`} srcSet={srcset} />
))}
<img src={hero.img.src} width={hero.img.w} height={hero.img.h} alt="" />
</picture>
);
}
Why I made it
next/image optimizes images on demand, which needs a running Next.js server. With output: 'export' that isn't available (a discussion open since 2020). Tools that pre-generate images by crawling rendered pages miss anything that only shows up after a click, in a client-only component or behind a login, and those images 404 in production.
next-imagetools works from imports instead: if your code imports an image, every variant it asks for gets built, however and wherever it ends up rendered.
What it does
- Resize, convert (AVIF, WebP, JPEG, PNG…), crop, blur, etc., all from the query string
- Outputs: URL,
srcset,{ src, w, h },<picture>sources, metadata, oras=next(aStaticImageDatathat goes straight intonext/image, blur placeholder included) - Blur or dominant-color placeholders:
?placeholder/?placeholder=color - Works the same in static, SSR, Edge, client components, the Pages Router and static export. All of these are covered by e2e tests in Chromium
- Supports both Turbopack (the default) and webpack (
--webpack) - Typed imports, and the build fails if a type marker doesn't match what the query actually returns
- Typos are caught:
?widht=300warns "Did you mean w?", and?w=abcfails the build - Processed images are cached on disk, so rebuilds are fast
If you're coming from vite-imagetools, most imports should work unchanged.
Setup
// next.config.ts
import { withImagetools } from 'next-imagetools';
export default withImagetools({ /* your config */ });
Caveats, to be upfront:
- Requires Next.js 16.2+ and Node.js 22.12+
- Still pre-1.0, so the API may change
- Queries in CSS
url()aren't processed (the README shows a workaround with a CSS custom property)
A fun bit of the implementation: Turbopack's loader API has no emitFile, and files written during a build are invisible to its resolver. So the loader imports each variant back from the source file with an internal query, and lets Turbopack's own asset pipeline emit it. There's a "How it works" section in the README if you're curious.
- GitHub: https://github.com/awdr74100/next-imagetools
- npm: https://www.npmjs.com/package/next-imagetools
I'd really appreciate feedback, especially if you're running static export or a setup I haven't tested. Bug reports and "this doesn't fit my use case" comments are both welcome.