Files
notifications-admin/app/asset_fingerprinter.py
Chris Hill-Scott ea124f2886 Tell browsers to preload fonts
When looking at Google’s PageSpeed Insights tool as part of the
compression work I noticed a suggestion that we preload our font files.
The tool suggests this should save about 300ms on first page load time.

***

Our font files are referenced from our CSS. This means that the browser
has to download and parse the CSS before it knows where to find the font
files. This means the requests happen in sequence.

We can make the requests happen in parallel by using a `<link>` tag with
`rel=preload`. This tells the browser to start downloading the fonts
before it’s even started downloading the CSS (the CSS will be the next
thing to start downloading, since it’s the next `<link>` element in the
head of the HTML).

Downloading fonts before things like images is important because once
the font is downloaded it causes the layout to repaint, and shift
everything around. So the page doesn’t feel stable until after the fonts
have loaded.

Google call this [cumulative layout shift](https://web.dev/cls/) which
is a score for how much the page moves around. A lower score means a
better experience (and, less importantly for us, means the page might
rank higher in search results)

We’re only preloading the WOFF2 fonts because only modern browsers
support preload, and these browsers also all support WOFF2.

We set an empty `crossorigin` attribute (which means anonymous-mode)
because the preload request needs to match the origin’s CORS mode. See
https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content#CORS-enabled_fetches
for more details.

We set `as=font` because this helps the browser use the correct content
security policy, and prioritise which requests to make first.
2020-12-29 16:31:11 +00:00

51 lines
1.5 KiB
Python

import hashlib
class AssetFingerprinter(object):
"""
Get a unique hash for an asset file, so that it doesn't stay cached
when it changes
Usage:
in the application
template_data.asset_fingerprinter = AssetFingerprinter()
where template data is how you pass variables to every template.
in template.html:
{{ asset_fingerprinter.get_url('stylesheets/application.css') }}
* 'app/static' is assumed to be the root for all asset files
"""
def __init__(self, asset_root='/static/', filesystem_path='app/static/'):
self._cache = {}
self._asset_root = asset_root
self._filesystem_path = filesystem_path
def get_url(self, asset_path, with_querystring_hash=True):
if not with_querystring_hash:
return self._asset_root + asset_path
if asset_path not in self._cache:
self._cache[asset_path] = (
self._asset_root +
asset_path +
'?' +
self.get_asset_fingerprint(self._filesystem_path + asset_path)
)
return self._cache[asset_path]
def get_asset_fingerprint(self, asset_file_path):
return hashlib.md5(
self.get_asset_file_contents(asset_file_path)
).hexdigest()
def get_asset_file_contents(self, asset_file_path):
with open(asset_file_path, 'rb') as asset_file:
contents = asset_file.read()
return contents
asset_fingerprinter = AssetFingerprinter()