Skip to content
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

Dynamic import is loading all dependency even it's lazy loaded. Next 7.0.0 #5221

Closed
ptdede opened this issue Sep 20, 2018 · 11 comments · Fixed by #5249
Closed

Dynamic import is loading all dependency even it's lazy loaded. Next 7.0.0 #5221

ptdede opened this issue Sep 20, 2018 · 11 comments · Fixed by #5249

Comments

@ptdede
Copy link

ptdede commented Sep 20, 2018

Bug report

Describe the bug

on "next": "^7.0.0", dynamic import is importing ALL of the module that is supposed to lazy loaded. unlike "next": "^6.1.2", the module is correctly loaded on demand.

To Reproduce

import dynamic from "next/dynamic";
const TextSideSlider: any = dynamic(
    import(/* webpackChunkName: 'TextSideSlider' */"./TextSideSlider") as any,
);

this component is used only on route /b. But, on route /a it's already loaded.

Expected behavior

Dynamic imported module loaded when it's needed. like on next 6.1.2.

Screenshots

"next": "^6.1.2"
on route /a
TextSideSlider component is not loaded.
screen shot 2018-09-20 at 14 25 08

on route /b
TextSideSlider component is loaded correctly on demand.
screen shot 2018-09-20 at 14 25 41

"next": "^7.0.0",
on route /a
TextSideSlider component is already loaded
screen shot 2018-09-20 at 14 28 30

on route /b
nothing changed
screen shot 2018-09-20 at 14 28 42

System information

  • OS: macOS
  • Browser chrome
  • Version of Next.js: 6.1.2 & 7.0.0
@tusbar
Copy link
Contributor

tusbar commented Sep 20, 2018

Probably dupe of #5213

@ptdede
Copy link
Author

ptdede commented Sep 20, 2018

@tusbar Yes.. Maybe it's related?
I tried to rm -rf node_modules/.cache, rm -rf .next, then npm run build. but the all dynamic module is still loaded initially in my project.

using your repo it's correctly loaded the dynamic component on demand when i do rm -rf node_modules/.cache. and remove Message component inside page/index.js
the message component chunk only loaded on /about page. Now i am shocked. hahaha. how can?

@dnakov
Copy link

dnakov commented Sep 20, 2018

This worked for me:

import dynamic from "next/dynamic";
const TextSideSlider: any = dynamic(
    () => import(/* webpackChunkName: 'TextSideSlider' */"./TextSideSlider") as any,
);

@ptdede
Copy link
Author

ptdede commented Sep 20, 2018

@dnakov well i just try with @tusbar repo. I confirm it works. hahaha. But not in my case. I still try to figure it out whats wrong with my code. comeback later after found out what's wrong.

@ptdede
Copy link
Author

ptdede commented Sep 20, 2018

@dnakov Oh wait. i didn't pay attention to your code. you're trying to return anonymous function within dynamic function? my typescript compiler is complaining about that

Argument of type '() => any' is not assignable to parameter of type 'Promise<ComponentType<{}>>'. 
Property 'then' is missing in type '() => any'.

@dnakov
Copy link

dnakov commented Sep 20, 2018

yeah, sorry, this wasnt typescript-correct. just meant that i put the import in a function so it doesn’t get loaded immediately

@ptdede
Copy link
Author

ptdede commented Sep 24, 2018

@dnakov no problem, i get your idea. Thanks. 👍

@timneutkens
Copy link
Member

#5249 fixes this issue.

@ptdede
Copy link
Author

ptdede commented Sep 24, 2018

Thanks @timneutkens ! Will try after release

@ptdede
Copy link
Author

ptdede commented Sep 27, 2018

This works using next 7.0.1-canary.2:
thanks for the fix @timneutkens 👍

const TextSideSlider = dynamic(() => import(/* webpackChunkName: 'TextSideSlider' */ "./TextSideSlider"));

@timneutkens
Copy link
Member

@ptdede I've updated your comment to use the recommended syntax 🙌

@lock lock bot locked as resolved and limited conversation to collaborators Sep 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants