question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lazy loading for Pages and nested components in Production mode

See original GitHub issue

I just invested some time to try out Next.js if we would use it for our next project and my first impressions were very good! I also read on the Readme page, that one nice feature is the code splitting, that on the code will be loaded for the current page and all components that are used on this page. So this is a great feature to prevent that in large applications the whole code has to fetched when the user is going to the first page.

So I build a small sample application with two pages and two components and each page is using one of this components. So my expectation would be, that when the user is going to the first page, no code from the second page will be loaded. And when later the user is navigating to the second page, the code for this page and the nested components will be fetched separately.

But unfortunately, the response I got when the second page was fetched was this

          window.__NEXT_REGISTER_PAGE('/about', function() {
            var comp = module.exports=webpackJsonp([2],[],[242]);
            return { page: comp.default }
          })

So this was not the code I expected. I investigated a moment to find out where the code for the second page comes from and the outcome was, that the code is within the app.js and this file was fetched with the initial page request from the first page.

So I’m now a bit irritated about if the code splitting is really working? It seems me that not. Is there any explanation for that behaviour.

I used the version 2.3.1 from Next.js and I create a Production version for my experiments.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
bensalilijamescommented, May 9, 2017

No worries 😃 It’s from the minChunks part of webpack’s CommonsChunkPlugin: https://github.com/zeit/next.js/blob/master/server/build/webpack.js#L104

1reaction
bensalilijamescommented, May 9, 2017

You may be falling victim to the idea that for code to be shared between pages (i.e. in app.js) it needs to be in greater than or equal to 50% of pages.

Because this example only has two pages, it’s putting everything in app.js (nothing is used in less than 50% of the pages). To see the code-splitting benefit, try adding a third page!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy loading for Pages and nested components in Production ...
So I build a small sample application with two pages and two components and each page is using one of this components. So...
Read more >
Lazy loading React components - LogRocket Blog
A component created using React.lazy() is loaded only when it needs to be rendered. Therefore, you should display some kind of placeholder ...
Read more >
Lazy-loading feature modules - Angular
To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. AppRoutingModule (excerpt)
Read more >
Lazy loading nested components - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
How to Implement Lazy Loading in Angular 11
Lazy loading is a technique that permits you to load the javascript components only when their respective routes are active or being hit....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found