diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-11-14 16:23:11 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-11-15 16:02:26 +0000 |
commit | f59e12ee3fa9c65f178a300954586f6ac91bba79 (patch) | |
tree | e28721f5b356f5b916e59811a192cc96cc32ac4c | |
parent | 865df7790d20ffbeacd8dd10316f4a7f733dcb1d (diff) |
Have front page load minimal JS necessary.
By creating a small file with only what is needed (focus, js flagging),
we can drop loading jQuery and all the application JavaScript on the
front page, prefetching them instead.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | templates/web/base/common_header_tags.html | 5 | ||||
-rw-r--r-- | templates/web/base/common_scripts.html | 14 | ||||
-rw-r--r-- | templates/web/fixmystreet.com/footer_extra_js.html | 2 | ||||
-rw-r--r-- | web/js/front.js | 13 |
5 files changed, 31 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a5224b3fd..9f1d99695 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Front end improvements: - Remove unneeded 2x PNG fallback images. - Individual cobrands can disable social login #1890 + - Improve performance of various pages, especially front. #1903 - Bugfixes - Shortlist menu item always remains a link #1855 - Fix encoded entities in RSS output. #1859 diff --git a/templates/web/base/common_header_tags.html b/templates/web/base/common_header_tags.html index 1dad1cfa0..f65f1e336 100644 --- a/templates/web/base/common_header_tags.html +++ b/templates/web/base/common_header_tags.html @@ -36,7 +36,12 @@ [% IF bodyclass.match('frontpage') %] <link rel="prefetch" href="[% version('/js/validation_rules.js') %]"> + [%~ IF NOT c.user_exists OR NOT (c.user.from_body OR c.user.is_superuser) %] + <link rel="prefetch" href="[% version('/jslib/jquery-1.7.2.min.js') %]"> + [%~ END %] + <link rel="prefetch" href="[% version('/js/jquery.multi-select.js') %]"> <link rel="prefetch" href="[% version('/vendor/jquery.validate.min.js') %]"> + <link rel="prefetch" href="[% version('/cobrands/fixmystreet/fixmystreet.js') %]"> [% END %] [% IF NOT bodyclass.match('mappage') %] [% FOR script IN map_js %] diff --git a/templates/web/base/common_scripts.html b/templates/web/base/common_scripts.html index b04404f06..c6225e8fa 100644 --- a/templates/web/base/common_scripts.html +++ b/templates/web/base/common_scripts.html @@ -7,15 +7,14 @@ scripts = []; scripts.push( start _ "/js/translation_strings." _ lang_code _ ".js?" _ Math.int( date.now / 3600 ), - version('/jslib/jquery-1.7.2.min.js'), - version('/js/jquery.multi-select.js'), - version('/cobrands/fixmystreet/fixmystreet.js'), ); +SET jquery_loaded = 0; SET geolocation_loaded = 0; IF bodyclass.match('frontpage'); SET geolocation_loaded = 1; scripts.push( + version('/js/front.js'), version('/js/geolocation.js'), ); ELSIF bodyclass.match('alertpage'); @@ -24,9 +23,13 @@ ELSIF bodyclass.match('alertpage'); version('/js/geolocation.js'), ); ELSE; + SET jquery_loaded = 1; scripts.push( version('/js/validation_rules.js'), + version('/jslib/jquery-1.7.2.min.js'), + version('/js/jquery.multi-select.js'), version('/vendor/jquery.validate.min.js'), + version('/cobrands/fixmystreet/fixmystreet.js'), ); END; @@ -40,6 +43,11 @@ IF c.user_exists AND (c.user.from_body OR c.user.is_superuser); version('/js/geolocation.js'), ); END; + IF NOT jquery_loaded; + scripts.push( + version('/jslib/jquery-1.7.2.min.js'), + ); + END; scripts.push( version('/cobrands/fixmystreet/staff.js') ); diff --git a/templates/web/fixmystreet.com/footer_extra_js.html b/templates/web/fixmystreet.com/footer_extra_js.html index 2b024bfa4..cb1b78743 100644 --- a/templates/web/fixmystreet.com/footer_extra_js.html +++ b/templates/web/fixmystreet.com/footer_extra_js.html @@ -8,7 +8,7 @@ IF bodyclass.match('frontpage'); scripts.push( version('/cobrands/fixmystreet.com/front.js'), ); -ELSE; +ELSIF NOT bodyclass.match('alertpage'); scripts.push( version('/cobrands/fixmystreet.com/js.js'), ); diff --git a/web/js/front.js b/web/js/front.js new file mode 100644 index 000000000..d2c814490 --- /dev/null +++ b/web/js/front.js @@ -0,0 +1,13 @@ +document.getElementById('pc').focus(); + +(function(){ + var around_forms = document.querySelectorAll('form[action*="around"]'); + for (var i=0; i<around_forms.length; i++) { + var form = around_forms[i]; + var el = document.createElement('input'); + el.type = 'hidden'; + el.name = 'js'; + el.value = 1; + form.insertBefore(el, form.firstChild); + } +})(); |