Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webp and avif conversion to strapi-plugin-upload #47

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions rfcs/xxxx-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
- Start Date: 2022-08-16
- RFC PR: (leave this empty)

# Summary

Add the option to convert all uploaded images to WebP or AVIF instead of just resizing them. This would be done via a **opt-in** parameter in `./config/plugins.js`.

# Motivation

WebP and AVIF are superior formats for the web and support is growing very fast.

It got to a point where some projects could decide to **only** work with these formats to improuve the user experience without adding engeneering complexity or hosting costs.

One could even argue that this could make your service more ecologically friendly, by reducing the data transfered on every page load.

### How much better?

"WebP typically achieves an average of 30% more compression than JPEG" -- [Google WebP FAQ](https://developers.google.com/speed/webp/faq#:~:text=WebP%20typically%20achieves%20an%20average,help%20make%20the%20web%20faster.)

- [Comparative study of WebP, JPEG and JPEG 2000, September 2010 - by Google](https://developers.google.com/speed/webp/docs/c_study)
- [WebP Compression Study - by Google](https://developers.google.com/speed/webp/docs/webp_studyhttps://developers.google.com/speed/webp/docs/webp_studyhttps://developers.google.com/speed/webp/docs/webp_study)
- [WebP vs AVIF - by avif.org](https://avif.io/blog/comparisons/avif-vs-webp/)

### How good is browser support?

As of Agust 2022 of the major browsers that don't support it are:
- WebP: Only IE does not support it
- AVIF: Edge and other smaller browsers don't have support, and Safari desktop has partial support

Sources:
- [https://caniuse.com/webp](https://caniuse.com/webp)
- [https://caniuse.com/avif](https://caniuse.com/avif)

# Detailed design

This could be done directly in [/packages/core/upload/server/services/image-manipulation.js](https://github.com/strapi/strapi/blob/master/packages/core/upload/server/services/image-manipulation.js) file.

A member of the community already had a prototype (no affiliation):
https://github.com/devgar/strapi/blob/243d914efeebfdb44b236af76689c30b73fed646/packages/strapi-plugin-upload/services/image-manipulation.js

Basically it would instruct `sharp` package to transform to `webp` or `avif`. This would **not** require an aditional dependency, not even a new import in the file.

It will of course still resizes images like before (e.g. `large`, `medium` ...).

These changes being applied only at the upload phase, would not require all the images to be reformatted making it pretty safe for retro-compatibility. And it can be turned on and off without much consequences.

# Example

Something like this:
```ts
// path: ./config/plugins.ts

export default ({ env }) => ({
upload: {
config: {
convertAllImagesTo: 'webp' | 'avif'; // To be defined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have some resource one which conversions are supported to webp? is it jpg/png to webp or are there more formats supported?

How does sharp handle non compatible formats ? is it just not doing the transform or throws ? if so we would need to have a mapping to maintain?

},
},
});
```

# Tradeoffs

- Even if I'm pretty confident that is a good decision for many of us, it is **opinionated**. This could be mitigated by adding more formats as targets like `png` or `jpeg`.
- I don't know how computer intensive it is to do the transformation.

# Alternatives

I guess this could be done in a plugin. But this would require replacing the official `strapi-plugin-update` for a community one. This plugin is quite big and important, I wouldn't recommend replacing it with a community-maintained plugin.

If is not in the official one, I wouldn't bother doing it.

# Unresolved questions

- Should it support many different formats (e.g. `png` and `jpeg`) or just work with `webp` and `avif` which are the ones that should be used? More formats would make it less opinionated but would add more maintenance, and may not be that relevant.
- How should the `./config/plugins.ts` look like?