open-next icon indicating copy to clipboard operation
open-next copied to clipboard

How does 'use cache' behave when deployed

Open leanderiversen opened this issue 1 month ago • 2 comments

Hi,

This is not an issue, but hoping to get some insight into how use cache differs in local development and OpenNext, as it looks like use cache behaves like use cache: remote based on:

Next: https://nextjs.org/docs/app/api-reference/config/next-config-js/cacheHandlers#handler-types OpenNext: https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/core/util.ts#L56

All of our web applications are currently using cache: no-store for staging and cache: force-cache for production, so I'm just trying to find the smoothest transition over to cacheComponents.

Thank you for your hard work!

leanderiversen avatar Oct 07 '26 10:10 leanderiversen

Your reading of the storage behavior is right, with one distinction. OpenNext replaces the handler for plain use cache; it does not rewrite the directive to use cache: remote.

On current main, cacheHandlers.default points to the composable handler. That handler delegates its reads and writes to globalThis.incrementalCache, using the composable cache type. With the S3 incremental-cache override, those entries therefore go to S3 rather than remaining solely in one Next.js process. Other overrides can change that storage behavior.

Next.js's default and remote names select handlers. A handler named default can itself use external storage, which is what explains the difference you noticed.

For your staging transition, treat fetch caching and the cached function/component boundary separately. If staging must stay uncached, choose the uncached loader outside the use cache wrapper, using an explicit server-side environment flag. Deployed staging also runs a production build, so NODE_ENV alone won't distinguish it from production. With Cache Components, put the component doing uncached async work under <Suspense>.

Compare a local production build with the deployed adapter, rather than relying only on next dev. This is source analysis of current OpenNext, not an AWS runtime test; pin your Next.js and OpenNext versions before validating the migration.

IlyaKhalafi avatar Oct 18 '26 21:10 IlyaKhalafi

Thanks for the question, and thanks @IlyaKhalafi for the detailed answer! To sum up: OpenNext registers its own handler as cacheHandlers.default, and that handler stores entries in the configured incremental cache (S3 by default). So a plain "use cache" is shared across instances, much like "use cache: remote" on Vercel.

Note that since 4.1.15 the entries are stored under a hash of the cache key (#1342).

Closing, but feel free to reopen if anything is still unclear.

vicb avatar Nov 13 '26 13:11 vicb