Does Fastify Work With Cloudflare Pages?
Fastify can run on Cloudflare Pages via Functions, but you're limited to Cloudflare's serverless runtime constraints rather than full Node.js.
Quick Facts
How Fastify Works With Cloudflare Pages
Fastify technically runs on Cloudflare Pages Functions, which execute in Cloudflare's V8 JavaScript runtime (not full Node.js). This means you get Fastify's performance benefits for request handling, but you lose Node.js-specific APIs like the `fs` module and child processes. The typical architecture involves wrapping your Fastify app as a Pages Function handler, where incoming requests are routed to Fastify's request listener. However, Cloudflare Pages Functions have a 30-second timeout limit and 128MB memory constraint, which may be tight for complex applications. The real friction point is that Fastify expects a traditional HTTP server listening on a port—Pages Functions work via event handlers instead. You'll need adapter code to bridge this gap, making the setup more complex than deploying to traditional Node.js hosts. For most use cases, you're better served using Cloudflare Workers directly with a lighter framework, or using Pages for static content with Workers handling API routes.
Best Use Cases
Fastify on Cloudflare Pages Functions
npm install fastify// functions/api.ts - Cloudflare Pages Function
import Fastify from 'fastify';
const app = Fastify({ logger: false });
app.get('/hello', async (request, reply) => {
return { message: 'Hello from Fastify!', timestamp: new Date() };
});
app.post('/data', async (request, reply) => {
const body = request.body as Record<string, unknown>;
return { received: body, processed: true };
});
export const onRequest: PagesFunction = async (context) => {
const { request } = context;
const url = new URL(request.url);
// Route to Fastify
return app.routing(request as any);
};Known Issues & Gotchas
V8 runtime doesn't support all Node.js APIs (fs, child_process, net module)
Fix: Audit your Fastify app for Node.js-specific dependencies. Use Cloudflare's APIs (KV, D1, R2) as replacements. Consider using Workers instead for better Node.js compatibility.
30-second timeout limit on Pages Functions can timeout long-running requests
Fix: Offload heavy work to background jobs or implement request queuing. Keep endpoint logic lean and delegate processing to external services.
Fastify's server.listen() pattern doesn't work in serverless context
Fix: Use a Pages Function adapter that converts Fastify to a handler function. Export the handler directly rather than starting a server.
Cold starts may negate Fastify's low-overhead advantages
Fix: Keep your Fastify bundle small, remove unused plugins, and consider using Workers instead for better performance characteristics.
Alternatives
- •Cloudflare Workers with Hono or Itty Router - native serverless framework without Node.js compatibility layer
- •Vercel with Fastify - traditional Node.js runtime with full framework support and simpler deployment
- •AWS Lambda with Fastify - more mature serverless Node.js environment with better timeout and memory options
Resources
Related Compatibility Guides
Explore more compatibility guides