防止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 可以提高性能,避免不必要的计算资源消耗。
相关推荐
pe7er17 小时前
软件设计不要“既要又要”
前端·后端·架构
柏舟飞流17 小时前
Spring Boot 深入实践指南:从入门到工程化落地
spring boot·后端·firefox
kyriewen17 小时前
从Webpack到Vite:我们迁移了一个10万行代码的项目,总结了这7个坑
前端·webpack·vite
IT_陈寒18 小时前
Java Stream并行流的坑:我花了3小时才找到的线程安全问题
前端·人工智能·后端
小新11018 小时前
最简单但完整的 Vue 响应式示例(一个简单的计数器按钮)
前端·javascript·vue.js
橘子海全栈攻城狮18 小时前
【最新源码】鸟博士微信小程序 023
spring boot·后端·web安全·微信小程序·小程序
Hiter_John18 小时前
Golang的运算符
开发语言·后端·golang
皮皮林55118 小时前
Dubbo 的 SPI 和 JDK 的 SPI 有什么区别?
后端
金銀銅鐵19 小时前
用 Tkinter 实现一个罗马数字转整数的简单工具
后端·python
鹿青19 小时前
给设计稿做体检:我搓了个 Skill,专治 Figma 转代码出垃圾
前端·claude·视觉设计