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

[STALLED] Feature/4172 eliminate render blocking resources #4604

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from

Conversation

rfultz
Copy link
Contributor

@rfultz rfultz commented May 6, 2021

Summary

Required reviewers

Two front-end and/or UX?

Impacted areas of the application

Preloading the homepage hero has no downside.

Now we're delaying script loads for a few templates. The most likely complication is that one of the scripts won't have loaded when it was expected, meaning a component isn't activated or isn't ready to when a user thinks it should have been. This could happen the first time a site visitor requests a script after our last deploy (pages that share scripts will use their cached versions) and when there's a slow connection.

The affected templates are for the homepage, elections, election search, and landing.

The pages

Screenshots

No visual changes

Related PRs

None

How to test

  • pull the branch
  • ./manage.py runserver
  • Make sure the interactive parts work: (menus, search, widgets, glossary, etc)
  • In Inspector, under the Network tab, change "No throttling" to something else and re-check the pages. They'll load more slowly and may not become useable until the last file is loaded and processed. But they should become interactive just like usual (just a little later than we're expecting).

For the homepage, Lighthouse should no longer mention "eliminate render blocking resources"

Copy link
Member

@patphongs patphongs left a comment

Choose a reason for hiding this comment

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

@rfultz Looks like lighthouse is still flagging the elections.css in the "Eliminate render blocking" report. Is there a way to defer that?

<link rel="stylesheet" type="text/css" href="{% asset_for_css 'elections.css' %}">

Screen Shot 2021-05-17 at 11 22 56 AM

@patphongs
Copy link
Member

@rfultz Looks like lighthouse is still flagging the elections.css in the "Eliminate render blocking" report. Is there a way to defer that?

<link rel="stylesheet" type="text/css" href="{% asset_for_css 'elections.css' %}">

Per @rfultz, this might be addressed in the clean up work in this PR.

Copy link
Member

@patphongs patphongs left a comment

Choose a reason for hiding this comment

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

Looks good!

Copy link
Contributor

@johnnyporkchops johnnyporkchops left a comment

Choose a reason for hiding this comment

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

The cleanURI() function that we use on the homepage to keep the querystring out of the URL does not seem to work if we defer election-lookup.js

It needs jQuery. Specifically, the .on method. I tried to start writing a vanilla JS version, but it gets tricky trying to emulate $.on. Should we replace it with vanilla JS or is there a better way around deferring the needed asset?

FYI- I had to open a new icognito window each time to definitively test removing/replacing defer on the script tag

Also weirdly, election-lookup js was not flagged for Eliminate render-blocking resources by lighthouse when removing defer (?)

Screen Shot 2021-05-17 at 8 43 25 PM

<script src="{% asset_for_js 'election-lookup.js' %}"></script>
<script defer src="{% asset_for_js 'home.js' %}"></script>
<script defer src="{% asset_for_js 'bythenumbers.js' %}"></script>
<!-- <script defer src="{% asset_for_js 'dataviz-common.js' %}"></script> -->
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want to leave this commented tag in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oof. Nope! Good catch!

<script defer src="{% asset_for_js 'home.js' %}"></script>
<script defer src="{% asset_for_js 'bythenumbers.js' %}"></script>
<!-- <script defer src="{% asset_for_js 'dataviz-common.js' %}"></script> -->
<script defer src="{% asset_for_js 'election-lookup.js' %}"></script>
Copy link
Contributor

Choose a reason for hiding this comment

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

The cleanURI() function that we use on the homepage to keep the querystring out of the URL does not work if we defer election-lookup.js

It needs jQuery. Specifically, the .on method. I tried to start writing a vanilla JS version, but it gets tricky trying to emulate $.on. Should we replace it with vanilla JS or is there a better way around this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if we change election-lookup to async?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this to native on another ticket. Should I merge that one into this one?

Copy link
Contributor

Choose a reason for hiding this comment

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

I did try async and got same problem

Copy link
Contributor

@johnnyporkchops johnnyporkchops May 18, 2021

Choose a reason for hiding this comment

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

I changed this to native on another ticket. Should I merge that one into this one?

Whatever fixes it is fine with me

@johnnyporkchops
Copy link
Contributor

johnnyporkchops commented May 18, 2021

Thanks @rfultz .
Now it is removing the querystring on-load, but then if you interact with map, it keeps only state or only the district.

If we ended up deciding that its OK to have querystrings once someone interacts with the map, we would probably want to have it register every parameter they choose

Update: I just realized that this ^^^ might still be a problem because the bar charts below the map also add querystrings to the URL which is confusing--part of the reason we scrub the URL in the first place.

@johnnyporkchops
Copy link
Contributor

johnnyporkchops commented May 19, 2021

@rfultz this works if we put it back on home_page.html inside a window.onload function.

<script>
window.onload = function () {
  $(function() {
    cleanURI()
      function cleanURI(){
        const uri = window.location.toString();
        if (uri.indexOf('?') > 0) {
          window.history.replaceState({}, document.title, location.href.split('?')[0]);
        }
      }
      $('#main').on('change ', 'input, select', function(){
        cleanURI();
      })
      //handle Chrome insconsistency with History API
      $('.js-office').val('S');
      $('.js-election-year').val('2022')
      $('.js-chart-toggle').filter('[value=receipts]').prop('checked', true)
  })
}
</script>
  • Works like this when in home_.page.html
  • If you were to move it to home.js, it loses the race condition intermittently, which I have not seen happening when putting it directly in home_page.html. Let me know what you find if you test it.
  • On some browsers I could remove jQuery's shorthand $(function() wrapper( which is the same as $(document).ready(function() ) and just wrap it in window.onloadbut then Opera does not work. So even though it might feel redundant to have two onloads, I think that is the most ubiquitious coverage.

Let me know what you think

@codecov-commenter
Copy link

Codecov Report

Merging #4604 (46baead) into develop (c4493b1) will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #4604      +/-   ##
===========================================
- Coverage    75.84%   75.83%   -0.02%     
===========================================
  Files          125      125              
  Lines         7660     7660              
  Branches       618      618              
===========================================
- Hits          5810     5809       -1     
- Misses        1850     1851       +1     
Impacted Files Coverage Δ
fec/fec/static/js/modules/calendar.js 92.02% <0.00%> (-0.73%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c4493b1...46baead. Read the comment docs.

@JonellaCulmer JonellaCulmer removed their request for review May 28, 2021 14:10
);
}
window.onload = function () {
$(function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we'd be using the the native onload event to trigger a jQuery function ($(function)) that creates another function (cleanURI), calls it, and calls four other jQuery commands (on, val, val, and filter.prop)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. Not ideal but since it was the only working solution yet, it's worth logging it to the conversation should we need to fall back on it. And only requires adding two lines of code to the existing. Do the latest changes committed since work? Were you able to confirm that before pushing.?

@patphongs
Copy link
Member

We're getting some errors here, so we can't merge this yet:

  1. Webpack error attributed to the defer on the vendor.js.
calc-admin-fines-modal-02db7d5fde06fe303948.js:1 Uncaught ReferenceError: webpackJsonp is not defined
    at calc-admin-fines-modal-02db7d5fde06fe303948.js:1
  1. Updating the district dropdown does not strip the URL. All the other dropdowns do strip the URL as expected.

Copy link
Member

@patphongs patphongs left a comment

Choose a reason for hiding this comment

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

Some things need to be addressed first: #4604 (comment)

@rfultz rfultz changed the title Feature/4172 eliminate render blocking resources [STALLED] Feature/4172 eliminate render blocking resources Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimization: Eliminate render-blocking resources
4 participants