Loading...
Loading...
Compare original and translation side by side
swaswa-cli.config.jsonswa initstaticwebapp.config.jsonswaswa-cli.config.jsonswa initstaticwebapp.config.jsonnpm install -D @azure/static-web-apps-clinpx swa --versionnpm install -D @azure/static-web-apps-clinpx swa --versionswa initswa-cli.config.jsonswa initswa-cli.config.jsonswa starthttp://localhost:4280swa loginswa deployswa initswa-cli.config.jsonswa initswa-cli.config.jsonswa starthttp://localhost:4280swa loginswa deployswa initswa initswa init --yes{
"$schema": "https://aka.ms/azure/static-web-apps-cli/schema",
"configurations": {
"app": {
"appLocation": ".",
"apiLocation": "api",
"outputLocation": "dist",
"appBuildCommand": "npm run build",
"run": "npm run dev",
"appDevserverUrl": "http://localhost:3000"
}
}
}{
"navigationFallback": {
"rewrite": "/index.html",
"exclude": ["/images/*", "/css/*"]
},
"routes": [
{ "route": "/api/*", "allowedRoles": ["authenticated"] }
],
"platform": {
"apiRuntime": "node:20"
}
}swa initswa initswa init --yes{
"$schema": "https://aka.ms/azure/static-web-apps-cli/schema",
"configurations": {
"app": {
"appLocation": ".",
"apiLocation": "api",
"outputLocation": "dist",
"appBuildCommand": "npm run build",
"run": "npm run dev",
"appDevserverUrl": "http://localhost:3000"
}
}
}{
"navigationFallback": {
"rewrite": "/index.html",
"exclude": ["/images/*", "/css/*"]
},
"routes": [
{ "route": "/api/*", "allowedRoles": ["authenticated"] }
],
"platform": {
"apiRuntime": "node:20"
}
}swa login # Interactive login
swa login --subscription-id <id> # Specific subscription
swa login --clear-credentials # Clear cached credentials--subscription-id, -S--resource-group, -R--tenant-id, -T--client-id, -C--client-secret, -CS--app-name, -nswa login # 交互式登录
swa login --subscription-id <id> # 指定订阅
swa login --clear-credentials # 清除缓存的凭据--subscription-id, -S--resource-group, -R--tenant-id, -T--client-id, -C--client-secret, -CS--app-name, -nswa init # Interactive setup
swa init --yes # Accept defaultsswa init # 交互式设置
swa init --yes # 接受默认设置swa build # Build using config
swa build --auto # Auto-detect and build
swa build myApp # Build specific configuration--app-location, -a--api-location, -i--output-location, -O--app-build-command, -A--api-build-command, -Iswa build # 使用配置文件构建
swa build --auto # 自动检测并构建
swa build myApp # 构建特定配置--app-location, -a--api-location, -i--output-location, -O--app-build-command, -A--api-build-command, -Iswa start # Serve from outputLocation
swa start ./dist # Serve specific folder
swa start http://localhost:3000 # Proxy to dev server
swa start ./dist --api-location ./api # With API folder
swa start http://localhost:3000 --run "npm start" # Auto-start dev server| Framework | Port |
|---|---|
| React/Vue/Next.js | 3000 |
| Angular | 4200 |
| Vite | 5173 |
--port, -p--api-location, -i--api-port, -j--run, -r--open, -o--ssl, -sswa start # 从outputLocation提供服务
swa start ./dist # 为特定文件夹提供服务
swa start http://localhost:3000 # 代理到开发服务器
swa start ./dist --api-location ./api # 搭配API文件夹
swa start http://localhost:3000 --run "npm start" # 自动启动开发服务器| 框架 | 端口 |
|---|---|
| React/Vue/Next.js | 3000 |
| Angular | 4200 |
| Vite | 5173 |
--port, -p--api-location, -i--api-port, -j--run, -r--open, -o--ssl, -sswa deploy # Deploy using config
swa deploy ./dist # Deploy specific folder
swa deploy --env production # Deploy to production
swa deploy --deployment-token <TOKEN> # Use deployment token
swa deploy --dry-run # Preview without deployingswa deploy --print-tokenSWA_CLI_DEPLOYMENT_TOKEN--envpreviewproduction--deployment-token, -d--app-name, -nswa deploy # 使用配置文件部署
swa deploy ./dist # 部署特定文件夹
swa deploy --env production # 部署到生产环境
swa deploy --deployment-token <TOKEN> # 使用部署令牌
swa deploy --dry-run # 预览部署(不实际部署)swa deploy --print-tokenSWA_CLI_DEPLOYMENT_TOKEN--envpreviewproduction--deployment-token, -d--app-name, -nswa db init --database-type mssql
swa db init --database-type postgresql
swa db init --database-type cosmosdb_nosqlswa db init --database-type mssql
swa db init --database-type postgresql
swa db init --database-type cosmosdb_nosqlswa initswa startswa deployswa-cli.config.jsonundefinedswa startswa deployswa initswa-cli.config.jsonundefinedundefinedundefinedmkdir api && cd api
func init --worker-runtime node --model V4
func new --name message --template "HTTP trigger"api/src/functions/message.jsconst { app } = require('@azure/functions');
app.http('message', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: async (request) => {
const name = request.query.get('name') || 'World';
return { jsonBody: { message: `Hello, ${name}!` } };
}
});staticwebapp.config.json{
"platform": { "apiRuntime": "node:20" }
}swa-cli.config.json{
"configurations": {
"app": { "apiLocation": "api" }
}
}npx swa start ./dist --api-location ./apimkdir api && cd api
func init --worker-runtime node --model V4
func new --name message --template "HTTP trigger"api/src/functions/message.jsconst { app } = require('@azure/functions');
app.http('message', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: async (request) => {
const name = request.query.get('name') || 'World';
return { jsonBody: { message: `Hello, ${name}!` } };
}
});staticwebapp.config.json{
"platform": { "apiRuntime": "node:20" }
}swa-cli.config.json{
"configurations": {
"app": { "apiLocation": "api" }
}
}npx swa start ./dist --api-location ./api
**Supported API runtimes:** `node:18`, `node:20`, `node:22`, `dotnet:8.0`, `dotnet-isolated:8.0`, `python:3.10`, `python:3.11`
**支持的API运行时:** `node:18`, `node:20`, `node:22`, `dotnet:8.0`, `dotnet-isolated:8.0`, `python:3.10`, `python:3.11`.github/workflows/azure-static-web-apps.ymlname: Azure Static Web Apps CI/CD
on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened, closed]
branches: [main]
jobs:
build_and_deploy:
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build And Deploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: upload
app_location: /
api_location: api
output_location: dist
close_pr:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
action: closeAZURE_STATIC_WEB_APPS_API_TOKENapp_locationapi_locationoutput_locationskip_app_build: trueapp_build_command.github/workflows/azure-static-web-apps.ymlname: Azure Static Web Apps CI/CD
on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened, closed]
branches: [main]
jobs:
build_and_deploy:
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build And Deploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: upload
app_location: /
api_location: api
output_location: dist
close_pr:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
action: closeAZURE_STATIC_WEB_APPS_API_TOKENapp_locationapi_locationoutput_locationskip_app_build: trueapp_build_command| Issue | Solution |
|---|---|
| 404 on client routes | Add |
| API returns 404 | Verify |
| Build output not found | Verify |
| Auth not working locally | Use |
| CORS errors | APIs under |
| Deployment token expired | Regenerate in Azure Portal → Static Web App → Manage deployment token |
| Config not applied | Ensure |
| Local API timeout | Default is 45 seconds; optimize function or check for blocking calls |
swa start --verbose log # Verbose output
swa deploy --dry-run # Preview deployment
swa --print-config # Show resolved configuration| 问题 | 解决方案 |
|---|---|
| 客户端路由返回404 | 在 |
| API返回404 | 验证 |
| 找不到构建输出 | 验证 |
| 本地身份验证不工作 | 使用 |
| CORS错误 | |
| 部署令牌过期 | 在Azure门户 → 静态Web应用 → 管理部署令牌中重新生成 |
| 配置未生效 | 确保 |
| 本地API超时 | 默认超时为45秒;优化函数或检查是否存在阻塞调用 |
swa start --verbose log # 详细输出
swa deploy --dry-run # 预览部署
swa --print-config # 显示解析后的配置