Drupal - Eliminate render-blocking JavaScript and CSS in above-the-fold content
The README has a guide on how to do this using current 7.x-2.31+ version of AdvAgg. Also see this wiki page on the High performance group. Most sites can achieve a perfect 100/100 score on https://developers.google.com/speed/pagespeed/insights/. Directions on how to achieve it for a fresh of install of AdvAgg below.
Be sure to check the site after every section to make sure the change didn't mess up your site. The changes under AdvAgg Modifier are usually the most problematic but they offer the biggest improvements.
Advanced CSS/JS AggregationGo to admin/config/development/performance/advagg
- Select "Use recommended (optimized) settings"
Install AdvAgg Compress Javascript if not enabled and go to
admin/config/development/performance/advagg/js-compress
- Select JSMin if available; otherwise select JSMin+
- Select Strip everything (smallest files)
- Click the batch compress link to process these files
Install AdvAgg Async Font Loader if not enabled and go to
admin/config/development/performance/advagg/font
- Select Local file included in aggregate (version: X.X.X). If this option is not available follow the directions right below the options on how to install it.
- Check "Use localStorage so the flash of unstyled text (FOUT) only happens once."
- Check "Set a cookie so the flash of unstyled text (FOUT) only happens once."
Install AdvAgg Bundler if not enabled and go to
admin/config/development/performance/advagg/bundler
HTTP/2.0 Settings
- Under "Target Number Of CSS Bundles Per Page" select 25
- Under "Target Number Of JS Bundles Per Page" select 25
- Under "Grouping logic" select "File size"
HTTP/1.1 Settings (default)
- Under "Target Number Of CSS Bundles Per Page" select 2
- Under "Target Number Of JS Bundles Per Page" select 5
- Under "Grouping logic" select "File size"
25 bundles vs 2 and 5 has to do with browser caching. More files equals a better chance of the browser having that combo in its cache, but more files means more connections being established and opened in HTTP 1.1. Use 2 for CSS as this number doesn't wait for any new connections; JS is 5 as most browsers have a concurrent connections limit of 6. This number for bundles was picked after many tests. In HTTP 2.0 there is one streaming connection and the penalty for multiple CSS and JS files is almost non existent; thus optimizing for the browser cache is the best choice; thus 25 should be used for CSS and JS when serving HTTP/2.0.
AdvAgg RelocateInstall AdvAgg Relocate if not enabled and go to
admin/config/development/performance/advagg/relocate
- Select "Use recommended (optimized) settings"
Install AdvAgg Modifier if not enabled and go to
admin/config/development/performance/advagg/mod
- Select "Use recommended (optimized) settings"
Go to https://www.sitelocity.com/critical-path-css-generator and input as many landing pages as needed for critical css; the top 10 is usually a good start. If you have Google Analytics this will show you how to find your top landing pages https://developers.google.com/analytics/devguides/reporting/core/v3/common-queries#top-landing-pages or for Piwik https://piwik.org/faq/how-to/faq_160/. If you don't know what page to start with do your site's homepage. You can also us this to generate css https://chrome.google.com/webstore/detail/critical-style-snapshot/gkoeffcejdhhojognlonafnijfkcepob?hl=en
Example filenames and paths below are for the "bootstrap" theme; they all start with sites/all/themes/bootstrap/critical-css/
; in your theme create the critical-css/
directory. The next directory is based on the user; anonymous/
, all/
, or authenticated/
can be used.
The next directory can be urls/
or type/
. Looking at urls/
; front is a special case for the front page, all other paths use current_path() as the directory and filename with .css
added to the end; See https://api.drupal.org/api/drupal/includes%21path.inc/function/current_path/7.x
Looking at type/
this is a special case for node types. Use the machine name and add .css
to the end. Any node of this type will then have this critical css file applied and inlined. Below are some examples showing how this works.
valid example file locations for the "front" page:
sites/all/themes/bootstrap/critical-css/anonymous/urls/front.css
valid example file locations for "node/1" current_path() page:
sites/all/themes/bootstrap/critical-css/anonymous/urls/node/1.css
valid example file locations for the node type of "page":
sites/all/themes/bootstrap/critical-css/anonymous/type/page.css
If you want some sort of automated way to generate these css files fourkitchens has an excellent article on how to set that up: https://fourword.fourkitchens.com/article/use-grunt-and-advagg-inline-critical-css-drupal-7-theme
You will never get truly high scores with prebuilt drupal modules, the only way to achieve this is by attending and carefully analyzing each the the suggestions made by the google speed tool, addressing each of those independently.
Some things I did to achieve 95 in a very active news site, that by the time I wrote this, scored better than nytimes (Now it doesn't):
- [blocking scripts] Delay execution of third party scripts like sharethis, facebook widgets, google plus, etc, to run only after the page has been fully rendered. This required some simple string replacements to on the output of html.tpl.php to catch those scripts and queue them for later execution.
- [blocking scripts] Store the $scripts variable of html.tpl.php (with json_encode) in a javascript variable to queue them for running after the first page load. This is unnatural, but necessary. Some browser specific issues had to be sorted out.
- [blocking css] Implemented something similar to the Keith Clark's technique, but with rel="prefetch". This introduces the need to solve the FOUC.
- [Minification and compression] Externalize compression and minification to a distribution server where I can use imagemagic, yui-compressor, pngoptim and more stuff to overcome those rules, without turning the drupal install into an unmanageable mess.