Skip to content

Cache: add the OpenNextCache entrypoint (Cloudflare) - #44

Merged
conico974 merged 20 commits into
mainfrom
conico/cache-2-cf-entrypoint
Sep 22, 2026
Merged

conico974 merged 20 commits into
mainfrom
conico/cache-2-cf-entrypoint

Conversation

@conico974

@conico974 conico974 commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Part 2 of 6 of the cache stack, split out of #31. Review only this PR's diff — it is based on the previous branch.

PR Base
#43 — core: dedicated cache handler function conico/share-build (#35)
👉 #44 — cloudflare: OpenNextCache entrypoint #43
#45 — core: route all caching through the cache override ⚠️ breaking #44
#46 — port SWR tag revalidation from AWS #45
#47 — core: honour Cache-Control on cache entries #46
#48 — cloudflare: per-entrypoint Workers caching #47

Builds the cache handler function as part of the worker and exposes it through the OpenNextCache named entrypoint.

  • service-cache.ts is a cache override speaking the cache handler HTTP API over the NEXT_CACHE_SERVICE binding.
  • buildCacheFunction emits the bundle from beforeServerBundle, because the worker imports the entrypoint; core's own cache step is skipped.
  • runWithCloudflareContext initialises the Cloudflare context outside of a request, for entrypoints invoked over RPC. The origin is populated separately, on the first call coming from the fetch handler.
  • withoutSelfEntrypointServices strips the self referencing binding before handing the config to getPlatformProxy, which cannot resolve a named entrypoint of the worker it is configuring.
  • Drops compile-cache-assets-manifest.ts, unreferenced.

Additive. defineCloudflareConfig is untouched — the entrypoint is built and exported but nothing routes to it yet. It is landed first so that PR 3 can flip core and Cloudflare together without leaving Cloudflare without a working cache.

…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.
@pkg-pr-new

pkg-pr-new Bot commented Aug 16, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 9cdc1b4

@conico974 conico974 mentioned this pull request Aug 16, 2026
2 tasks

@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 found 2 potential issues.

View 4 additional findings in Devin Review.

Open in Devin Review

Comment thread packages/cloudflare/src/utils/wrangler-config.ts
Comment thread examples-cloudflare/overrides/r2-incremental-cache/wrangler.jsonc
Add a standalone cache handler function that owns the incremental cache, the
tag cache and CDN invalidation, and a `cache` override that other functions use
to reach it.

- `adapters/cache-adapter.ts` exposes `GET/PUT/DELETE /cache/*` and
  `POST /cache/revalidate-tags` through `createGenericHandler`.
- `build/createCacheBundle.ts` bundles it to `.open-next/cache-function`, wired
  into `buildAdapter` behind the new `skipCache` option and reported as
  `cacheFunction` in the OpenNext output.
- New top level `cacheHandler` config option carries the incremental cache, tag
  cache and cdn invalidation overrides for that function.
- New `cache` override with `fetch`, `local` and `dummy` implementations, and a
  shared wire format in `utils/cache-get.ts` that splits the entry metadata into
  headers and the payload into the body.

Nothing is removed here: the existing in-process `incrementalCache` and
`tagCache` overrides keep working unchanged.
Build the OpenNext cache handler function as part of the worker and expose it
through a named entrypoint, so the cache can later run behind a service binding
- in this worker or in one of its own.

- `service-cache.ts` is a `cache` override that speaks the cache handler HTTP
  API over the `NEXT_CACHE_SERVICE` binding.
- `buildCacheFunction` emits the bundle from `beforeServerBundle`, because the
  worker imports the entrypoint; core's own cache step is skipped.
- `runWithCloudflareContext` initialises the Cloudflare context outside of a
  request, for entrypoints invoked over RPC. The origin is now populated
  separately, on the first call coming from the fetch handler.
- `withoutSelfEntrypointServices` strips the self referencing binding before
  handing the config to `getPlatformProxy`, which cannot resolve a named
  entrypoint of the worker it is configuring.
- Drop `compile-cache-assets-manifest.ts`, unreferenced.

`defineCloudflareConfig` is untouched: the entrypoint is built and exported but
nothing routes to it yet.
@conico974
conico974 force-pushed the conico/cache-2-cf-entrypoint branch from e65c368 to 8b63860 Compare August 16, 2026 13:47
@vicb
vicb added this pull request to stack #52 September 21, 2026 07:15
Base automatically changed from conico/cache-1-core-fn to main September 21, 2026 11:50
Copilot AI lite review requested due to automatic review settings September 21, 2026 13:29

@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

rebased and added a couple of minor changes

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.

Copilot review overview

🔵 Needs a closer look

The cache handler’s dependency installation options are not propagated when building the cache function.

Review effort: Lite
Findings: None

What changed in this PR

Adds the Cloudflare OpenNextCache named entrypoint and service-binding cache client.

Changes:

  • Builds and exports the cache handler entrypoint.
  • Adds cache service bindings and local-development context handling.
  • Updates templates, examples, tests, and changeset documentation.
File Summary
packages/​cloudflare/​templates/​wrangler.jsonc Adds the cache service binding.
packages/​cloudflare/​src/​utils/​wrangler-config.ts Removes self-entrypoint services for local proxying.
packages/​cloudflare/​src/​utils/​wrangler-config.spec.ts Tests Wrangler configuration rewriting.
packages/​cloudflare/​src/​cli/​templates/​worker.ts Exports OpenNextCache.
packages/​cloudflare/​src/​cli/​templates/​init.ts Supports requestless context initialization.
packages/​cloudflare/​src/​cli/​templates/​cache-entrypoint.ts Defines the cache entrypoint.
packages/​cloudflare/​src/​cli/​commands/​utils/​helpers.ts Uses sanitized Wrangler configuration.
packages/​cloudflare/​src/​cli/​build/​open-next/​compile-cache-entrypoint.ts Compiles the entrypoint bundle.
packages/​cloudflare/​src/​cli/​build/​open-next/​compile-cache-assets-manifest.ts Removes obsolete manifest generation.
packages/​cloudflare/​src/​cli/​adapter.ts Builds the cache function; cache handler install options need propagation.
packages/​cloudflare/​src/​api/​overrides/​cache/​service-cache.ts Implements the service-binding cache client.
packages/​cloudflare/​src/​api/​overrides/​cache/​service-cache.spec.ts Tests cache service operations.
packages/​cloudflare/​src/​api/​cloudflare-context.ts Handles self-entrypoint services in development.
examples-cloudflare/​prisma/​wrangler.jsonc Adds the cache binding.
examples-cloudflare/​playground16/​wrangler.jsonc Adds the cache binding.
examples-cloudflare/​overrides/​static-assets-incremental-cache/​wrangler.jsonc Adds the cache binding.
examples-cloudflare/​overrides/​r2-incremental-cache/​wrangler.jsonc Adds cache bindings.
examples-cloudflare/​overrides/​memory-queue/​wrangler.jsonc Adds the cache binding.
examples-cloudflare/​overrides/​kv-tag-next/​wrangler.e2e.jsonc Adds the cache binding.
examples-cloudflare/​overrides/​d1-tag-next/​wrangler.e2e.jsonc Adds the cache binding.
examples-cloudflare/​e2e/​pages-router/​wrangler.jsonc Adds the cache binding.
examples-cloudflare/​e2e/​experimental/​wrangler.jsonc Adds the cache binding.
examples-cloudflare/​e2e/​app-router/​wrangler.jsonc Adds the cache binding.
examples-cloudflare/​e2e/​app-pages-router/​wrangler.jsonc Adds the cache binding.
.changeset/​cloudflare-cache-entrypoint.md Documents the new entrypoint and configuration.

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

@conico974

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the changes @vicb

@conico974
conico974 merged commit 9a8a92d into main Sep 22, 2026
9 checks passed
@conico974
conico974 deleted the conico/cache-2-cf-entrypoint branch September 22, 2026 08:26
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