防止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 可以提高性能,避免不必要的计算资源消耗。
相关推荐
雨中飘荡的记忆13 小时前
Multi-Agent + Skills + Spring AI 构建自主决策智能体
后端·spring
whinc13 小时前
🚀 两年小程序开发,我把踩过的坑做成了开源 Skills
前端·微信小程序·ai编程
我叫黑大帅14 小时前
Go 语言并发编程的 “工具箱”
后端·面试·go
sure28214 小时前
React Native中创建自定义渐变色
前端·react native
用户83562907805114 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
用户9083246027315 小时前
Spring Boot 缓存架构:一行配置切换 Caffeine 与 Redis,透明支持多租户隔离
后端
KKKK15 小时前
SSE(Server-Sent Events)流式传输原理和XStream实践
前端·javascript
tyung15 小时前
zhenyi-base 开源 | Go 高性能基础库:TCP 77万 QPS,无锁队列 16ns/op
后端·go
子兮曰15 小时前
Humanizer-zh 实战:把 AI 初稿改成“能发布”的技术文章
前端·javascript·后端
桦说编程15 小时前
你的函数什么颜色?—— 深入理解异步编程的本质问题(上)
后端·性能优化·编程语言