Loading...
Loading...
Edge Rendering generates and serves dynamic pages at the network edge, eliminating origin round-trips for content that varies by city, service, or user segment.
npx skill4agent add itallstartedwithaidea/agent-skills edge-renderinggraph TD
A[Request: /google-ads/chicago] --> B[Worker: Parse city + service]
B --> C{Edge Cache Hit?}
C -->|Hit| D[Return Cached HTML]
C -->|Miss| E[Fetch City Data from KV]
E --> F[Fetch Service Data from D1]
F --> G[Render Template]
G --> H[Inject JSON-LD Structured Data]
H --> I[Generate Meta Tags]
I --> J[Cache at Edge]
J --> K[Return Fresh HTML]
D --> L[Background: Revalidate if Stale]export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url);
const [, service, city] = url.pathname.split("/");
const cacheKey = new Request(url.toString());
const cache = caches.default;
let response = await cache.match(cacheKey);
if (response) {
const age = parseInt(response.headers.get("age") ?? "0");
if (age > 3600) {
ctx.waitUntil(revalidate(env, cache, cacheKey, service, city));
}
return response;
}
return revalidate(env, cache, cacheKey, service, city);
},
} satisfies ExportedHandler<Env>;
async function revalidate(
env: Env, cache: Cache, cacheKey: Request, service: string, city: string
): Promise<Response> {
const [cityData, serviceData] = await Promise.all([
env.CITY_KV.get<CityData>(city, "json"),
env.DB.prepare("SELECT * FROM services WHERE slug = ?").bind(service).first<ServiceData>(),
]);
if (!cityData || !serviceData) {
return new Response("Not Found", { status: 404 });
}
const html = renderPage(cityData, serviceData);
const response = new Response(html, {
headers: {
"Content-Type": "text/html;charset=UTF-8",
"Cache-Control": "public, s-maxage=86400, stale-while-revalidate=3600",
},
});
await cache.put(cacheKey, response.clone());
return response;
}
function renderPage(city: CityData, service: ServiceData): string {
const jsonLd = {
"@context": "https://schema.org",
"@type": "LocalBusiness",
name: `${service.name} in ${city.name}`,
address: { "@type": "PostalAddress", addressLocality: city.name, addressRegion: city.state },
geo: { "@type": "GeoCoordinates", latitude: city.lat, longitude: city.lng },
url: `https://googleadsagent.ai/${service.slug}/${city.slug}`,
};
return `<!DOCTYPE html>
<html lang="en">
<head>
<title>${service.name} in ${city.name} | googleadsagent.ai</title>
<meta name="description" content="${service.description} for businesses in ${city.name}, ${city.state}." />
<script type="application/ld+json">${JSON.stringify(jsonLd)}</script>
</head>
<body>${service.template.replace("{{city}}", city.name)}</body>
</html>`;
}Promise.allstale-while-revalidate| Platform | Support | Notes |
|---|---|---|
| Cursor | Full | Worker development + deployment |
| VS Code | Full | Wrangler integration |
| Windsurf | Full | Edge-first development |
| Claude Code | Full | Template + Worker generation |
| Cline | Full | Edge architecture support |
| aider | Partial | Template generation only |
edge-renderingcity-service-pagescloudflare-workersseojson-ldstale-while-revalidatedynamic-pagesstructured-data