注意:这篇文章上次更新于686天前,文章内容可能已经过时。
This article was last updated686 days ago, the content may be outdated.
hexo-img-onerror 是一个为 Hexo 网站添加备用图片源的插件。
故名思义,它的实现原理是为每一个 img 标签添加一个 onerror 事件,当图片加载失败时,会自动替换为备用图片。
如图所示:

hexo-img-onerror is a plugin that adds a fallback image source to Hexo websites.
As its name suggests, it works by attaching an onerror event to every img tag: when an image fails to load, it is automatically replaced with the fallback image.
As shown below:

安装与配置
运行以下命令安装插件:
1 | npm install hexo-img-onerror --save |
安装插件后,请在 Hexo 项目中配置它。
- 在 Hexo
_config.yml中添加以下配置,请确保src_prefix和onerror_src_prefix一一对应:
1 | img_onerror: |
如果配置了 onerror-map,插件会优先从 JSON 文件中查找 /files/ 之后的路径对应的备用链接,并将该链接写入 onerror。如果没有配置 onerror-map,或者在映射表中未找到对应项,则会回退到现有的 src_prefix / onerror_src_prefix 替换逻辑。
在你的文章或页面中,添加图像:
1 |  |
渲染出来的 img 标签会被设置 onerror 事件,重新指定 src:
1 | <img src="https://example.com/path1/image.jpg" onerror="this.src='https://fallback.com/path1/image.jpg';"> |
此时,如果 image.jpg 从 https://example.com/path1/image.jpg 加载失败,浏览器将自动从 https://fallback.com/path1/image.jpg 加载备用图像。
源码解读
1 | function addOnError(data, config) { |
为什么需要它?
我博客的图片放在了家里服务器的 Alist 里,最终的存储是国内的天翼云盘。 从这个服务器获取图片速度很快,但它的缺点是不稳定,有时会因为各种原因导致图片无法访问。
- DDNS 失败了
- 服务器 断网了
- Alist 挂了
- …
此外,这些博客里的图片在 Sharepoint 上也有备份,Sharepoint 的国内访问速度就比较一般了,但胜在稳定。
于是,一个自然的需求就是,为我的网站的所有图片添加一个备用源,让浏览器优先访问 Alist,如果失败了,再访问 Sharepoint。
经过与 AI 的一番交涉,我最终选择了为图片添加 onerror 事件的方式。
还有一种方式是给图片添加 srcset 属性,但是经过我的测试,浏览器并不会优先加载 src 属性,有时候会直接加载 srcset 属性指定的图片,这样就无法实现我想要的效果。
Installation & Configuration
Install the plugin by running:
1 | npm install hexo-img-onerror --save |
After installing, configure it in your Hexo project.
- Add the following configuration to your Hexo
_config.yml. Make suresrc_prefixandonerror_src_prefixcorrespond one-to-one:
1 | img_onerror: |
If onerror-map is configured, the plugin will first look up the path after /files/ in the JSON file, find the corresponding fallback link, and write it to onerror. If onerror-map is not configured, or no match is found in the mapping table, it falls back to the existing src_prefix / onerror_src_prefix replacement logic.
In your posts or pages, add images:
1 |  |
The rendered img tags will have an onerror event that reassigns src:
1 | <img src="https://example.com/path1/image.jpg" onerror="this.src='https://fallback.com/path1/image.jpg';"> |
Now, if image.jpg fails to load from https://example.com/path1/image.jpg, the browser will automatically load the fallback image from https://fallback.com/path1/image.jpg.
Source Code Explained
1 | function addOnError(data, config) { |
Why do I need it?
The images on my blog are stored in an Alist instance running on my home server, with the actual files on China Telecom’s Tianyi cloud drive. Fetching images from that server is fast, but it has a downside: it’s not very stable, and images can become inaccessible for various reasons.
- DDNS failed
- Server lost network connectivity
- Alist crashed
- …
Additionally, these images also have backups on Sharepoint, which is slower to access from mainland China but far more stable.
So a natural requirement arose: give every image on my site a fallback source, letting the browser try Alist first and fall back to Sharepoint on failure.
After some discussion with AI, I ended up going with attaching onerror events to images.
Another approach is adding a srcset attribute to images, but from my testing, browsers don’t necessarily prefer the src attribute — sometimes they load the image specified by srcset directly, which defeats my purpose.


