Next.js / Advanced: caching modes, lifecycle and release evidence / Lesson 17
Runtime, environment, deployment and cache ownership
Match the build artifact to its runtime capabilities and keep secrets, cache lifetime and multi-instance behavior explicit.
On this page
Lesson goals
- Distinguish build-time public values from server-only runtime configuration.
- Compare server hosting and static export without assuming feature equivalence.
The deployment artifact carries assumptions
A Next build can contain prerendered output, server code, client assets and configuration-derived values. Choose a deployment mode that supports the required actions, Route Handlers, runtime APIs, image behavior and cache integration. A static export is not a drop-in replacement for a server application that needs request-time sessions or mutations. Different hosts can impose execution, filesystem and persistence constraints. Keep the selected runtime and cacheComponents mode together in review, because the opt-in model has its own Node/runtime restrictions. No framework label guarantees identical behavior across a long-lived process, container, serverless function and static file host.
Classify values by when and where they become visible
NEXT_PUBLIC_ variables intended for browser code are inlined during build under the documented contract. Changing the container environment after that build does not automatically rewrite those client bytes. Server-only values need an appropriate runtime read path, and prerendering can capture values earlier than a developer expects. The JSON note is a synthetic classification, not a real environment file or deployment configuration. Never place a credential under a public prefix simply to make it available in a client component. Keep approved public configuration and private source credentials in separate explicit contracts.
{"publicClientValue":{"name":"NEXT_PUBLIC_CATALOG_LABEL","binding":"build-time","containsSecret":false},"serverCredential":{"name":"CATALOG_CREDENTIAL","binding":"reviewed-server-runtime-path","value":"not-present"},"artifactPromotion":"verify-build-bound-values"}
The record contains no credential value and configures no process. It distinguishes binding times for review.
Process-local state is not shared durable infrastructure
Multiple instances do not automatically share module variables, in-memory sessions, replay maps or cache entries. A rolling deployment can also mix build identities and retained client assets. Define cache storage, invalidation propagation, key/build scope and retention under the chosen platform. A local filesystem may be ephemeral or unsuitable for shared uploads. Keep source pools and long-lived work owned by the runtime lifecycle, bound concurrency and propagate supported cancellation. Do not turn sticky routing into a durability claim or infer a backup from a successful cached response. Verify recovery and cross-instance behavior separately from a single-process example.
Parse a bounded local option without touching real environment
The parser defaults to ten only when a synthetic input is omitted, accepts a positive integer up to fifty and rejects malformed supplied text. These values are a lab contract, not hosting recommendations. The function never reads process.env, secrets or files. It demonstrates why a missing optional value and an invalid deployment value should not always select the same fallback. A real configuration adapter should validate before readiness and log setting names rather than private values. This test cannot prove that a build captured the intended configuration or that runtime changes reach the correct rendering phase.
function limit(text) {
if(text===undefined)return 10;
if(typeof text!=="string" || !/^[1-9][0-9]*$/.test(text))throw new Error("INVALID_LIMIT");
const n=Number(text);if(!Number.isSafeInteger(n)||n>50)throw new Error("INVALID_LIMIT");return n;
}
console.assert(limit(undefined)===10 && limit("20")===20);
let rejected=false;try {limit("20rows");}catch {rejected=true;}console.assert(rejected);
Omission and malformed supplied text have different outcomes. No environment, build or hosting operation is performed.
Review build, serve and rollback together
Validate exact package/runtime versions, build outputs, public-variable binding, runtime reads, asset paths and required server features before deployment. A healthy process is not proof that the application can serve its authoritative data, and a build success is not a complete accessibility or authorization test. Retain rollback combinations for server code and client assets. This course parses the classification and runs the local parser only; it does not build, export, deploy, start a process or inspect a cloud platform. Actual hosting, cache sharing, lifecycle and runtime integration need authorized isolated verification with honest evidence.
Common errors and fixes
A public variable leaks a credential to the client bundle
- Why it happens
- The public prefix was used as an easy transport for a secret.
- How to fix it
- Keep credentials server-only and project only approved public values.
- Prevent it next time
- Review generated client configuration and secret-free fixtures.
A promoted build still shows the previous public setting
- Why it happens
- A build-time value was assumed to be read at container runtime.
- How to fix it
- Rebuild or use a deliberate supported runtime-public-config design.
- Prevent it next time
- Record binding time in artifact-promotion review.
Two instances disagree about cached or replay state
- Why it happens
- Process-local storage was treated as shared durable state.
- How to fix it
- Use a reviewed shared protocol where the domain requires it.
- Prevent it next time
- Test cross-instance and replacement behavior separately.
Check your understanding
1. Does changing NEXT_PUBLIC_ after build update already emitted browser code?
Show worked solution
Not automatically. The documented browser values are bound into the build output. A later environment change does not rewrite those bytes. Choose a deliberate runtime-public configuration mechanism if needed and review its audience, freshness and deployment behavior separately from server-only runtime secrets.
2. Why does static export not replace every server deployment?
Show worked solution
Request-time sessions, actions and other server features require execution capabilities a static file host does not provide. Compare the exact feature set rather than assuming framework branding makes modes equivalent. Verify the intended output and hosting contract before choosing a smaller deployment mode.
Key takeaways
- Build-time binding and runtime reads require different evidence.
- Deployment mode and shared-state ownership are part of application correctness.