payload-webp
payloadcms/payload plugin for automatic image conversion to webp format.
Getting started
- Install the package with
npm i payload-webp
OR yarn add payload-webp
- Import the plugin to your
payload.config.ts
:
import webp from "payload-webp";
export default buildConfig({
...
plugins: [
webp(webpPluginOptions)
]
)};
- After uploading images to your upload-enabled collection new field called webp is added with converted image => webp file meteadata and its sizes.
Access webp field with graphql like so:
query {
allMedia {
docs {
url
filesize
webp {
url
filesize
sizes {
thumbnail {
width
height
url
}
}
}
}
}
}
By default webp images are being processed to reduce their filesize as well.
Plugin options
Optionally you can pass JSON with following plugin options to tweak compression or limit conversion to particular mimeTypes or specific upload-enabled collections.
interface WebpPluginOptions {
mimeTypes?: string[];
sharpWebpOptions?: sharp.WebpOptions;
collections?: CollectionConfig['slug'][];
sync?: boolean;
}
Regenerate Webp images
You can regenerate existing images with following GraphQL mutation:
mutation {
WebpRegenerate(slug: String!, sort: String) {
currentFile
current
total
}
}
Arguments
First Header | Second Header |
---|
slug | Upload collection slug in camelCase |
sort | You can pass the sort parameter to set the direction in which images will be regenerated. Default: createdAt |
Fields and subsequent calls
You can use returned fields to show notify of current progress to user.
Any subsequent call while regeneration of particular collection is in the progress will not start new regeneration process, but will return current progress.