Skip to content

Commit e437fac

Browse files
committed
Add pages-functions-test fixture
Manual test fixture for the @cloudflare/pages-functions package. Demonstrates compiling a functions directory and running with wrangler.
1 parent 3078968 commit e437fac

File tree

11 files changed

+135
-0
lines changed

11 files changed

+135
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Pages Functions Test Fixture
2+
3+
Test fixture for `@cloudflare/pages-functions` package.
4+
5+
## Usage
6+
7+
From the workers-sdk root:
8+
9+
```bash
10+
# Build the pages-functions package first
11+
pnpm --filter @cloudflare/pages-functions build
12+
13+
# Install deps for this fixture
14+
pnpm --filter pages-functions-test install
15+
16+
# Compile functions -> worker, then bundle, then run dev
17+
pnpm --filter pages-functions-test dev
18+
```
19+
20+
## What it does
21+
22+
1. `build.mjs` uses `compileFunctions()` to compile `functions/` into `dist/worker.js`
23+
2. esbuild bundles `dist/worker.js` (with path-to-regexp) into `dist/bundled.js`
24+
3. wrangler runs the bundled worker
25+
26+
## Test endpoints
27+
28+
- `GET /` - Index route
29+
- `GET /api/hello` - Static API route
30+
- `POST /api/hello` - POST handler
31+
- `GET /api/:id` - Dynamic route
32+
- `PUT /api/:id` - Update handler
33+
- `DELETE /api/:id` - Delete handler
34+
35+
All routes go through the root middleware which adds `X-Middleware: active` header.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"version": 1,
3+
"description": "Generated by @cloudflare/pages-functions",
4+
"include": [
5+
"/*"
6+
],
7+
"exclude": []
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Build script that uses @cloudflare/pages-functions to compile
4+
* the functions directory into a worker entrypoint.
5+
*/
6+
import { mkdirSync, writeFileSync } from "node:fs";
7+
import { compileFunctions } from "@cloudflare/pages-functions";
8+
9+
// Ensure dist directory exists
10+
mkdirSync("./dist", { recursive: true });
11+
12+
// Compile the functions directory
13+
const result = await compileFunctions("./functions");
14+
15+
// Write the generated worker entrypoint
16+
writeFileSync("./dist/worker.js", result.code);
17+
console.log("✓ Generated dist/worker.js");
18+
19+
// Write _routes.json
20+
writeFileSync("./_routes.json", JSON.stringify(result.routesJson, null, 2));
21+
console.log("✓ Generated _routes.json");
22+
23+
console.log("\nRoutes:");
24+
for (const route of result.routes) {
25+
const method = route.method || "ALL";
26+
console.log(` ${method.padEnd(6)} ${route.routePath}`);
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const onRequest = async (context) => {
2+
const response = await context.next();
3+
response.headers.set("X-Middleware", "active");
4+
return response;
5+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const onRequestGet = (context) => {
2+
const { id } = context.params;
3+
return Response.json({ message: `Getting item ${id}` });
4+
};
5+
6+
export const onRequestPut = async (context) => {
7+
const { id } = context.params;
8+
const body = await context.request.json();
9+
return Response.json({ message: `Updating item ${id}`, data: body });
10+
};
11+
12+
export const onRequestDelete = (context) => {
13+
const { id } = context.params;
14+
return Response.json({ message: `Deleted item ${id}` });
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const onRequestGet = () => {
2+
return Response.json({ message: "Hello from GET /api/hello" });
3+
};
4+
5+
export const onRequestPost = async (context) => {
6+
const body = await context.request.json();
7+
return Response.json({
8+
message: "Hello from POST /api/hello",
9+
received: body,
10+
});
11+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const onRequest = () => {
2+
return new Response("Hello from the index!");
3+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@fixture/pages-functions-test",
3+
"private": true,
4+
"scripts": {
5+
"build": "node build.mjs",
6+
"dev": "node build.mjs && wrangler dev"
7+
},
8+
"dependencies": {
9+
"@cloudflare/pages-functions": "workspace:*"
10+
},
11+
"devDependencies": {
12+
"wrangler": "workspace:*"
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": ["//"],
3+
"tasks": {
4+
"build": {
5+
"dependsOn": ["@cloudflare/pages-functions#build"]
6+
}
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "pages-functions-test",
3+
"main": "dist/worker.js",
4+
"compatibility_date": "2026-01-20",
5+
}

0 commit comments

Comments
 (0)