๐ฐ๏ธ Edge-First Flows โ
Flows that are aware they might be running on the edge (Cloudflare, Vercel, Deno) and automatically adapt. They use edge-native caching and can split execution between edge and client.
Browser Request
Edge Request (Cloudflare/Vercel)
Client Runtime
150ms
Edge Runtime
The Concept โ
Modern frameworks run code both on the edge and in the browser. Traditional async libraries only optimize the browser. Edge-First Flows detect the runtime, utilize the Edge Cache API, and allow you to "split" heavy processing to the edge while keeping UI updates on the client.
Quick Start โ
ts
const { data, execute } = useFlow(getPersonalizedFeed, {
edge: {
enabled: true,
runtime: "auto", // Detects Cloudflare, Vercel, Deno, or Browser
cache: {
strategy: "stale-while-revalidate",
ttl: 60, // Edge cache TTL in seconds
},
split: {
// Run heavy pre-processing on the edge
edge: preprocessFeed,
// Run light personalization on the client
client: personalizeFeed,
},
},
});Runtime Detection โ
The engine automatically detects where the code is executing:
| Runtime | Detection Logic |
|---|---|
cloudflare | globalThis.caches exists, no HTMLElement |
vercel | globalThis.EdgeRuntime exists |
deno | globalThis.Deno exists |
browser | window and document exist |
node | process.versions.node exists |
Split Execution โ
When split is configured:
- If on Edge: Only the
edgefunction runs. The result is returned to the client. - If on Client: The
clientfunction receives the result of theedgeexecution and enriches it before returning the final data to the UI.
This is perfect for Next.js API Routes, Nuxt Server Routes, or Cloudflare Workers.
Unified Edge Cache โ
The EdgeCache utility provides a single interface that uses the Cache API when available (Edge + Browser), and falls back to a memory map when it isn't.
mermaid
graph TD
A["flow.execute()"] --> B{"Is Cache API Available?"}
B -->|Yes| C["Read/Write to Edge Cache"]
B -->|No| D["Read/Write to Memory Map"]API Reference โ
EdgeDetector โ
ts
import { EdgeDetector } from "@asyncflowstate/core";
EdgeDetector.detect(); // 'cloudflare', 'browser', etc.
EdgeDetector.isEdge(); // true if Cloudflare, Vercel, or Deno
EdgeDetector.isBrowser(); // true if browserEdgeCache โ
ts
import { EdgeCache } from "@asyncflowstate/core";
const cache = new EdgeCache({ ttl: 60, strategy: "stale-while-revalidate" });
await cache.set("my-key", { foo: "bar" });
const data = await cache.get("my-key");
await cache.invalidate("my-key");
await cache.clear();Configuration โ
| Option | Type | Default | Description |
|---|---|---|---|
enabled | boolean | false | Enable edge awareness |
runtime | string | 'auto' | Target runtime or auto-detect |
cache | Object | โ | Edge cache configuration |
split | Object | โ | Functions for split execution |
