[ PROMPT_NODE_24998 ]
netlify-toml
[ SKILL_DOCUMENTATION ]
# netlify.toml 配置参考
用于 Netlify 构建和部署的配置文件。
## 基本结构
toml
[build]
command = "npm run build"
publish = "dist"
## 构建设置
### 通用配置
toml
[build]
# 构建站点的命令
command = "npm run build"
# 发布目录(相对于仓库根目录)
publish = "dist"
# 函数目录
functions = "netlify/functions"
# 基础目录(如果不是仓库根目录)
base = "packages/frontend"
# 在特定条件下忽略构建
ignore = "git diff --quiet HEAD^ HEAD package.json"
## 环境变量
toml
[build.environment]
NODE_VERSION = "18"
NPM_FLAGS = "--prefix=/dev/null"
[context.production.environment]
NODE_ENV = "production"
## 框架检测
Netlify 会自动检测框架,但你可以覆盖它:
### Next.js
toml
[build]
command = "npm run build"
publish = ".next"
### React (Vite)
toml
[build]
command = "npm run build"
publish = "dist"
### Vue
toml
[build]
command = "npm run build"
publish = "dist"
### Astro
toml
[build]
command = "npm run build"
publish = "dist"
### SvelteKit
toml
[build]
command = "npm run build"
publish = "build"
## 重定向与重写
toml
[[redirects]]
from = "/old-path"
to = "/new-path"
status = 301
[[redirects]]
from = "/api/*"
to = "https://api.example.com/:splat"
status = 200
# SPA 后备(用于客户端路由)
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
## 请求头
toml
[[headers]]
for = "/*"
[headers.values]
X-Frame-Options = "DENY"
X-XSS-Protection = "1; mode=block"
Content-Security-Policy = "default-src 'self'"
[[headers]]
for = "/assets/*"
[headers.values]
Cache-Control = "public, max-age=31536000, immutable"
## 上下文特定配置
针对不同上下文部署不同设置:
toml
# 生产环境
[context.production]
command = "npm run build:prod"
[context.production.environment]
NODE_ENV = "production"
# 部署预览
[context.deploy-preview]
command = "npm run build:preview"
# 分支部署
[context.branch-deploy]
command = "npm run build:staging"
# 特定分支
[context.staging]
command = "npm run build:staging"
## 函数配置
toml
[functions]
directory = "netlify/functions"
node_bundler = "esbuild"
[[functions]]
path = "/api/*"
function = "api"
## 构建插件