App Router Server Components shift data-fetching behavior between next dev and next start
0 reputation · 13 Nov 2022, 13:34 UTC
0 reputation · 13 Nov 2022, 13:34 UTC
Next.js 13 introduced the App Router with React Server Components as the default, changing how data fetching and client interactivity work compared to the Pages Router. While next dev runs with NODE_ENV=development and enables hot module replacement, next start after next build executes in production mode with minification and different caching defaults.
Establish a repeatable development workflow where data-fetching patterns, error boundaries, and streaming behavior observed locally match the deployed output: 'standalone' container.
The App Router's default fetch caching (force-cache vs no-store) and React Server Component boundaries behave differently under Turbopack (next dev --turbo) versus the webpack-based production build. Environment variables inlined at build time via NEXT_PUBLIC_ prefixes are not refreshed without rebuilding, while .env.local values read at runtime in server components may diverge between next dev and next start.
next dev --turbo replicate the production fetch cache semantics for Server Components?next.config.js flags ensure .next/standalone includes all runtime-required server modules?Suspense boundaries render identically in both modes?37275 reputation · 13 Nov 2022, 19:38 UTC
In Next.js 13+ App Router, next start after next build runs the compiled Node.js server from .next/standalone with fetch defaulting to force-cache for Server Components. This behavior is consistent across all output: 'standalone' bundles.
output: 'standalone' bundles all compiled server modules and their dependencies by default. No next.config.js flags are strictly required for basic runtime modules, but next/dynamic imports using ssr: false or dynamic import() may not appear in the standalone output unless an explicit loading component is provided or ssr: true is set.
Environment variables prefixed with NEXT_PUBLIC_ are inlined at build time, making them identical between next dev and next start. Runtime .env.local values read via process.env in Server Components may diverge between the two modes without a rebuild, potentially causing data-fetch or UI differences.
next dev --turbo uses Turbopack, which applies per-request caching heuristics that may differ from the production http server, especially regarding Cache-Control header retention and incremental revalidation. Explicit no-store directives are respected in both modes, but intermediate caching behavior can vary.
CI pipelines seeking identical Suspense boundary streaming must account for Turbopack memory-based rendering versus Node.js streamed rendering. Using next build && next start in CI and comparing captured HTML streams via Playwright or react-test-renderer provides a reproducible verification path.
next build && next start (or next build && npx serve .next/standalone).next dev --turbo (if Turbopack is enabled) or next dev (webpack).curl -I http://localhost:3000/route in both modes and compare Cache-Control headers; identical force-cache directives confirm parity.Suspense streaming: Use a headless browser script (e.g., Playwright) to capture the full streamed HTML from both modes. Assert that Suspense fallback positions and markup order match..env.local values that change between modes without rebuilding. If .env.local must be tested, rebuild the standalone bundle after any change, or mock process.env in CI tests.next start.next/dynamic or dynamic import() not reachable from the entry point may be omitted, requiring explicit loading components or manual webpackChunkNext configuration..env.local values read in Server Components are not auto-reloaded between next dev and next start; a CI pipeline must either rebuild on env change or mock process.env to avoid stale data in verification steps.Use comments to ask for clarification. Post a solution as an answer.
7,140 reputation · 13 Nov 2022, 23:32 UTC
Turbopack's in-memory fetch cache in next dev --turbo does not honor force-cache/no-store the same way the production disk cache does. In development you'll often see stale responses that would be revalidated or rejected after next build. A quick local check: run next build && next start with NODE_ENV=production, then compare x-nextjs-cache headers via curl -I against the Turbopack dev server for the same route. The header will show HIT/MISS/STALE in production but is absent or misleading in --turbo.
For CI, the most reliable gate is a headless container that executes next build && next start and uses Playwright to snapshot streaming Suspense boundaries at 0 ms, 500 ms, and 2 s. Compare rendered HTML (not RSC payload) against a committed baseline; this catches chunk-ordering hydration mismatches that HMR masks.