-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Are nextjs's dynamic imports more performant than an array of components? #2690
Comments
Please have a look at these lessons: https://learnnextjs.com/excel/lazy-loading-modules You can't use variables in dynamic imports. |
Why can't you use variables in dynamic imports? Seems like it kind of defeats the purpose |
@tweeres04 the purpose of dynamic imports is to dynamically load modules only when they are required, that allow to reduce the size of the bundle, e.g. only load admin related modules and components if the user is an admin user. |
The TC39 proposal mentions passing a variable or template string literals in order to not only lazy load, but also pick which module to load at runtime based on some context. An example from the proposal is Will this ever be supported by next.js? I am hoping to achieve something like Either way, the documentation suggests the tc39 import spec is supported, which it only partially supports. I think some clarification on that would clear things up for some. |
I'd love to hear some clarification as well if possible. @arunoda? |
More info: Webpack should make this possible according to this article: https://dev.to/kayis/dynamic-imports-with-webpack-2 (I couldn't find anything in Webpack's own documentation) but it doesn't seem to work with Next's config for some reason |
Ok, after doing more research it seems that Webpack itself only allows for string literals or template string literals (i.e. backtick strings). If a template string is provided then Webpack will bundle together all files that could possibly match the template into one chunk. Also, after reading the TC39 proposal more carefully it seems like " I still don't know what's the best way to import a single module dynamically given a runtime-determined name. |
No official statements on this yet? |
Maybe this could be achieved by providing a set of available options to the dynamic require somehow. There is no way to support any value for the dynamic import, as webpack need to know what bundles to prepare in advance. I'd be surprised if this was possible in a feasible way. |
I tried doing something like dynamic(import(`./blog/${postSlug}`)) as mentioned by @tweeres04, with Next 7. It does load the component but SSR doesn't work (it shows "loading..."). Is this behavior expected? @arunoda |
It is not expected, but indicates you're probably doing something non-standard, can you provide a minimal reproduction in a new issue? Following the issue template. |
@timneutkens thanks! I ended up going with what you suggested here: #5208 (comment) - I'll create a minimal repro when I have time / run into this again. |
Title may sound dumb, but I can't use variables w/ the dynamic variables.
Fails but
dynamic(import('./page'))
works.That's fine I guess - I'm currently circumventing this by having all of my templates in an array and using some logic to figure out which template to use by its key value.
However, it looks like I could maybe do this with the
modules{}
object that dynamic takes. Would it be more performant or would it ultimately be the same thing?The text was updated successfully, but these errors were encountered: