防止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 分钟前
别再写 Prompt 了Spec Mode 才是下一代 AI 编程范式
前端·人工智能·ai编程
如意猴4 分钟前
【前端】002--怎样制作一个简历界面?
开发语言·前端·javascript
NickJiangDev10 分钟前
Elpis Schema 动态组件与表单:配置驱动的完整 CRUD 闭环
前端
ffqws_10 分钟前
Spring Boot入门:通过简单的注册功能串联Controller,Service,Mapper。(含有数据库建立,连接,及一些关键注解的讲解)
数据库·spring boot·后端
kerli13 分钟前
Compose 组件:Box 核心参数及其 Bias 算法
android·前端
luckyCover14 分钟前
TypeScript学习系列(二):高级类型篇
前端·typescript
NickJiangDev14 分钟前
Elpis NPM 发布:把框架从业务中剥离出来
前端
im_AMBER16 分钟前
手撕发布订阅与观察者模式:从原理到实践
前端·javascript·面试
程序边界17 分钟前
行标识符机制的技术演进与实践(下)——ROWID与实战应用
后端
九英里路18 分钟前
cpp容器——string模拟实现
java·前端·数据结构·c++·算法·容器·字符串