Plaiceholder Documentation

website·Indexed 33 days ago

https://plaiceholder.co/docs

Documentation for Plaiceholder, including getting started guides, usage parameters, and plugins for Next.js and Tailwind. Version 3.0 is the final release and is ESM only.

Tokens
3K
Snippets
17
Records
25
Agent score
69%

What's inside Plaiceholder

  1. Generate low quality image placeholders (LQIP) with plaiceholder

    Plaiceholder is a suite of server-side functions used to create low quality image placeholders (LQIP). It provides four different strategies depending on the desired output format and performance characteristics: Color, CSS, SVG, and Base64.
  2. Understand Plaiceholder environment requirements

    Plaiceholder is a server-side library and cannot be used in the browser. It is distributed as ESM only. It supports all runtimes compatible with the sharp library, which is currently limited primarily to Node.js.
  3. Install Plaiceholder and its dependencies

    Plaiceholder requires the sharp library to transform images. While some frameworks include sharp by default, you may need to install it manually before installing the main package.
    npm install sharp
    npm install plaiceholder
  4. Install and configure @plaiceholder/tailwindcss

    The @plaiceholder/tailwindcss package is ESM only and requires a Node.js environment. To use it, install the package and add it to your Tailwind configuration. Note that your Tailwind config file must use the .mjs or .ts extension.
    npm install @plaiceholder/tailwindcss
    // tailwind.config.mjs
    // @ts-check
    import plaiceholder from "@plaiceholder/tailwindcss";
       
    /** @type {import('tailwindcss').Config} */
    export default {
      content: [],
      theme: {
        extend: {},
      },
      variants: {},
      plugins: [plaiceholder()],
    };
  5. Migrate plaiceholder to version 3.0

    Upgrading to plaiceholder 3.0 requires several breaking changes to ensure future-proofing and wider runtime support. Key requirements include:

    • ESM Only: All plaiceholder packages are now ESM only. CommonJS projects must migrate to ESM.
    • Dependency Update: Ensure all plaiceholder and @plaiceholder/* packages are updated to at least version 3.0.0.
    • Runtime Support: Continues to work on all runtimes supported by sharp (currently limited to Node.js).
  6. Implement a resolver for @plaiceholder/tailwindcss

    The plaiceholder plugin requires a resolver function to determine how to convert an image path (provided in the Tailwind class square brackets) into a Buffer. This is configured within the plugin options in your Tailwind config.
    // tailwind.config.mjs
    // @ts-check
    import fs from "node:fs";
    import path from "node:path";
    import plaiceholder from "@plaiceholder/tailwindcss";
       
    /** @type {import('tailwindcss').Config} */
    export default {
      content: [],
      theme: {
        extend: {},
      },
      variants: {},
      plugins: [
        plaiceholder({
          resolver: (src) =>
            fs.readFileSync(path.join("./public", `${src}.jpg`)),
        }),
      ],
    };
  7. Understand plaiceholder module system requirements

    All plaiceholder packages are ESM (ECMAScript Module) only. They cannot be used in CommonJS environments without a build step or specific compatibility layers.
  8. Install and configure @plaiceholder/next for Next.js

    The @plaiceholder/next plugin is required for Next.js projects to ensure that all Plaiceholder functions start in the main thread. Note that plaiceholder packages are ESM only. To set up the plugin, install the package and wrap your Next.js configuration with withPlaiceholder. Your configuration file must use the .mjs extension (or .ts where supported).
    npm install @plaiceholder/next
    // next.config.mjs
    // @ts-check
    import withPlaiceholder from "@plaiceholder/next";
    
    /**
     * @type {import('next').NextConfig}
     */
    const config = {
      // your Next.js config
    };
    
    export default withPlaiceholder(config);
  9. Implement custom image resolution for getPlaiceholder in 3.0

    In version 3.0, getPlaiceholder() no longer automatically resolves images via file paths or URLs. You must now pass a Buffer directly to the function. This change allows the library to support non-Node.js runtimes.
    import path from "node:path";
    import fs from "node:fs/promises";
    import { getPlaiceholder } from "plaiceholder";
    
    // For local images in /public (Node.js)
    const getImageLocal = 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 } };
    };
    
    // For remote images (Node.js)
    const getImageRemote = 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 } };
    };
  10. Generate image placeholders using getPlaiceholder

    plaiceholder packages are ESM only. The primary function getPlaiceholder(input, options) takes a raw Buffer image source and an optional configuration object to generate low-resolution placeholders and metadata.
    import { getPlaiceholder } from "plaiceholder";
    
    // input must be a raw Buffer
    const result = await getPlaiceholder(input, options);
  11. Create a custom Plaiceholder implementation

    Developers interested in creating their own implementation of Plaiceholder should refer to the official contributing guidelines on GitHub for technical requirements and standards.
    Guidelines: https://github.com/joe-bell/plaiceholder/blob/main/CONTRIBUTING.md#implementations
  12. Update @plaiceholder/next and @plaiceholder/tailwindcss for 3.0

    Due to the shift to ESM, configuration files for associated plugins must be migrated to ESM formats:

    • Next.js: Migrate next.config.js to next.config.mjs (or .ts if supported).
    • Tailwind CSS: Migrate tailwind.config.js to tailwind.config.mjs or tailwind.config.ts.