AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Inline BeforeInteractive Scripts Are Duplicated In RSC Flight Payload Causing Significant Page Weight Overhead

next/script strategy="beforeInteractive" inline scripts are serialized twice: once into the executable __next_s push in <head> and again into the React Server Components flight payload as client component props. The flight copy has no production reader, causing up to ~20 KB gzip overhead per pageview on real pages.

highConfidence 92%Next.jsAffected V16.3.1-Canary.3

Origin Analysis

next/script is implemented as a client component. For strategy="beforeInteractive" inline scripts, the component renders an executable __next_s push containing the full script body, while its props (including the same body via dangerouslySetInnerHTML) are also serialized into the RSC flight payload for client hydration. The flight copy is never consumed in production because app-bootstrap reads only __next_s and hydration does not diff inline content.
1. Create a Next.js app using the provided repro (https://github.com/carriemorrison/nextjs-doubleship-repro) or add an inline <Script strategy="beforeInteractive" dangerouslySetInnerHTML={{ __html: largeBody }} /> in the root layout.\n2. Run `next build`.\n3. Inspect `.next/server/app/index.html` and search for a unique substring from the script body.\n4. Observe the substring appears twice: once in the __next_s push in <head> and once in an inert __next_f flight row.

Fixing Code Block

Edge Case Audit

This change removes the body from the flight payload, which may affect dev-mode hydration mismatch warnings or any future client-side consumer of that data. To mitigate, keep a separate dev-only path that serializes the body for HMR/diff checks, and add a regression test to verify beforeInteractive scripts still execute correctly in production. Rollback: revert to the previous next/script implementation if any unexpected client-side errors occur, especially in apps that rely on non-standard flight payload access. Version pinning is recommended during rollout.

Ecosystem Topology