elastic / kibana

Your window into the Elastic Stack
https://www.elastic.co/products/kibana
Other
19.71k stars 8.12k forks source link

Lazy loading React components #64179

Closed mshustov closed 4 years ago

mshustov commented 4 years ago

I created this issue to discuss the lazy loading strategy for react components used by different registries in Kibana. If you build Kibana platform plugins locally and check their sizes. You can notice that maps plugin size reaches 4.8Mb. That's mostly due to new optimizer architecture including all the plugin dependencies in the bundle and lack of code tree-shaking (WIP https://github.com/elastic/kibana/pull/62390). Even if we move some deps to shared dependencies and remove unused code, the build size will be quite significant (@elastic/ems-client, @elastic/maki only gives us 1Mb) Ideally, a plugin code should be as slim as possible and include the thing that necessary for proper registration. All other code must be loaded on demand.

I'm wondering if we can improve the situation with loading Maps React components on demand. To understand how it affects the initial load time let's look at maps.plugin.js size (this is the chunk loaded every time a user opens the Kibana): without lazy loading - 4.8Mb with MapViewComponent loaded lazily - 29Kb

There are several approaches to address the problem:

Maybe @elastic/kibana-app-arch has other ideas or opinion of whatever pattern is preferable?

elasticmachine commented 4 years ago

Pinging @elastic/kibana-platform (Team:Platform)

elasticmachine commented 4 years ago

Pinging @elastic/kibana-gis (Team:Geo)

mshustov commented 4 years ago

We have the same problem for visualizations registry as well:

elasticmachine commented 4 years ago

Pinging @elastic/kibana-operations (Team:Operations)

Dosant commented 4 years ago

Wonder if React.lazy and Suspense are working in Kibana env? If yes, I guess we won't need react-loadable?

mshustov commented 4 years ago

@Dosant yes. I did try it in our app and all integration tests passed. https://github.com/elastic/kibana/pull/64285 Regarding the other infrastructure:

I think you can use React.Lazy if you want. Do you want me to create a separate issue for @elastic/kibana-app-arch team to refactor visualizations plugins? That would be great if we can have it fixed until FF v7.8. There are some complaints about Kibana loading time.

Dosant commented 4 years ago

@restrry, thanks for checking react.lazy 👍 I guess which team fixes it depends on where we do lazy loading. If lazy loading will be handled inside each visualisation type by it's own - then I guess it would be @elastic/kibana-app. But if we somehow make it part of visualisations infrastructure, then @elastic/kibana-app-arch. I'll bring in up to the team

timroes commented 4 years ago

Related to https://github.com/elastic/kibana/issues/58280

Dosant commented 4 years ago

@restrry, @elastic/kibana-app-arch is going to look into generic approach for code splitting of app arch registries and components during our next planning week. In a meantime, if possible during 7.8, we suggest @elastic/kibana-app to look into specific visualisations implementations and wrap them with React.Lazy where it would benefit the most.