Upgrading to 3.0
3.0 marks the end of the plaiceholder project, with this major release focussing on making the project feature complete and as future-proof as possible.
plaiceholder continues to work on all runtimes supported by sharp (opens in a new tab).
sharp (opens in a new tab) is currently limited to Node.js, but there is a pull request (opens in a new tab) to expand support.
Preparing for wider support meant dropping some of the "magic" features that plaiceholder previously offered.
There is no obligation to upgrade to 3.0 if you would prefer to keep using the previous API!
If this project has been useful to you, please consider sponsoring my work (opens in a new tab).
Migrate to ESM
plaiceholder packages are ESM only (opens in a new tab).
If you're using any of the plaiceholder packages in a CommonJS project, you'll need to migrate to ESM before continuing.
Update Your Dependencies
Ensure all plaiceholder and @plaiceholder/* packages are set to at least 3.0.0.
Update Your Code
plaiceholder
-
Implement your own image resolver
Previously,
plaiceholderwould automatically resolve your image based on a specified file path or URL, e.g.getPlaiceholder('./path-to-image').To enable future adoption within other non-Node.js runtimes, this feature has been removed.
Instead, you will need to pass a
BuffertogetPlaiceholder(), based on your own use-case.In the case of Node.js, for local images in
/public:import path from "node:path"; import fs from "node:fs/promises"; const getImage = async (src: string) => { const buffer = await fs.readFile(path.join("./public", src)); const { metadata: { height, width }, ...plaiceholder } = await getPlaiceholder(buffer, { size: 10 }); return { ...plaiceholder, img: { src, height, width }, }; }; // Usage const { base64, img } = await getImage("/assets/image/example.jpg");In the case of Node.js, for remote images:
const getImage = async (src: string) => { const buffer = await fetch(src).then(async (res) => Buffer.from(await res.arrayBuffer()) ); const { metadata: { height, width }, ...plaiceholder } = await getPlaiceholder(buffer, { size: 10 }); return { ...plaiceholder, img: { src, height, width }, }; }; // Usage const { base64, img } = await getImage( "https://images.unsplash.com/photo-1621961458348-f013d219b50c?auto=format&fit=crop&w=2850&q=80" ); -
blurhashis no longer returnedSwitch any
blurhashconsumers to an alternative LQIP strategy, or use theblurhashpackage directly. -
img→metadataUpdate any
imgconsumers to make use of the newmetadataobject.Instead, use
metadatato create your own<img />attributes.Note:
img.typehas moved tometadata.format. -
No longer
removeAlphaby defaultTo preserve previous behaviour, you can specify
removeAlpha: truewithingetPlaiceholder()'s options. -
Update Your Types
TGetPlaiceholderSrc→GetPlaiceholderSrcIGetPlaiceholderOptions→GetPlaiceholderOptionsIGetPlaiceholderReturn→GetPlaiceholderReturnIGetPlaiceholder→typeof getPlaiceholder
@plaiceholder/next
- Migrate your
next.config.jstonext.config.mjs
(or.tswhen supported) - Ensure your config matches the configuration steps.
@plaiceholder/tailwindcss
- Migrate your
tailwind.config.jstotailwind.config.mjsortailwind.config.ts - Follow the configuration steps to add a
resolver.