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

feat: lazily load plugin scripts and dependencies when needed (DHIS2-10518) #1546

Merged
merged 10 commits into from
Mar 11, 2021

Conversation

amcgee
Copy link
Member

@amcgee amcgee commented Feb 17, 2021

  1. Remove blocking external javascript script tags from index.html head
  2. Dynamically load legacy plugin scripts when they are needed, not before
  3. Dynamically import() the Data Visualizer plugin so that it can be code-split out of the main bundle. (NOTE: for this to really be useful it requires a tree-shakable analytics library, which we don't have yet)

This also removes the need for a custom index.html and for setting any environment variables in development

DV and Maps plugins will hopefully move to dynamic modules, but this solution will still be required for eventReports and eventVisualizer

Here is a production example : https://debug.dhis2.org/2.35dev/api/apps/dashboard2/index.html#/Goz4vyRx2cy

NOTE: ER and EV plugins don't work locally because of CORS

TODO:

  • Decide whether to use prefetch, preload, or neither for data-visualizer-plugin chunk (currently prefetching)
  • Investigate EV EXT error
  • Show <LoadingMask> while fetching scripts
  • Remove console.logs

@jenniferarnesen
Copy link
Collaborator

jenniferarnesen commented Feb 18, 2021

  • only map - loads map.js and dv plugin. Dashboard without map - no map.js
  • no visualizations should not load any plugins. Actual - loads dv plugin, but not the other plugins
  • only ev - loads eventchart.js and dv plugin chunk
  • only er - loads eventreport.js and dv plugin chunk
  • only chart/pivot - loads dv plugin chunk and no other plugins

I'm getting a bunch of errors on the production instance resulting in ev not rendering:

3UiManager.js?d38e:536 Uncaught ReferenceError: Ext is not defined
    at m.n.alert (UiManager.js?d38e:536)
    at Object.<anonymous> (Layout.js?bf56:459)
    at u (jquery-3.3.1.min.js:2)
    at Object.fireWith [as rejectWith] (jquery-3.3.1.min.js:2)
    at k (jquery-3.3.1.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery-3.3.1.min.js:2)

Dashboard with only a text item
image

Dashboard with a chart and pivot table
image

@amcgee
Copy link
Member Author

amcgee commented Feb 18, 2021

@jenniferarnesen the duplication of data-visualizer-plugin chunk there is expected - the javascript type is from a <link> element with rel="prefetch" inject into the html by webpack to prefetch and cache the script as early as possible, but not block rendering. That way it's immediately available if it's needed later. I'd like to investigate whether we want to switch to preload instead... but I don't think this shouold be detrimental here.

prefetch | Specifies that the browser should preemptively fetch and cache the target resource as it is likely to be required for a follow-up navigation
preload | Specifies that the browser agent must preemptively fetch and cache the target resource for current navigation according to the destination given by the "as" attribute (and the priority associated with that destination).

@amcgee
Copy link
Member Author

amcgee commented Feb 18, 2021

Strange that we're seeing the EV EXT error now though.... i am seeing it too but it was working for me before. Will have to investigate that.

@amcgee
Copy link
Member Author

amcgee commented Feb 18, 2021

Re: the EV EXT error

This error also appears on the current dashboard https://debug.dhis2.org/2.35dev/dhis-web-dashboard/#/PZIxO0YLxFt -- the eventchart.js plugin tries to use EXT to show an alert saying "the user admin doesn't have access to this program", but there's no EXT available (that's only in the app itself). It works with the system user, which has access to all programs. So this appears to be a bug in the EV plugin and/or we need to also load EXT when loading that plugin (which can be done but... ew)

@amcgee amcgee marked this pull request as ready for review March 11, 2021 00:40
@amcgee
Copy link
Member Author

amcgee commented Mar 11, 2021

@jenniferarnesen I've updated this a bit (better late than never) to add loading mask and remove console logs, the implementation changed a bit to support this. Think we can get this in to 2.36 before the hard freeze?

Comment on lines 93 to 99
onLoadingComplete={this.onLoadingComplete}
forDashboard={true}
style={pluginProps.style}
userSettings={{
displayProperty: this.context.userSettings
.keyAnalysisDisplayProperty,
}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forDashboard, userSettings and onLoadingComplete could be moved to DataVisualizerPlugin. Should be pretty straightforward (onLoadingComplete just sets a state var that is only relevant for DataVisualizerPlugin). But if you don't have time for this, I can do it. If its not done by noon (either by me or you), then let's just go ahead and merge as is, and I can do this later.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes committed

@amcgee
Copy link
Member Author

amcgee commented Mar 11, 2021

@jenniferarnesen nice, much cleaner thanks! Changes look good, shall we merge? 👍

@amcgee amcgee merged commit c13eafe into master Mar 11, 2021
@amcgee amcgee deleted the feat/lazy-load-plugins branch March 11, 2021 11:41
dhis2-bot added a commit that referenced this pull request Mar 11, 2021
# [31.14.0](v31.13.10...v31.14.0) (2021-03-11)

### Features

* lazily load plugin scripts and dependencies when needed (DHIS2-10518) ([#1546](#1546)) ([c13eafe](c13eafe))
@dhis2-bot
Copy link
Contributor

🎉 This PR is included in version 31.14.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants