惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
K
Kaspersky official blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Cisco Talos Blog
Cisco Talos Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
PCI Perspectives
PCI Perspectives
酷 壳 – CoolShell
酷 壳 – CoolShell
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
N
News and Events Feed by Topic
V
Vulnerabilities – Threatpost
T
Troy Hunt's Blog
GbyAI
GbyAI
C
CERT Recently Published Vulnerability Notes
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
量子位
Scott Helme
Scott Helme
月光博客
月光博客
Attack and Defense Labs
Attack and Defense Labs
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
Project Zero
Project Zero
G
GRAHAM CLULEY
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
小众软件
小众软件
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
S
Security @ Cisco Blogs
The Last Watchdog
The Last Watchdog
MongoDB | Blog
MongoDB | Blog
H
Hacker News: Front Page
Latest news
Latest news
P
Proofpoint News Feed

Dvel's Blog

Rime 全拼双拼混输 CHD 油猴脚本:每日签到自动答题 Rime 配置:雾凇拼音 Hammerspoon 自动切换输入法 macOS 利用 Karabiner 修改 Emacs 键位为 Vim 键位 macOS 修改 Emacs 键位为 Vim 键位 Surge 配置 汉字的混乱 Netflix 中英双语字幕的好办法 优化 Rime 英文输入体验 判断字符是否为简体字或繁体字 Cloudflare 转入域名提示「出现了问题。请重试转移此域。尚未向您收费。」 图片压缩:Squoosh、TinyPNG、ImageOptim、WebP macOS grep + sed 批量替换多个文件的内容 在 macOS 根目录创建文件夹 博客图床小妙招 折腾 Hugo & PaperMod 主题 利用 Cloudflare Workers 进行批量 301 重定向 将博客部署在 Cloudflare Pages Alfred File Navigation(文件导航器)详解 在 Hugo Goldmark Markdown 中设置以新标签打开链接 日期与时间(UTC、GMT、时间戳、时区) 让 Alfred 以新标签的方式打开 Finder 用 git filter-branch 给 Git 仓库瘦身 qBittorrent 设置教程 联通光猫破解+桥接记录 配置 Mac 终端走代理 Steam for Mac 中文游戏推荐 我的 Mac 软件/工具列表 《Flask Web 开发》笔记 用 sleepwatcher 让 Mac 在睡眠及唤醒时自动执行一些脚本 解决 pyenv 导致的 brew warning 《SQL 必知必会》笔记 Git 自总结 Python 异步 IO 笔记 再见,双点医院! 用 CloudXNS 解决 CNAME 与 MX 记录冲突 你好,Hugo! 为 Mac 打造 zsh command line 环境 新 Mac 新装修 秦皇岛暑期自驾的烦恼 苹果公司开发者账号申请流程 UIScrollView 中使用 Masonry 解决 CocoaPods 的类库 import 没有提示 iOS 使用 CocoaPods 的快速回顾 GitBook 简单生成本地静态 HTML 的方法 你好, Hexo! About
修复 Hugo 本地图片的累计布局偏移(CLS)问题
Dvel · 2023-01-08 · via Dvel's Blog

布局偏移

累计布局偏移 Cumulative Layout Shift(CLS)是一项 Web 指标。

布局偏移如图所示,元素突然变化的高度影响了用户交互体验:

CLS problem

一般给图片一个高度就可以了,但最好是能自动化,而不是手写。

看到了这篇博文 累计布局偏移修复方案改进 —— 自动生成图片宽高,不过他是从腾讯云对象存储获取的图片宽高,而我都将图片存储在了 Hugo 配置目录的 static/img/ 文件夹。

我平时在 Markdown 里都是直接写的 path:

解决本地图片的偏移问题

参考 How to prevent CLS in Hugo,利用 Hugo 的 Render Hooks 功能重新渲染图片相关代码,获取图片的宽高,在 <img> 标签里加上 widthheight 属性。

不知道为什么照他的办法,在配置中加入了 mounts 相关属性,仍然不起作用,不太会前端和 Hugo,摸索着改了改。

创建 layouts/_default/_markup/render-image.html 文件:

<p>
{{ if hasPrefix .Destination "/img" }}
    {{ $img := os.ReadFile (path.Join "/static" .Destination) | resources.FromString .Destination }}
    {{ if ne $img.MediaType.SubType "svg" }}
        <img loading="lazy"
            src="{{ .Destination | safeURL }}"
            alt="{{ .Text }}"
            width="{{ $img.Width }}"
            height="{{ $img.Height }}"
            style="max-width: 100%; height: auto;"
        />
    {{ end }}
    {{ if or (not $img) (eq $img.MediaType.SubType "svg") }}
        <img loading="lazy" src="{{ .Destination | safeURL }}" alt="{{ .Text }}" />
    {{ end }}
{{ else }}
    <img loading="lazy" src="{{ .Destination | safeURL }}" alt="{{ .Text }}" />
{{ end }}
</p>

在他的基础上手动加上了 /static 地址前缀,不然这个 os.ReadFile 方法一直报错;

加上了 style="max-width: 100%; height: auto;",保持宽度超过父容器的图片的比例;

套了个 if else,只处理图片地址为 /img 开头的本地图片,并可以显示网络图片,但是网络图片仍然会产生偏移。

我用的是 Hugo PaperMod 主题,主页的封面图也存在这个问题,得修改 cover.html,看不太懂,我看上面那位博主是修改的这里,确实起作用。

修改最外层的 {{- else }} 里的内容(Hugo PaperMod 6.0 版本),和之前的一样,就是替换一下 srcalt 的属性值:

{{- else }}{{/* For absolute urls and external links, no img processing here */}}
    {{- if $addLink }}<a href="{{ (.Params.cover.image) | absURL }}" target="_blank"
        rel="noopener noreferrer">{{ end -}}
        <!-- <img loading="lazy" src="{{ (.Params.cover.image) | absURL }}" alt="{{ $alt }}"> -->
        <!-- 修改,将上面那行改成下面这些: -->
        {{ if hasPrefix .Params.cover.image "/img" }}
            {{ $img := os.ReadFile (path.Join "/static" .Params.cover.image) | resources.FromString .Params.cover.image }}
            {{ if ne $img.MediaType.SubType "svg" }}
                <img loading="lazy"
                    src="{{ .Params.cover.image | safeURL }}"
                    alt="{{ $alt }}"
                    width="{{ $img.Width }}"
                    height="{{ $img.Height }}"
                    style="max-width: 100%; height: auto;"
                />
            {{ end }}
            {{ if or (not $img) (eq $img.MediaType.SubType "svg") }}
                <img loading="lazy" src="{{ .Params.cover.image | safeURL }}" alt="{{ $alt }}" />
            {{ end }}
        {{ else }}
            <img loading="lazy" src="{{ .Params.cover.image | safeURL }}" alt="{{ $alt }}" />
        {{ end }}
{{- end }}