Skip to content

POC - Add support for Cloudflare Containers - #39

Merged
conico974 merged 25 commits into
mainfrom
conico/cf-container
Sep 18, 2026
Merged

conico974 merged 25 commits into
mainfrom
conico/cf-container

Conversation

@conico974

@conico974 conico974 commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Introduce support for Cloudflare Containers, including configuration updates, request normalization, and E2E tests. Update dependencies and enhance context management with AsyncLocalStorage.

Just a POC for now, final version will require the new cache interface


Open in Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/opennextjs/adapters-api/@opennextjs/aws@8b264bb
npm i https://pkg.pr.new/opennextjs/adapters-api/@opennextjs/cloudflare@8b264bb
npm i https://pkg.pr.new/opennextjs/adapters-api/@opennextjs/core@8b264bb

commit: 8b264bb

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

…y into core adapter

feat: enhance build process with additional server bundle customization options

chore: update package.json scripts for improved build and testing workflow

test: add unit tests for adapter build process and server bundle generation

fix: ensure proper handling of external dependencies and edge configuration in server bundle
…udflare specific overrides"

This reverts commit 37c4d90.
…ad of throwing

- Extract ValidateConfigResult type with success/message/shouldThrow/level
- Convert validateFunctionOptions and validateSplittedFunctionOptions to return result objects
- Remove logger dependency from validateConfig.ts
- Preserve compatibilityMatrix, TODO comment, @ts-expect-error pragmas
- Add 5 characterization tests in validateConfig.spec.ts
- No caller impact: compileConfig.ts is the sole importer (updated in T3)
- Export OpenNextOutput interface (was internal)
- Extract buildOpenNextOutput(buildOpts) for construction-only (no fs write)
- Keep legacy generateOutput as thin wrapper (construction + file write)
- Preserve all construction logic verbatim, including @ts-expect-error
- Add 3 characterization tests in generateOutput.spec.ts
- Backward compatible: byte-equivalent output to today
- Replace bare validateConfig(config) call with result-handling block
- Throw on shouldThrow:true (bad routes — preserves existing behavior)
- Log at appropriate level on shouldThrow:false (level field from T1)
- All 3 export signatures and edge-runtime detection block unchanged
- Direct callers (aws/build.ts, cloudflare/utils.ts) unaffected
…OpenNextAdapterOptions

- Make OpenNextAdapterOptions<T = OpenNextOutput> and buildAdapter<T> generic
- Add validateConfig override hook (runs after callback in modifyConfig)
- Add generateOutput override hook (returns T, gated by skipGenerateOutput)
- buildAdapter serializes override return via fs.writeFileSync (override never touches fs)
- Default path uses buildOpenNextOutput (extracted in T2)
- Add 5 new tests covering override behaviors + default path + skipGenerateOutput
- All 16 existing adapter tests preserved; AWS/Cloudflare adapters compile with default T
When an adapter config specifies full package-specifier paths (e.g.,
@opennextjs/aws/overrides/wrappers/aws-lambda.js), esbuild cannot
resolve them during bundling. Use createRequire(args.path).resolve()
in the openNextResolvePlugin to convert package specifiers to
filesystem-relative paths at build time, falling back to the original
value if resolution fails.

This fixes the openbuild:local build error:
  ERROR: Could not resolve "@opennextjs/aws/overrides/wrappers/aws-lambda.js"
  ERROR: Could not resolve "@opennextjs/aws/overrides/tagCache/dynamodb.js"

Added test I verifying resolution of a mock package in node_modules.
@conico974
conico974 force-pushed the conico/share-build branch from d1b99ee to f2de8e2 Compare August 1, 2026 09:23
@conico974
conico974 force-pushed the conico/cf-container branch from bf347d0 to ca6b7d7 Compare August 1, 2026 09:23
@conico974
conico974 force-pushed the conico/share-build branch from 4e9a367 to cd8f83e Compare August 2, 2026 09:46
Base automatically changed from conico/share-build to main August 28, 2026 10:57
# Conflicts:
#	examples-cloudflare/e2e/app-router/package.json
#	packages/aws/src/adapter.ts
#	packages/cloudflare/src/cli/adapter.ts
#	packages/core/src/build/adapter.spec.ts
#	packages/core/src/build/adapter.ts
#	packages/core/src/build/createMiddleware.ts
#	packages/core/src/build/createServerBundle.ts
#	packages/core/src/build/generateOutput.spec.ts
#	packages/core/src/build/generateOutput.ts
#	packages/core/src/build/middleware/buildNodeMiddleware.ts
#	packages/core/src/build/validateConfig.spec.ts
#	packages/core/src/build/validateConfig.ts
#	packages/core/src/plugins/resolve.spec.ts
#	packages/core/src/plugins/resolve.ts
Forward string-valued Worker bindings, including vars and secrets, through the Container SDK envVars field while excluding object service bindings.

Add focused tests for binding filtering and the OpenNextContainer runtime environment.
Generate the default server image from a glibc Node 24 base and run npm rebuild after copying the traced application so native dependencies target the container platform rather than the build host.

Extract and test the Dockerfile generator, including command ordering.
Fail configuration validation when container mode declares split functions because the generated Worker only routes to the default container.

Add focused validation coverage so unsupported bundles cannot be silently generated and ignored.
Overwrite x-forwarded-proto from the public request URL at the trusted Container Durable Object boundary and use that header when the Node converter reconstructs the request URL.

Cover spoofed incoming headers and HTTPS conversion in Cloudflare and core converter tests.
Reattach the original client signal after middleware request conversion and pass the Durable Object request signal into the Container SDK reconstruction path.

Add tests proving both forwarding boundaries retain cancellation behavior and keep the generated template type-safe.
Record the new Cloudflare Containers configuration and runtime support as a minor Cloudflare release, and the target-platform Docker dependency rebuild as a core patch.
Copilot AI lite review requested due to automatic review settings September 18, 2026 06:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The broad experimental container integration requires final human review.

Pull request overview

Adds experimental Cloudflare Containers support, including container builds, request forwarding, configuration, dependency updates, and E2E coverage.

Changes:

  • Adds container runtime, Durable Object integration, and configuration.
  • Generates Dockerfiles and container bundles.
  • Updates tests, dependencies, CI, and release notes.
File summaries
File Summary
pnpm-workspace.yaml Updates Wrangler.
pnpm-lock.yaml Locks updated dependencies.
packages/tests-unit/tests/converters/node.test.ts Tests forwarded HTTPS handling.
packages/core/src/overrides/converters/node.ts Normalizes forwarded protocol.
packages/core/src/build/dockerfile.ts Generates container Dockerfiles.
packages/core/src/build/dockerfile.spec.ts Tests Dockerfile generation.
packages/core/src/build/createServerBundle.ts Integrates generated Dockerfiles.
packages/core/src/build/adapter.ts Supports selected config paths.
packages/core/src/build/adapter.spec.ts Tests config path selection.
packages/cloudflare/src/cli/templates/container.ts Adds container Worker template.
packages/cloudflare/src/cli/commands/build.ts Propagates config paths.
packages/cloudflare/src/cli/build/utils/ensure-cf-config.ts Validates container configuration.
packages/cloudflare/src/cli/build/utils/ensure-cf-config.spec.ts Tests configuration validation.
packages/cloudflare/src/cli/build/utils/copy-package-cli-files.ts Selects Worker templates.
packages/cloudflare/src/cli/build/utils/copy-package-cli-files.spec.ts Tests template copying.
packages/cloudflare/src/cli/build/open-next/compileContainer.ts Compiles the container Durable Object.
packages/cloudflare/src/cli/adapter.ts Integrates container builds.
packages/cloudflare/src/api/container.ts Implements container request handling.
packages/cloudflare/src/api/container.spec.ts Tests container behavior.
packages/cloudflare/src/api/config.ts Adds container configuration.
packages/cloudflare/src/api/config.spec.ts Tests container defaults.
packages/cloudflare/package.json Adds container dependency.
examples-cloudflare/e2e/app-router/wrangler.container.jsonc Adds container Wrangler configuration.
examples-cloudflare/e2e/app-router/package.json Adds container scripts.
examples-cloudflare/e2e/app-router/open-next.container.config.ts Enables container mode.
examples-cloudflare/e2e/app-router/e2e/playwright.container.config.ts Configures container E2E runs.
examples-cloudflare/e2e/app-router/e2e/container.test.ts Adds container E2E tests.
examples-cloudflare/common/config-e2e.ts Supports configurable E2E runs.
.github/workflows/local.yml Runs container E2E tests.
.changeset/cloudflare-containers.md Documents release changes.
Review details

Files not reviewed (1)

  • pnpm-lock.yaml: Generated file

Suppressed comments (1)

packages/cloudflare/src/cli/build/utils/ensure-cf-config.ts:109

  • When the user selects a custom config via --openNextConfigPath, this validation error still tells them to edit open-next.config.ts, which can send them to the wrong file. Please either pass the resolved config path into validation or use wording that refers to the selected OpenNext config generically.
		const containerErrorMessage =
			"The `open-next.config.ts` should use this configuration for Cloudflare Containers:\n\n" +
			`{
  • Files reviewed: 29/30 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@vicb vicb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@conico974
conico974 merged commit bf593d7 into main Sep 18, 2026
9 checks passed
@conico974
conico974 deleted the conico/cf-container branch September 18, 2026 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants