Skip to content

feat: Include CF exec context in RouteOptions #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/content/docs/core/routing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ The request handler function takes in the following options:
2. `params`: The matched parameters from the request URL.
3. `env`: The Cloudflare environment.
4. `ctx`: The context object (See [Middleware & Context](#middleware-context)).
5. `cf:` Cloudflare's [Execution Context API](https://developers.cloudflare.com/workers/runtime-apis/context/) methods, e.g. `waitUntil()`

Return values:
- `Response`: A standard response object.
Expand Down
10 changes: 8 additions & 2 deletions sdk/src/runtime/lib/router.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { isValidElementType } from "react-is";
import { ExecutionContext } from "@cloudflare/workers-types";

export type HandlerOptions<TContext = Record<string, any>> = {
request: Request;
env: Env;
cf: ExecutionContext;
ctx: TContext;
headers: Headers;
rw: RwContext<TContext>;
};

export type RouteOptions<TContext = Record<string, any>, TParams = any> = {
cf: ExecutionContext;
request: Request;
params: TParams;
env: Env;
Expand All @@ -19,7 +22,7 @@ export type RouteOptions<TContext = Record<string, any>, TParams = any> = {

export type PageProps<TContext> = Omit<
RouteOptions<TContext>,
"request" | "headers" | "rw"
"request" | "headers" | "rw" | "cf"
> & { rw: { nonce: string } };

export type LayoutProps<TContext> = PageProps<TContext> & {
Expand Down Expand Up @@ -134,12 +137,14 @@ export function defineRoutes<TContext = Record<string, any>>(
): {
routes: Route<TContext>[];
handle: ({
cf,
request,
ctx,
env,
rw,
headers,
}: {
cf: ExecutionContext;
request: Request;
ctx: TContext;
env: Env;
Expand All @@ -150,7 +155,7 @@ export function defineRoutes<TContext = Record<string, any>>(
const flattenedRoutes = flattenRoutes(routes);
return {
routes: flattenedRoutes,
async handle({ request, ctx, env, rw, headers }) {
async handle({ cf, request, ctx, env, rw, headers }) {
const url = new URL(request.url);
let path = url.pathname;

Expand All @@ -162,6 +167,7 @@ export function defineRoutes<TContext = Record<string, any>>(
// Find matching route
let match: RouteMatch<TContext> | null = null;
const routeOptions: RouteOptions<TContext> = {
cf,
request,
params: {},
ctx,
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/runtime/worker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare global {

export const defineApp = <Context,>(routes: Route<Context>[]) => {
return {
fetch: async (request: Request, env: Env, _ctx: ExecutionContext) => {
fetch: async (request: Request, env: Env, cf: ExecutionContext) => {
globalThis.__webpack_require__ = ssrWebpackRequire;

const router = defineRoutes(routes);
Expand Down Expand Up @@ -127,6 +127,7 @@ export const defineApp = <Context,>(routes: Route<Context>[]) => {
const userHeaders = new Headers();

const response = await router.handle({
cf,
request,
headers: userHeaders,
ctx: {} as Context,
Expand Down