-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Introduce dynamic(() => import()) #5249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce dynamic(() => import()) #5249
Conversation
Partly fixes #5213 as well. |
@tusbar I've spent this morning testing against your reproduction and it seems like my patch mzgoddard/hard-source-webpack-plugin#446 fixes the wrong assignment of bundles too, I'm going to temporarily release the package under a prefix so that we can get rid of these issues 👍 |
Been testing this branch a bit since we are also doing something similar to #5208 (we get the page composition from an API and dynamically load components based on if the page needs them) and we want the benefits of webpack 4 (great job, btw). In ProfChaos' example repo https://github.com/ProfChaos/nexttest The original page import Icon from '../components/Icon'
export default () => {
return (
<div>
<h1>Only load "one"</h1>
<Icon name="one" />
</div>
)
} This now works, the extra chunks are not loaded. But if I do this import Icon from '../components/Icon'
export default () => {
return (
<div>
<h1>Only load "one"</h1>
<Icon name="one" />
<Icon name="one" />
</div>
)
} Now the "one" chunk loads twice, and if you add e.g. 15 edit: export default () => {
return (
<div>
<h1>Only load "one"</h1>
<Icon name="one" />
<Icon name="one" />
<Icon name="one" />
<Icon name="one" />
<Icon name="one" />
<Icon name="one" />
<Icon name="one" />
<Icon name="one" />
<Icon name="one" />
<Icon name="one" />
</div>
)
} |
Re: #5249 (comment) Looks like it still doesn't work, spent most of the day debugging. |
@Fumler great catch, I've fixed it and added a test for it. 👍 |
@timneutkens I think this might be causing a different bug with dynamic imports, see #5305 |
Fixes #5221
Fixes #5208
Fixes #5213
Bringing back some old non-standard behavior:
Will compile to:
next/dynamic
accepts a loader function as first argumentnext/dynamic
usage with justdynamic(import())
is transpiled to a loader function,The reason for these changes is that calling
import()
will immediately load the module when the code is executed, which is what most users don't want. This is to mirror Next 6 and below behavior around this syntax. Do note that it's non-standard and has to be deprecated. We can easily write a codemod to change this usage to the new loader function syntax.preloadReady
react-loadable function can filter out only the modules needed for hydration, instead of loading everything and waiting for that. This will introduce a performance boost when you are usingnext/dynamic
but also make sure that modules aren't loaded when you don't render them.TODO v7.1