// wrangler.jsonc{ "$schema": "node_modules/wrangler/config-schema.json", "compatibility_date": "2026-03-09", "name": "my-vike-cloudflare-app", "main": "vike:server-entry", // Only required if your app (or one of your libraries) uses a Node.js API "compatibility_flags": ["nodejs_compat"]}
// pages/+onCreateGlobalContext.server.js// Environment: serverexport { onCreateGlobalContext }import { env } from 'cloudflare:workers'async function onCreateGlobalContext(globalContext) { // Making the environment variable LOG_LEVEL available on the client-side globalContext.logLevel = env.LOG_LEVEL || 'info'}
// pages/+onCreateGlobalContext.server.ts// Environment: serverexport { onCreateGlobalContext }import { env } from 'cloudflare:workers'import type { GlobalContextServer } from 'vike/types'async function onCreateGlobalContext(globalContext: GlobalContextServer) { // Making the environment variable LOG_LEVEL available on the client-side globalContext.logLevel = env.LOG_LEVEL || 'info'}declare global { namespace Vike { interface GlobalContext { logLevel: string } }}
Cloudflare Workers requires your entire worker code to be bundled into a single file — you can use Wrangler to achieve that (it uses esbuild under the hood).
Cloudflare uses the term "worker code" to denote server code that is run on its edge infrastructure.
For a significantly faster development experience we recommend, whenever possible, using Vite's development server (or a server such as Express.js or Hono) instead of Wrangler.
In other words:
Skip wrangler / Cloudflare Workers altogether while developing your app.
Use wrangler dev to preview your worker.
Use wrangler deploy to deploy your worker to Cloudflare Workers.
When using Node.js(/Bun/Deno) for development and Cloudflare Workers for production, you may need a fetch() function that works in both environments.
You can define a fetch function at pageContext.fetch that works in all environments.
The trick is to add a different fetch() implementation to pageContextInit at renderPage(pageContextInit).