-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove client only dynamic chunks from edge bundle (#56761)
### Issue In the client components world, when you're using `next/dynamic` with `ssr: false` to split chunks in pages of edge runtime, you could get the dynamic imported module still bundled in the server bundle for edge runtime. This could easily hit the bundle limit on edge runtime if you're loading a large size of non-SSR module. This is caused by the whole chunk is still being included when we're creating the client entry. Since the client entry is imported eagerily, webpack will bundle all the modules under it, unless it's explicitly marked not being included. ### Fix For client components, SSR rendering layer of bundle, non-SSR `next/dynamic` calls, we're transform the result of `dynamic()` call from to conditional import the dynamic loaded module. From ```js dynamic(() => import(...)) ``` To ```js dynamic(() => { require.resolveWeak(...) }, { ssr: false }) ``` This will only be applied to SSR layer bundle client components non-SSR `next/dynamic` calls and only when webpack is bundling since turbopack doesn't need this. In this way, the server side will be stripped but it can still enter the module graph since we need to traverse if there's SA in client modules with using webpack API `require.resolveWeak`. And for client side bundle will still include the actual module. Close NEXT-1703
- Loading branch information
Showing
17 changed files
with
272 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
11 changes: 11 additions & 0 deletions
11
...t-swc/crates/core/tests/fixture/next-dynamic-app-dir/no-ssr/output-server-client-layer.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import dynamic from 'next/dynamic'; | ||
export const NextDynamicNoSSRServerComponent = dynamic(async ()=>{ | ||
typeof require.resolveWeak !== "undefined" && require.resolveWeak("../text-dynamic-no-ssr-server"); | ||
}, { | ||
loadableGenerated: { | ||
modules: [ | ||
"some-file.js -> " + "../text-dynamic-no-ssr-server" | ||
] | ||
}, | ||
ssr: false | ||
}); |
4 changes: 3 additions & 1 deletion
4
packages/next-swc/crates/core/tests/fixture/next-dynamic/issue-48098/output-server.js
This file contains 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
This file contains 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
4 changes: 3 additions & 1 deletion
4
packages/next-swc/crates/core/tests/fixture/next-dynamic/wrapped-import/output-server.js
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use client' | ||
|
||
import dynamic from 'next/dynamic' | ||
|
||
const DynamicComponent = dynamic( | ||
() => import('../csr').then((mod) => mod.CSR), | ||
{ | ||
ssr: false, | ||
} | ||
) | ||
|
||
export default function Client() { | ||
return ( | ||
<div> | ||
<DynamicComponent /> | ||
</div> | ||
) | ||
} | ||
|
||
export const runtime = 'edge' |
This file contains 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
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/dynamic/app/dynamic-mixed-ssr-false/client-edge/page.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use client' | ||
|
||
export { default } from '../dynamic-import' | ||
|
||
export const runtime = 'edge' |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/dynamic/app/dynamic-mixed-ssr-false/client/page.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use client' | ||
|
||
export { default } from '../dynamic-import' |
14 changes: 14 additions & 0 deletions
14
test/e2e/app-dir/dynamic/app/dynamic-mixed-ssr-false/dynamic-import.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import dynamic from 'next/dynamic' | ||
|
||
const DynamicSSRFalse = dynamic(() => import('./ssr-false-module'), { | ||
ssr: false, | ||
}) | ||
|
||
export default function page() { | ||
return ( | ||
<div> | ||
<DynamicSSRFalse /> | ||
<p id="content">dynamic-mixed-ssr-false</p> | ||
</div> | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
test/e2e/app-dir/dynamic/app/dynamic-mixed-ssr-false/ssr-false-module/index.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Client from './ssr-false-client' | ||
import Server from './ssr-false-server' | ||
|
||
export default function Comp() { | ||
return ( | ||
<> | ||
<Client /> | ||
<Server /> | ||
</> | ||
) | ||
} |
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/dynamic/app/dynamic-mixed-ssr-false/ssr-false-module/ssr-false-client.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use client' | ||
|
||
export default function Comp() { | ||
return <p id="ssr-false-client-module">ssr-false-client-module-text</p> | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/dynamic/app/dynamic-mixed-ssr-false/ssr-false-module/ssr-false-server.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Comp() { | ||
return <p id="ssr-false-server-module">ssr-false-server-module-text</p> | ||
} |
Oops, something went wrong.