Improving Jekyll generation speed for AMP pages

2017-11-23 3 min read

    Last September I migrated my blog over to AMP which entailed a variety of challenges ranging from converting every img tag to an amp-img tag with some additional metadata to figuring out how to support Disqus. I tackled the critical ones but the one I never got to was speeding up the build time since it had no impact on the actual reader experience and just slowed down my build and commit process. During this Thanksgiving break I finally decided to do something about it after discovering that jekyll has a profiling feature. It’s expected that the bulk of the time is spent generating the post pages but running the profiler highlighted that the majority of the work wasn’t in the actual content block but in generating the head element - something that shold similar from page to page.

    FilenameCountBytesTime
    _layouts/default.html57016663.07K230.544
    _includes/themes/amp/default.html57016658.06K229.425
    _includes/head.html57011934.62K228.908
    _layouts/post.html5623595.71K5.542
    _includes/themes/amp/post.html5623590.77K4.640
    _includes/metadata.json570461.90K3.392
    _includes/setup218114.91K2.345
    _includes/tags_list55973.99K0.874
    _includes/footer.html5701167.83K0.256
    sitemap.xml162.20K0.245
    _includes/styles.scss5705736.18K0.115
    archive.html1120.81K0.114
    _includes/posts_collate1120.80K0.105
    atom.xml11739.08K0.063
    tags.html1108.78K0.056
    _includes/header.html570709.16K0.056
    _includes/pages_list2103.64K0.047
    index.md1106.79K0.020
    _layouts/page.html6246.62K0.010
    sitemap.txt134.99K0.010
    done in 245.117 seconds.

    After taking a quick look at the head.html file it became clear what the problem was: AMP requires the CSS to be inline which combined with my usage of SCSS which needed to be transpiled to CSS on every page. Unsurprisingly, this was an expensive operation and the solution was to somehow get rid of it. While we can’t avoid inlining the CSS what we can do is transpile the SCSS to CSS once and include the contents in every post. Even better is to read the contents of the resulting CSS file once and then just inject it whenever necessary via the jekyll-include-cache extension. Applying these tactics gets the blog generation time down to 15 seconds from over 4 minutes. It does require the CSS file to be regenerated every time the SCSS file changes but given the infrequency it’s not a huge issue. This doesn’t make it any quicker to write posts but it does feel good to get such a big improvement in blog generation speed.

    FilenameCountBytesTime
    _layouts/default.html57016663.10K4.277
    _layouts/post.html5623595.71K4.104
    _includes/themes/amp/default.html57016658.09K3.984
    _includes/head.html57011934.64K3.607
    _includes/themes/amp/post.html5623590.78K3.306
    _includes/metadata.json570461.92K2.420
    _includes/setup161211.02K1.367
    _includes/tags_list55973.99K0.700
    sitemap.xml162.20K0.251
    archive.html1120.81K0.119
    _includes/posts_collate1120.80K0.111
    atom.xml11739.13K0.065
    tags.html1108.78K0.064
    _includes/pages_list2103.64K0.050
    _includes/header.html570709.16K0.041
    index.md1106.79K0.019
    sitemap.txt134.99K0.011
    _layouts/page.html6246.62K0.008
    done in 15.077 seconds.