写前哔哔

最近也是上了腾讯云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;

// 从 Hexo 配置读取 EdgeOne 参数
const edgeoneCfg = hexo.config.edgeone || {};

// 若未启用,直接退出
if (!edgeoneCfg.enable) {
console.log("[EdgeOne] 缓存刷新已禁用(edgeone.enable=false)");
return;
}

// 初始化腾讯云 TEO 客户端
const client = new TeoClient({
credential: {
secretId: edgeoneCfg.secretId,
secretKey: edgeoneCfg.secretKey,
},
region: edgeoneCfg.region || "ap-guangzhou",
profile: { httpProfile: { endpoint: "teo.tencentcloudapi.com" } },
});

/**
* 执行 EdgeOne 缓存刷新
*/
async function refreshCache() {
try {
const params = {
ZoneId: edgeoneCfg.zoneId, // 必传:EdgeOne 站点 ID
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 部署完成钩子
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 # 是否启用(必填,true/false)
secretId: "***" # 访问密钥 ID(必填)
secretKey: "***" # 访问密钥 Key(必填)
zoneId: "***" # EdgeOne 站点 ID(必填,格式如 zone-xxx)
type: "***" # 刷新类型:purge_host/purge_url/purge_prefix/purge_all
targets: # 刷新目标(数组,根据 type 填写)
- "***"
- "***"
method: "***" # 清除方式:invalidate/delete

关键参数解释:

secretIdsecretKey腾讯云访问密钥 - 控制台创建,一组密钥secretKey只在创建的时候出现一次,注意保存。

zoneId在腾讯云EO服务总览->站点概览。

typetargets对应关系,可同时配置多个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响应异常不影响刷新)。

image-20250814190742711

最后叨叨

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

参考教程