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

CSS Module Styles Lost After Client Navigation When Page CSS Gains `Data-N-P` After ISR Revalidation

In Pages Router, when a component with a CSS module is statically imported on one route and dynamically imported on another, after ISR revalidation of the static route, client navigation to the dynamic route removes the shared stylesheet and does not re-add it, resulting in missing styles.

highConfidence 85%Next.jsAffected V15.5.18Affected V16.3.0-Canary.103

Origin Analysis

Next.js client-side router removes page-level stylesheets (marked with `data-n-p`) on navigation, but does not account for stylesheets that are also present in the `dynamic-css-manifest.json`. This manifest was introduced in PR #72959 for dynamic CSS handling. When a CSS module is shared between a static import and a dynamic import, the same CSS file is both a page-level style and a dynamic dependency. After ISR regenerates the static page, the stylesheet link gains `data-n-p`. On client navigation to the dynamic route, the router removes any `data-n-p` link, eliminating the shared CSS. The dynamic import loader then assumes the CSS is already loaded (by href) and does not re-inject it, leaving the component unstyled.
1. Clone https://github.com/hshahdramafever/nextjs-bug-demo 2. Run `npm install && npm run build && npm start` 3. Wait about 5 seconds (or until the home page's `revalidate: 5` expires) after build finishes. 4. Open http://localhost:3000/ and click 'Go to second page →' — initially styled correctly. 5. Navigate back to home and reload the page (full document load). 6. Click 'Go to second page →' again. 7. Observe that the box has CSS module class names but no styles; DevTools shows the stylesheet link is removed and not re-fetched.

Fixing Code Block

Edge Case Audit

This workaround may cause duplicate stylesheet loading if the original link is not removed or if multiple route changes occur rapidly; it also introduces an extra network request for the manifest on every route change. It does not fix the underlying router logic and may behave inconsistently across Next.js versions. Users should test thoroughly and consider upgrading to an official fix when available. Rollback is straightforward: remove the code from `_app.js`.

Ecosystem Topology