防止Cloudflare扣费隐患:Cloudflare Pages 中使用functions一定要配置_routes.json

Cloudflare Pages Functions 项目中,_routes.json 用于 定义哪些请求应该由 Functions 处理,哪些应该直接返回静态文件

在官方文档中有付费提示:

vbnet 复制代码
On a purely static project, Pages offers unlimited free requests. However, once you add Functions on a Pages project, all requests by default will invoke your Function. To continue receiving unlimited free static requests, exclude your project's static routes by creating a `_routes.json` file.

_routes.json的使用方法

  1. functions/ 目录下创建 _routes.json 文件(如果没有)。

  2. 编写路由规则,格式如下:

    json 复制代码
    json
    复制编辑
    {
      "version": 1,
      "include": ["/api/*"],
      "exclude": ["/static/*"]
    }
    • "include":匹配这些路径的请求会由 Functions 处理。
    • "exclude":这些路径的请求会直接返回静态文件,而不会触发 Functions。

示例

示例 1:让 /api/ 目录下的所有请求交给 Functions 处理

json 复制代码
json
复制编辑
{
  "version": 1,
  "include": ["/api/*"]
}

📌 效果

  • /api/user → 由 Cloudflare Pages Functions 处理
  • /index.html → 返回静态文件(不触发 Functions)

示例 2:只让 /api/ 目录下的请求交给 Functions,静态资源除外

json 复制代码
json
复制编辑
{
  "version": 1,
  "include": ["/api/*"],
  "exclude": ["/api/static/*"]
}

📌 效果

  • /api/data → 由 Functions 处理
  • /api/static/image.png → 返回静态文件

示例 3:排除某些路径,不让 Functions 处理

json 复制代码
json
复制编辑
{
  "version": 1,
  "include": ["/*"],
  "exclude": ["/assets/*", "/docs/*"]
}

📌 效果

  • /home → 由 Functions 处理
  • /docs/tutorial.html → 返回静态文件(不触发 Functions)
  • /assets/logo.png → 返回静态文件

常见问题

  1. 必须包含 "include" 吗?

    是的,include 不能为空,否则 Functions 不会生效

  2. 如果没有 _routes.json 会怎样?
    默认所有请求都会执行 Functions,如果你只想处理 API 请求,最好手动配置。

  3. Cloudflare Pages Functions 适用于哪些场景?

    • 处理 API 请求(/api/
    • 服务器端渲染(SSR)
    • 保护某些资源(认证、权限控制)

includeexclude 共同决定了哪些路径会触发 Cloudflare Pages Functions,它们的关系如下:

1. 只写 include(不写 exclude

只有 include 中指定的路径会触发 Functions,其余路径默认不会触发。

示例

json 复制代码
{
  "version": 1,
  "include": ["/api/*"]
}

📌 效果:

  • /api/user → ✅ 触发 Functions
  • /api/orders/123 → ✅ 触发 Functions
  • /index.html → ❌ 直接返回静态文件
  • /assets/logo.png → ❌ 直接返回静态文件

2. 只写 exclude(不写 include

默认所有路径都会触发 Functions,exclude 指定的路径不会触发。

示例

json 复制代码
{
  "version": 1,
  "exclude": ["/static/*"]
}

📌 效果:

  • /api/data → ✅ 触发 Functions
  • /home → ✅ 触发 Functions
  • /static/image.png → ❌ 直接返回静态文件

3. include + exclude 同时使用

include 先生效,exclude 会从 include 里剔除路径。

示例

json 复制代码
{
  "version": 1,
  "include": ["/*"],
  "exclude": ["/static/*", "/docs/*"]
}

📌 效果:

  • /api/data → ✅ 触发 Functions
  • /home → ✅ 触发 Functions
  • /static/image.png → ❌ 直接返回静态文件
  • /docs/index.html → ❌ 直接返回静态文件

总结

规则写法 触发 Functions 不触发 Functions
只写 include 只有 include 里的路径 其他路径默认不触发
只写 exclude 除了 exclude 里的路径,其他都触发 exclude 里指定的路径
同时写 includeexclude include 里的路径,去掉 exclude 指定的 exclude 里指定的

🔹 建议

  • 只想让部分 API 请求触发 Functions?👉 使用 include (比如 /api/*)。
  • 想让大部分请求触发 Functions,只有少数路径例外?👉 使用 exclude (比如 /static/*)。
  • 想要更精准控制?👉 同时使用 includeexclude
  • _routes.json 控制哪些请求由 Cloudflare Pages Functions 处理,哪些返回静态文件。
  • include 定义需要触发 Functions 的路径,exclude 定义不触发的路径。
  • 合理配置 _routes.json 可以提高性能,避免不必要的计算资源消耗。
相关推荐
RadiumAg26 分钟前
记一道有趣的面试题
前端·javascript
yangzhi_emo30 分钟前
ES6笔记2
开发语言·前端·javascript
yanlele1 小时前
我用爬虫抓取了 25 年 5 月掘金热门面试文章
前端·javascript·面试
ai小鬼头1 小时前
Ollama+OpenWeb最新版0.42+0.3.35一键安装教程,轻松搞定AI模型部署
后端·架构·github
萧曵 丶2 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
中微子2 小时前
React状态管理最佳实践
前端
老任与码2 小时前
Spring AI Alibaba(1)——基本使用
java·人工智能·后端·springaialibaba
烛阴2 小时前
void 0 的奥秘:解锁 JavaScript 中 undefined 的正确打开方式
前端·javascript
中微子2 小时前
JavaScript 事件与 React 合成事件完全指南:从入门到精通
前端
Hexene...2 小时前
【前端Vue】如何实现echarts图表根据父元素宽度自适应大小
前端·vue.js·echarts