vercel/next.js
next_ssg removes exported variables after the first in multi-declarator export statements
Summary
Context: The
next_ssgtransform in Next.js removes server-side data fetching functions (getStaticProps,getStaticPaths,getServerSideProps) and their unused dependencies from the client bundle.Bug: When multiple variable declarators are exported in a single
export conststatement, only the first declarator is protected from removal, while subsequent declarators can be incorrectly removed even though they are exported.Actual vs. expected: Exported variables in positions 2+ of a multi-declarator export statement are incorrectly removed if they’re only referenced in data-fetching functions; they should be preserved because they’re exported and may be imported by other modules.
Impact: Exported constants become
undefinedat runtime when imported by other modules, causing application failures.
Code with bug
Example
Expected: All three exports (first, second, third) are preserved because they are exported and may be imported elsewhere.
Actual: Only first is explicitly marked as referenced outside data functions. second and third are seen only within getStaticPropsand are removed by the tree-shaker:
visit_mut_export_decladds onlyfirsttorefs_from_other.secondandthirdare added only torefs_from_data_fn.With:
second and third satisfy should_remove = true and are dropped, leading to undefined when imported by other modules.
Recommended fix
Copying the proposed change from the exploration: iterate all declarators, not just the first.
