This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
[Tree Shake] Next.js emit unused code into the main
bundle
#35900
Labels
bug
Issue was opened via the bug report template.
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
next build / export
Describe the Bug
I notice the
main
bundle emitted bynext build
is a bit heavy, so I use@next/bundle-analyzer
to analyze my bundle:And there are many features I don't use at all that are included in the bundle:
next/dist/shared/amp.js
I don't use the AMP feature at all. So I take a look at the source code.
shared/lib/amp.ts
has 2 named export,useAmp
andisInAmpMode
:next.js/packages/next/shared/lib/amp.ts
Lines 4 to 15 in 0eb9f7e
isInAmpMode
is then imported byshared/lib/head.tsx
:next.js/packages/next/shared/lib/head.tsx
Line 5 in fbfe430
I do use
next/head
component, so it is very reasonable thatisInAmpMode
is included in my bundle. However,useAmp
is also included in the final bundle:next/dist/client/script.js
I don't use
next/script
component at all. And thescriptLoader
property of the__NEXT_DATA__
is also an empty array:So I take a look at the source code again.
client/script.tsx
has a default export and a named exportinitScriptLoader
:next.js/packages/next/client/script.tsx
Lines 132 to 134 in fbfe430
...which is then imported by
client/index.tsx
:next.js/packages/next/client/index.tsx
Lines 254 to 257 in 0eb9f7e
However, the entire
next/dist/client/script.js
is still included in the finalmain
bundle:next/dist/client/streaming/vitals.js
I do use
reportWebVitals
named export on my custom_app
, but I don't useunstable_useWebVitalsReport
. So once again, I take a look at the source code ofnext.js
.There are tons of named export at
client/streaming/vitals.ts
:next.js/packages/next/client/streaming/vitals.ts
Line 5 in 0eb9f7e
next.js/packages/next/client/streaming/vitals.ts
Lines 10 to 12 in 0eb9f7e
next.js/packages/next/client/streaming/vitals.ts
Lines 19 to 22 in 0eb9f7e
next.js/packages/next/client/streaming/vitals.ts
Line 24 in 0eb9f7e
flushBufferedVitalsMetrics
andtrackWebVitalMetric
are imported byclient/index.tsx
:next.js/packages/next/client/index.tsx
Lines 36 to 39 in 0eb9f7e
... which is used by
<Root />
component:next.js/packages/next/client/index.tsx
Lines 1032 to 1036 in 0eb9f7e
... and
hydrate
function:next.js/packages/next/client/index.tsx
Line 323 in 0eb9f7e
However,
useWebVitalsReport
is also included in my final bundle:next/dist/client/streaming/refresh.js
I am pretty sure I don't use
useRefreshRoot
at all. And Next.js itself doesn't usesuseRefreshRoot
either. However, it is still included in the final bundle:Expected Behavior
All those unused codes I mentioned above should not be included in the final
main
bundle.To Reproduce
I introduce
@next/bundle-analyzer
into another bare bone Next.js reproduction (the one I used in #35286), and all those unused codes are included in themain
bundle:The text was updated successfully, but these errors were encountered: