使用 Workers Static Assets 提供静态前端。使用 durable_object 调度策略将 API 请求路由到三个 Containers 组成的池。
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "static-frontend-container-backend",
"main": "src/index.ts",
"upload_source_maps": true,
// Set this to today's date
"compatibility_date": "2026-11-20",
"observability": {
"enabled": true
},
"assets": {
"directory": "./dist",
"binding": "ASSETS",
"run_worker_first": [
"/api",
"/api/*"
]
},
"containers": [
{
"class_name": "Backend",
"scheduling_policy": "durable_object",
"images": {
"base": {
"dockerfile": "./Dockerfile"
}
}
}
],
"durable_objects": {
"bindings": [
{
"name": "BACKEND",
"class_name": "Backend"
}
]
},
"exports": {
"Backend": {
"type": "durable-object",
"storage": "sqlite"
}
}
}name = "static-frontend-container-backend"
main = "src/index.ts"
upload_source_maps = true
# Set this to today's date
compatibility_date = "2026-11-20"
[observability]
enabled = true
[assets]
directory = "./dist"
binding = "ASSETS"
run_worker_first = ["/api", "/api/*"]
[[containers]]
class_name = "Backend"
scheduling_policy = "durable_object"
[containers.images.base]
dockerfile = "./Dockerfile"
[[durable_objects.bindings]]
name = "BACKEND"
class_name = "Backend"
[exports.Backend]
type = "durable-object"
storage = "sqlite"在 ./dist 目录中创建一个简单的 index.html 文件。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Widgets</title>
<script
defer
src="https://cdnjs.cloudflare.com/ajax/libs/alpinejs/3.13.3/cdn.min.js"
></script>
</head>
<body>
<div x-data="widgets()" x-init="fetchWidgets()">
<h1>Widgets</h1>
<div x-show="loading">Loading...</div>
<div x-show="error" x-text="error" style="color: red;"></div>
<ul x-show="!loading && !error">
<template x-for="widget in widgets" :key="widget.id">
<li>
<span x-text="widget.name"></span> - (ID:
<span x-text="widget.id"></span>)
</li>
</template>
</ul>
<div x-show="!loading && !error && widgets.length === 0">
No widgets found.
</div>
</div>
<script>
function widgets() {
return {
widgets: [],
loading: false,
error: null,
async fetchWidgets() {
this.loading = true;
this.error = null;
try {
const response = await fetch("/api/widgets");
if (!response.ok) {
throw new Error(
`HTTP ${response.status}: ${response.statusText}`,
);
}
this.widgets = await response.json();
} catch (err) {
this.error = err.message;
} finally {
this.loading = false;
}
},
};
}
</script>
</body>
</html>前端使用 Alpine.js ↗ 从 /api/widgets 获取小组件列表。
这是一个非常简单的示例,但你可以实现更复杂的逻辑。有关更多信息,请参阅 Workers 与前端框架集成的示例。
start() 会发起启动。Worker 会等待 /health 成功响应后再转发流量。并发请求共用此就绪检查;如果检查失败,下一个请求可以重试。
import { DurableObject } from "cloudflare:workers";
const INSTANCE_COUNT = 3;
const INACTIVITY_TIMEOUT_MS = 2 * 60 * 60 * 1000;
export class Backend extends DurableObject {
starting;
constructor(ctx, env) {
super(ctx, env);
const container = ctx.container;
if (container.running) {
void ctx.blockConcurrencyWhile(() =>
container.setInactivityTimeout(INACTIVITY_TIMEOUT_MS),
);
}
}
async fetch(request) {
// Concurrent requests share startup and readiness checks.
this.starting ??= this.startAndWaitForPort().finally(() => {
this.starting = undefined;
});
await this.starting;
const url = new URL(request.url);
url.protocol = "http:";
url.host = "container";
const forwarded = new Request(url, request);
forwarded.headers.delete("host");
return this.ctx.container.getTcpPort(8080).fetch(forwarded);
}
async startAndWaitForPort() {
const container = this.ctx.container;
if (!container.running) {
container.start({
image: container.images.base,
instance: "lite",
enableInternet: false,
});
}
await container.setInactivityTimeout(INACTIVITY_TIMEOUT_MS);
const port = container.getTcpPort(8080);
let lastError;
for (let attempt = 0; attempt < 100; attempt++) {
try {
const response = await port.fetch("http://container/health", {
signal: AbortSignal.timeout(1000),
});
await response.body?.cancel();
if (!response.ok) {
throw new Error(`Health check returned ${response.status}`);
}
return;
} catch (error) {
lastError = error;
await scheduler.wait(200);
}
}
throw new Error("Container did not become ready on port 8080", {
cause: lastError,
});
}
}
export default {
fetch(request, env) {
const pathname = new URL(request.url).pathname;
if (pathname === "/api" || pathname.startsWith("/api/")) {
const index = Math.floor(Math.random() * INSTANCE_COUNT);
return env.BACKEND.getByName(`instance-${index}`).fetch(request);
}
return env.ASSETS.fetch(request);
},
};import { DurableObject } from "cloudflare:workers";
interface Env {
BACKEND: DurableObjectNamespace<Backend>;
ASSETS: Fetcher;
}
const INSTANCE_COUNT = 3;
const INACTIVITY_TIMEOUT_MS = 2 * 60 * 60 * 1000;
export class Backend extends DurableObject<Env> {
private starting: Promise<void> | undefined;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
const container = ctx.container!;
if (container.running) {
void ctx.blockConcurrencyWhile(() =>
container.setInactivityTimeout(INACTIVITY_TIMEOUT_MS),
);
}
}
async fetch(request: Request): Promise<Response> {
// Concurrent requests share startup and readiness checks.
this.starting ??= this.startAndWaitForPort().finally(() => {
this.starting = undefined;
});
await this.starting;
const url = new URL(request.url);
url.protocol = "http:";
url.host = "container";
const forwarded = new Request(url, request);
forwarded.headers.delete("host");
return this.ctx.container!.getTcpPort(8080).fetch(forwarded);
}
private async startAndWaitForPort(): Promise<void> {
const container = this.ctx.container!;
if (!container.running) {
container.start({
image: container.images.base,
instance: "lite",
enableInternet: false,
});
}
await container.setInactivityTimeout(INACTIVITY_TIMEOUT_MS);
const port = container.getTcpPort(8080);
let lastError: unknown;
for (let attempt = 0; attempt < 100; attempt++) {
try {
const response = await port.fetch("http://container/health", {
signal: AbortSignal.timeout(1000),
});
await response.body?.cancel();
if (!response.ok) {
throw new Error(`Health check returned ${response.status}`);
}
return;
} catch (error) {
lastError = error;
await scheduler.wait(200);
}
}
throw new Error("Container did not become ready on port 8080", {
cause: lastError,
});
}
}
export default {
fetch(request: Request, env: Env): Promise<Response> {
const pathname = new URL(request.url).pathname;
if (pathname === "/api" || pathname.startsWith("/api/")) {
const index = Math.floor(Math.random() * INSTANCE_COUNT);
return env.BACKEND.getByName(`instance-${index}`).fetch(request);
}
return env.ASSETS.fetch(request);
},
} satisfies ExportedHandler<Env>;Worker 会为每个 API 请求选择三个具名实例之一。每个实例使用已配置的 base 镜像和 lite 实例规格。
你的容器应能够处理对 /api/widgets 的请求。
在本例中,我们将使用一个简单的 Golang 后端,该后端返回硬编码的小组件列表。
server.go
package main
import (
"encoding/json"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
widgets := []map[string]interface{}{
{"id": 1, "name": "Widget A"},
{"id": 2, "name": "Sprocket B"},
{"id": 3, "name": "Gear C"},
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
json.NewEncoder(w).Encode(widgets)
}
func main() {
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/api/widgets", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}健康检查端点让 Worker 可以在转发请求前等待后端就绪。使用项目根目录中的这个 Dockerfile 构建后端镜像:
Dockerfile
FROM golang:1.25-alpine AS build
WORKDIR /app
COPY server.go .
RUN CGO_ENABLED=0 go build -o /server server.go
FROM alpine:3.20
COPY --from=build /server /server
EXPOSE 8080
CMD ["/server"]启动兼容 Docker 的引擎。在项目中安装 Wrangler,本地开发请使用 4.136.0 或更高版本,参见本地开发。
npm i -D wrangleryarn add -D wranglerpnpm add -D wranglerbun add -d wranglernpx wrangler devyarn wrangler devpnpm wrangler dev访问 http://localhost:8787/ 以加载前端及其小组件列表。