防止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 可以提高性能,避免不必要的计算资源消耗。
相关推荐
未来龙皇小蓝3 分钟前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions11 分钟前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发12 分钟前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
独断万古他化15 分钟前
【Spring 原理】Bean 的作用域与生命周期
java·后端·spring
程序员猫哥_19 分钟前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞0520 分钟前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、25 分钟前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao25 分钟前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
杨超越luckly31 分钟前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强
一 乐1 小时前
校园二手交易|基于springboot + vue校园二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端