[Discover] Optimize Discover plugin page load bundle#208298
[Discover] Optimize Discover plugin page load bundle#208298davismcphee merged 27 commits intoelastic:mainfrom
Conversation
ed317eb to
3b9a8c8
Compare
0957f74 to
2e512cc
Compare
|
Pinging @elastic/kibana-data-discovery (Team:DataDiscovery) |
💛 Build succeeded, but was flaky
Failed CI StepsTest Failures
Metrics [docs]Module Count
Public APIs missing comments
Async chunks
Public APIs missing exports
Page load bundle
Unknown metric groupsAPI count
async chunk count
References to deprecated APIs
History
cc @davismcphee |
| */ | ||
| overrideServices: Partial<DiscoverServices>; | ||
| getDiscoverServices: () => DiscoverServices; | ||
| getDiscoverServices: () => Promise<DiscoverServices>; |
There was a problem hiding this comment.
I wonder what are benefits of making services async if the app can't work without them?
There was a problem hiding this comment.
I'm not sure if you mean in the specific case of this component, or generally loading services async in the Discover plugin.
In this specific case it's because they're loaded async in the plugin but this component is exposed synchronously on the Discover plugin start contract, so we need to fetch them async when rendering the container. If it seems cleaner, we could instead wrap this in a HOC within the React.lazy call that first loads the services and then passes them synchronously to the container, although the end result will be the same loading wise.
If you mean the benefits of loading services async in the Discover plugin generally, then it's because plugin page load bundles are loaded for every enabled plugin every time Kibana is loaded, regardless of which page. So if a user opens the home page and has no intention of going to Discover in their session, the entire Discover page load bundle still has to be loaded. All plugins should limit their page load bundles to the bare minimum required to minimize that impact, and load the remainder of what's needed to support the app only when interacting with it / navigating to it.
Also the patterns this PR introduces will prevent the page load bundle from growing much whenever we add new services or new code to existing services (e.g. ProfilesManager or DiscoverEBTManager). This is another big benefit since it protects us from changes in the future.
Discover is only a small piece of this, but there's a broader push for it across Kibana plugins right now, and this makes sure we're at least doing our part.
There was a problem hiding this comment.
Thanks for the explanation! Was not aware how much Discover plugin loads on other pages. Sounds like it could be something to improve in the plugins system too.
There was a problem hiding this comment.
Np, in hindsight I should've included more of these details in the PR summary anyway. And agreed there's probably room for improvement in the plugin system itself, but my understanding is it's tricky due to plugin dependencies. So for the most part I've been trying to follow the guidance in the plugin performance docs.
| } | ||
|
|
||
| forwardLegacyUrls(plugins.urlForwarding); | ||
| registerDiscoverAnalytics(core, this.discoverEbtContext$); |
There was a problem hiding this comment.
It used to run conditionally and at the moment of initialisation. I can look into it again on Monday.
There was a problem hiding this comment.
Yeah this was one of the trickiest parts to figure out, but I think it should be working the same now. We used to call ebtManager.initialize() directly in setup with the conditions hardcoded to true, so the registrations always happened whenever Kibana was loaded. I tried keeping the logic and calling initialize when the Discover app was mounted instead, but it seems core.analytics doesn't accept registrations after setup, so the events weren't working correctly and breaking the functional tests.
There was a problem hiding this comment.
Looks like the conditions are redundant now. Also I think it would be better to colocate registerDiscoverAnalytics() with the initialization and call it after ebtManager.initialize().
There was a problem hiding this comment.
Good point, I removed the flags here so the props are set whenever initialize is called: 85e126c.
If you mean colocating the code within the same file, I completely agree, but it seems plugins only support tree shaking at the file level. If we put the registration logic in the same file, it'll also bring the manager and all imports into the page load bundle. Not quite the same, but I renamed the registration file to discover_ebt_manager_registrations so they're at least side by side in the file tree (also renamed the function to registerDiscoverEBTManagerAnalytics). Hopefully that makes the relationship a bit clearer for others: c6a17b2.
I also can't call registerDiscoverEBTManagerAnalytics directly after initialize since the registrations need to happen synchronously in setup, and EBT manager code is loaded async, but I put the registration call directly below getEbtManager in the plugin file to also hopefully make it clearer.
|
There are some merge conflicts now. |
jughosta
left a comment
There was a problem hiding this comment.
LGTM 👍
Thanks for these optimizations!
|
Starting backport for target branches: 8.x https://github.com/elastic/kibana/actions/runs/13797576181 |
💔 All backports failed
Manual backportTo create the backport manually run: Questions ?Please refer to the Backport tool documentation |
|
Friendly reminder: Looks like this PR hasn’t been backported yet. |
|
Friendly reminder: Looks like this PR hasn’t been backported yet. |
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
## Summary This PR optimizes the Discover page load bundle by reducing it to only code which is actually required on startup, and dynamically loading other code when it's needed, resulting in a 55% decrease in the bundle size. Before (44.15 KB):  After (20.12 KB):  ### Checklist - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> (cherry picked from commit e1bffa6) # Conflicts: # src/platform/plugins/shared/discover/public/__mocks__/services.ts # src/platform/plugins/shared/discover/public/index.ts # src/platform/plugins/shared/discover/public/plugin.tsx # src/platform/plugins/shared/discover/public/types.ts
|
Looks like this PR has a backport PR but it still hasn't been merged. Please merge it ASAP to keep the branches relatively in sync. |
…214867) # Backport This will backport the following commits from `main` to `8.x`: - [[Discover] Optimize Discover plugin page load bundle (#208298)](#208298) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Davis McPhee","email":"davis.mcphee@elastic.co"},"sourceCommit":{"committedDate":"2025-03-11T20:30:25Z","message":"[Discover] Optimize Discover plugin page load bundle (#208298)\n\n## Summary\n\nThis PR optimizes the Discover page load bundle by reducing it to only\ncode which is actually required on startup, and dynamically loading\nother code when it's needed, resulting in a 55% decrease in the bundle\nsize.\n\nBefore (44.15 KB):\n\n\n\nAfter (20.12 KB):\n\n\n\n### Checklist\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n---------\n\nCo-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>","sha":"e1bffa6a9b6a82e347b1c1f4dbfaa7571fff546b","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Discover","release_note:skip","backport missing","Team:DataDiscovery","backport:version","v9.1.0","v8.19.0"],"title":"[Discover] Optimize Discover plugin page load bundle","number":208298,"url":"https://github.com/elastic/kibana/pull/208298","mergeCommit":{"message":"[Discover] Optimize Discover plugin page load bundle (#208298)\n\n## Summary\n\nThis PR optimizes the Discover page load bundle by reducing it to only\ncode which is actually required on startup, and dynamically loading\nother code when it's needed, resulting in a 55% decrease in the bundle\nsize.\n\nBefore (44.15 KB):\n\n\n\nAfter (20.12 KB):\n\n\n\n### Checklist\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n---------\n\nCo-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>","sha":"e1bffa6a9b6a82e347b1c1f4dbfaa7571fff546b"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/208298","number":208298,"mergeCommit":{"message":"[Discover] Optimize Discover plugin page load bundle (#208298)\n\n## Summary\n\nThis PR optimizes the Discover page load bundle by reducing it to only\ncode which is actually required on startup, and dynamically loading\nother code when it's needed, resulting in a 55% decrease in the bundle\nsize.\n\nBefore (44.15 KB):\n\n\n\nAfter (20.12 KB):\n\n\n\n### Checklist\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n---------\n\nCo-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>","sha":"e1bffa6a9b6a82e347b1c1f4dbfaa7571fff546b"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT-->
## Summary This PR optimizes the Discover page load bundle by reducing it to only code which is actually required on startup, and dynamically loading other code when it's needed, resulting in a 55% decrease in the bundle size. Before (44.15 KB):  After (20.12 KB):  ### Checklist - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Summary
This PR optimizes the Discover page load bundle by reducing it to only code which is actually required on startup, and dynamically loading other code when it's needed, resulting in a 55% decrease in the bundle size.
Before (44.15 KB):

After (20.12 KB):

Checklist
release_note:breakinglabel should be applied in these situations.release_note:*label is applied per the guidelines