fix(app-router): ignore very dynamic requests during build analysis#12
Closed
NathanDrake2406 wants to merge 1 commit into
Closed
fix(app-router): ignore very dynamic requests during build analysis#12NathanDrake2406 wants to merge 1 commit into
NathanDrake2406 wants to merge 1 commit into
Conversation
App Router production builds currently fail when a page or route module contains guarded require(dynamic) calls. That differs from Next.js, where requests with no static request part are ignored during graph analysis and only fail if executed at runtime. The violated invariant was that bundler analysis should not turn unreachable, very dynamic requests into build-time failures. vite-plugin-commonjs tried to expand require(dynamic) as a static glob before the branch could remain runtime-only code. Add a pre-transform that preserves directives, removes only very dynamic require calls from CJS graph analysis, and marks matching dynamic import calls with @vite-ignore. The regression builds both an App Router page and route module ported from Next.js dynamic-requests coverage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
import(...)andrequire(...)calls.packages/vinext/src/plugins/ignore-dynamic-requests.ts,packages/vinext/src/index.ts,tests/build-optimization.test.tsrequire(dynamic)/import(dynamic)patterns that Next.js accepts.Why
App Router production builds must separate graph-analysis inputs from code that is only meaningful at runtime. Next.js/Turbopack treats requests with no static known path part as "very dynamic" and, with
ignore_dynamic_requests, avoids adding them to the module graph. Vinext violated that boundary becausevite-plugin-commonjstried to expandrequire(dynamic)as a static glob, causing a build-time failure even when the code was unreachable.require(...)calls that have no static request part to a runtime helper before CommonJS analysis.import(...)/* @vite-ignore */only for imports with no static request part.What changed
require(dynamic)with no static path partvite-plugin-commonjswith an invalid dynamic import error.import(dynamic)with no static path part@vite-ignore.require('./' + name)orimport(./${name})Maintainer review path
packages/vinext/src/plugins/ignore-dynamic-requests.tsvalidates the request classification and transform boundaries.packages/vinext/src/index.tsplaces the plugin immediately beforevite-plugin-commonjs, which is the failing analyzer.tests/build-optimization.test.tsports the upstream dynamic-requests scenario at the production build boundary.Validation
vp test run tests/build-optimization.test.ts tests/cjs.test.tsvp checkvp env exec --node 24 ./scripts/run-nextjs-deploy-suite.sh /Users/nathan/Projects/vinext/.refs/nextjs-v16.2.6 --retries 0 -c 1 --debug test/e2e/app-dir/dynamic-requests/dynamic-requests.test.tsknipRisk / compatibility
require(...)calls are redirected to a runtime helper that throws if executed, matching the intended runtime-only failure shape.ignore_dynamic_requestsbehaviour for requests without a static known part.Non-goals
new URL, filesystem, or child process request patterns.References
ignore_dynamic_requestsoptionimport(...)handlingrequire(...)handling