Documentation

Workers Runtime

ready

How NextDeploy compiles and runs Next.js 14, 15, and 16 (including native Turbopack) inside Cloudflare Workers (workerd) with nodejs_compat_v2, R2 asset serving, and a 1.60 MB gzipped Worker bundle.

Source reference: shared/nextcompile/, shared/nextcompile/runtime_src/nextserver/, cli/internal/serverless/cloudflare/adapter/, cli/internal/serverless/cloudflare/bindings.go


Why a Compiler Instead of a Node Process Bridge

On AWS Lambda or a VPS, NextDeploy can run Next.js inside a Node.js process. Cloudflare Workers run on V8 isolates (workerd) with no OS processes and no local filesystem.

Instead of shipping third-party adapter npm packages or forcing --webpack, NextDeploy compiles .next/standalone directly in Go (nextcompile) + in-process esbuild into a single self-contained worker.mjs:

  • Zero external npm dependencies in your app's package.json.
  • Auto-detects Turbopack vs Webpack (engine: auto chooses nextserver for Turbopack builds and legacy for classic Webpack builds).
  • Runs real NextNodeServer via a build-time Virtual Filesystem, static server chunk registry, streaming Web↔Node HTTP bridge, and R2+KV CacheHandler.

Compatibility Date & Flags

Recommended configuration for Next.js 15/16 apps:

serverless:
  provider: cloudflare
  cloudflare:
    engine: auto # auto | nextserver | legacy
    compatibility_date: "2026-05-01" # validated YYYY-MM-DD (min 2025-09-01 for node:fs)
    compatibility_flags:
      - nodejs_compat # requires nodejs_compat or nodejs_compat_v2

NextDeploy validates compatibility_date (YYYY-MM-DD) and requires nodejs_compat or nodejs_compat_v2 before uploading. With nodejs_compat on modern workerd, Cloudflare natively provides node:async_hooks (AsyncLocalStorage), node:buffer, node:constants, node:crypto, node:diagnostics_channel, node:events, node:path, node:perf_hooks, node:process, node:querystring, node:stream (stream/promises, stream/consumers), node:string_decoder, node:timers (timers/promises), node:url, node:util, node:zlib, node:assert, and partial implementations of node:net, node:tls, node:dns (dns/promises), and node:module.

For Node APIs that workerd does not implement (or leaves as throwing [unenv] stubs):

  • fs / node:fs / node:fs/promises → backed by NextDeploy's build-time Virtual Filesystem (vfs_fs_shim.mjs, reporting UTF-8 byte lengths in statSync).
  • process.listeners, process.on/off/emit, process.cwd, process.uptime, process.hrtime.bigint, process.stdout/stderr → polyfilled before boot by ensureProcessCompat().
  • vm.runInNewContext (evalManifest) → replaced with static chunk-registry execution (100% CSP-compliant).
  • tty, os, child_process, worker_threads, http, https → safe CJS stubs (isatty: () => false, real globalThis.performance); non-framework imports of stubbed built-ins log an explicit build warning and are recorded in compat-report.json.
  • Native .node binary addons → rejected at bundle time with an actionable error instead of copying unusable binaries.

Bundle Size, Compatibility Report & Build Guarantees

Cloudflare enforces gzipped Worker script limits of 3 MiB on the Free plan and 10 MiB on the Paid plan.

NextDeploy builds into an isolated staging directory (.nextdeploy-cf-staging-*) and atomically promotes .nextdeploy-cf only after bundling and validation succeed (preserving any prior good build on failure). On every build it trims dev/experimental/alternate-bundler runtimes, logs the top bundle contributors, and emits .nextdeploy-cf/_nextdeploy/compat-report.json with:

  • Engine, Next.js/React versions, min_compatibility_date vs configured date, and bundle_sha256
  • Raw & gzipped byte counts with automatic Free (3 MiB) / Paid (10 MiB) tier warnings
  • Full inventory of native_builtins, partial_builtins, stubbed_app_modules, stubbed_framework_modules, trimmed_packages, and detected Next.js feature capabilities

Worker Exports & Bindings

Every compiled Worker exports:

  • fetch(request, env, ctx) — runs edge protection guard, /_next/image, R2 static/SSG fast paths, and NextNodeServer.
  • scheduled(event, env, ctx) — dispatches Cron Triggers to POST /api/cron.
  • queue(batch, env, ctx) — dispatches Cloudflare Queue batches to POST /api/queue/[name].

String secrets and env vars folded into the Worker upload are automatically mirrored into process.env at request time, while Cloudflare bindings (env.DB, env.ASSETS, env.KV, env.HYPERDRIVE, env.AI) are available via globalThis.__nextdeployEnv.

Observability & Logs

Stream live edge logs from your deployed Worker:

nextdeploy logs

Post-deploy SmokeVerify probes your custom domain root and up to 3 public static routes (automatically skipping Next.js internal /_not-found and /_global-error routes) and reports exact status codes and latencies.

Related