javascript, yall.js, web
img に loading 属性が追加され yall.js を利用する必要はなくなりました
<img>, <picture>, <video>, <iframe> を遅延読み込みする yall.js
lighthouse を参考に webpage を高速化していく – Think Abstract を行っている最中、Google の技術記事を読んでいたら、malchata/yall.js というのを知った。
これを利用すると web page の最初の読み込みでダウンロードされる画像を少なくできるため、非常に快適に page を見ることができる。
また、あわせて画面のサイズにあわせてイメージのサイズがかわるように下記の Hugo の shortcodes を作成した。
参考:Responsive Images in Hugo | Adam Wills
/layouts/shortcodes/img.html
1<div class="picture">
2 {{ if .Get "caption"}}
3 <figure>
4 {{ end }}
5 <picture>
6 <img class="lazy post-img" data-src="{{ .Get "src" }}.{{ .Get "type" }}" alt="{{ .Get "alt" }}" style="{{ .Get "style" }}">
7 </picture>
8 {{ if .Get "caption"}}
9 <figcaption>{{ .Get "caption" }}</figcaption>
10 </figure>
11 {{ end }}
12</div>
img の class に lazy と、data-src が指定されているのがポイント。
post-img の css には、max-width: 100% を指定することで、画面のサイズに追従させた。
あとは、
1{{< img src="/assets/2019/01/lighthouse_google_mobile" type="png" >}}
と markdown 内で呼び出せば、遅延読み込みし、かつ画面のサイズにあったイメージが作成される。
Hugo はまだまだ使いこなせるし、最適化しがいがあるなぁ。yall.js を使うと下記のような形になった。

ちなみに、How To Escape Shortcode In Hugo Template | Lua Software Code より、
1{{</* ... */>}}
で shortcode はエスケープできるようだ。奥が深い…