返回资讯中心
外部精选
软件工程
#开发者实时流

Hugo 网站现在可无缝生成 WebP 和 AVIF 图片

来源标题:Seamless generation of WebP and AVIF images in Hugo

Hugo 网站可直接生成 WebP 和 AVIF 图片,无需再依赖自定义脚本。作者此前用 bash/fish 脚本及 Markdown 渲染钩子转换图片,现改为 Hugo 内置的无缝生成方式。WebP 和 AVIF 已获主流浏览器广泛支持,可作为 JPG/PNG 的渐进增强替代。

Hacker News 最新speckx2026/11/17 · 今天8 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

Shipping a website that uses only JPG and PNG is literally disrespectful to the end user. Especially for those without access to multi-Mbps broadband. Using “backwards compatibility” as an excuse not to serve better image formats is stupid because the browser serves the best one it is capable of serving and falls back to the archaic formats as a last resort. Progressive enhancement at its best. But the chance is slim that you use a browser that fails to use formats like WebP or AVIF because they have fantastic browser support.

32 2014.01.14

Microsoft Edge

18 2018.10.02

Firefox

65 2019.01.29

Safari

14 2020.09.16

Baseline: AVIF is widely available

85 2020.08.25

Microsoft Edge

121 2024.01.25

Firefox

113 2023.05.09

Safari

16.4 2023.03.27

I have been serving these modern formats to my readers on this very website for years, maybe even a decade. I have been using a combination of custom bash/fish scripts to generate these images, and some conditional logic that hooks into the Markdown rendering pipeline. “WebP and AVIF images on a Hugo website” and “Quick script to convert JPEG/PNG images to WebP and AVIF” explain the way I have been doing that in the past. Hugo makes this process a tonne easier now, you literally don’t need to generate anything yourself, because it is all built in.

When you add an image to one of your Markdown files, by default it spits out an good old plain HTML tag with the resource you gave.

Some image

<img src="file.jpg" alt="Some image" />

This is the place for an image rander hook to override the generated HTML output. What we need is a few modern Hugo features to generate WebP and AVIF outputs, and the HTML <picture> element.

<picture> {{- $destination := .Destination -}} {{- $extension := path.Ext $destination -}} {{- $image := .Page.Resources.Get $destination -}} {{- $altText := .Text -}}

{{- $formats := slice -}} {{- if in (slice ".jpg" ".jpeg" ".png") (lower $extension) -}} {{- $formats = slice "avif" "webp" -}} {{- end -}}

{{- with $image -}} {{- range $format := $formats -}} <source srcset="{{- ($image.Process $format).Permalink -}}" type="image/{{- $format -}}"> {{- end -}} {{- end -}}

{{- with $image -}} <img src="{{- .Permalink -}}" alt="{{- $altText -}}" loading="lazy" decoding="async" width="{{- .Width -}}" height="{{- .Height -}}" /> {{- end -}} </picture>

Here is the example of the generated markup.

<picture

<source srcset="https://pawelgrzybek.com/file_hu_9bb41489c358b97.avif" type="image/avif" /> <source srcset="https://pawelgrzybek.com/file_hu_236e41ca6d79e727.webp" type="image/webp" /> <img src="https://pawelgrzybek.com/file.jpg" alt="Some image" loading="lazy" decoding="async" width="1800" height="1201" /></picture>

No more custom scripts, no more local binaries. Easy!

Did you like it? Please share it with your friends. Thanks!

Thanks Paweł. with this post you help me to give me a kick to implement AVIF across my websites (which I am currently in progress), that I postpone for some time as I need to rethink image render hook at the same time :)

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →