写前哔哔
最近也是上了腾讯云EdgeOne的免费末班车,但是对于我这种懒人来说,每次都要手动刷新缓存简直是太难了,尤其是腾讯云老是要求要扫码更要老命了。后来无意中看到了 青桔气球的文章,实现了不用登陆腾讯云就能刷新缓存,但这个方法还是要手动node EdgeOne.js
才行,那对我这种懒人必须得优化优化,于是豆包大法开始(为什么用豆包,因为我毫无相关基础,只是代码的搬运工。。。)。
正片开始
- 首先在博客根目录下新建一个
scripts
文件夹,然后在该文件夹内新建一个edgeone-cache.js
文件。
- 安装必要依赖(我也不知道装哪个,反正我都装了):
1 2
| npm install tencentcloud-sdk-nodejs npm install tencentcloud-sdk-nodejs-teo
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| const tencentcloud = require("tencentcloud-sdk-nodejs-teo"); const TeoClient = tencentcloud.teo.v20220901.Client;
const edgeoneCfg = hexo.config.edgeone || {};
if (!edgeoneCfg.enable) { console.log("[EdgeOne] 缓存刷新已禁用(edgeone.enable=false)"); return; }
const client = new TeoClient({ credential: { secretId: edgeoneCfg.secretId, secretKey: edgeoneCfg.secretKey, }, region: edgeoneCfg.region || "ap-guangzhou", profile: { httpProfile: { endpoint: "teo.tencentcloudapi.com" } }, });
async function refreshCache() { try { const params = { ZoneId: edgeoneCfg.zoneId, Type: edgeoneCfg.type || "purge_host", Targets: edgeoneCfg.targets || [hexo.config.url], Method: edgeoneCfg.method || "invalidate", };
const res = await client.CreatePurgeTask(params); if (res && res.Response) { if (res.Response.TaskId) { console.log(`✅ EdgeOne 刷新成功 | 任务 ID:${res.Response.TaskId}`); console.log(`→ 类型:${params.Type} | 方式:${params.Method}`); console.log(`→ 目标:${JSON.stringify(params.Targets)}`); } else { console.error(`⚠️ API 响应无 TaskId:`, res.Response); } } else { console.error(`⚠️ API 响应异常:`, res); } } catch (err) { console.error(`❌ EdgeOne 刷新失败:${err.code} | ${err.message}`); hexo.log.error(err); } }
hexo.on("deployAfter", async () => { console.log("\n=== EdgeOne 缓存自动刷新启动 ==="); await refreshCache(); console.log("=== EdgeOne 缓存刷新结束 ===\n"); });
|
根据豆包描述,这个脚本利用了 Hexo 的deployAfter
钩子,确保在部署完成后才执行缓存刷新操作,保证了内容已经更新到服务器后再刷新缓存,避免了无效的缓存刷新。
- 然后在
_config.yaml
里边添加以下配置文件:
1 2 3 4 5 6 7 8 9 10
| edgeone: enable: true secretId: "***" secretKey: "***" zoneId: "***" type: "***" targets: - "***" - "***" method: "***"
|
关键参数解释:
secretId
和secretKey
在腾讯云访问密钥 - 控制台创建,一组密钥secretKey
只在创建的时候出现一次,注意保存。
zoneId
在腾讯云EO服务总览->站点概览。

type
与targets
对应关系,可同时配置多个targets:
需求 |
配置值 |
示例 Targets |
全站缓存 |
purge_all |
无需填写 Targets |
域名级缓存 |
purge_host |
"blog.qjqq.cn" |
目录级缓存(如 /blog) |
purge_prefix |
"https://blog.qjqq.cn/blog/" |
单页面缓存 |
purge_url |
"https://blog.qjqq.cn/posts/123.html" |
invalidate
(标记过期):将缓存标记为 “过期”,后续用户请求时,CDN 会回源校验资源是否真的过期(不会立即删除缓存,而是懒校验)。
delete
(直接删除):直接从 CDN 节点的缓存中物理删除资源,后续请求会直接回源获取最新内容。
- 执行
hexo d
,出现EdgeOne刷新结束后,打开个人博客验证是否刷新成功(出现API响应异常不影响刷新)。

最后叨叨
个人纯小白,全程豆包编码,仅做个人成功案例分享,大佬勿喷。
参考教程