Fast pages
How to make a GGDS website load as fast as it can, on any phone and any connection: four steps, in the order that saves the most.
GGDS is small to begin with: all of its CSS is about 8 KB compressed, its optional script under 2 KB, and it uses the fonts already on people's phones, so there is no font to download. These steps make a website faster still. Each works on its own; do the first two on every website.
1. Let browsers keep the files
A file the browser already has costs nothing. Put each GGDS version in its own folder, such as /ggds/0.5.0/, and tell browsers they may keep everything in it for a year:
Cache-Control: public, max-age=31536000, immutableTo update GGDS, add the new version's folder beside the old one and change the links. Never change the files inside a version's folder: browsers that kept the old ones would not see the change. From npm or a CDN the version is already in the address, so this is done for you.
| Server | Setting |
|---|---|
| nginx | location /ggds/ { add_header Cache-Control "public, max-age=31536000, immutable"; } |
| Apache | <Location /ggds/> Header set Cache-Control "public, max-age=31536000, immutable" </Location> |
| IIS | <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" /> in the folder's web.config |
2. Send the files compressed
Compression makes the CSS about a sixth of its size. In the download, every CSS, JavaScript, SVG and JSON file also comes ready-compressed, as a .br (Brotli) and a .gz (gzip) file made at the strongest setting, so the server does no work for it: switch on gzip_static on; in nginx (and brotli_static on; with the Brotli module), or file_server { precompressed br gzip } in Caddy. On other servers, switch on their own compression for CSS, JavaScript and SVG.
3. Load only the components and icons you use
ggds.min.css holds every component. A website that uses a few of them can load one file holding only those, with the same result: the pages look exactly the same, because each component keeps its place in the cascade layers. The command finds the components from the ggds- classes in your website's own files:
# from npm
npx --package=@ggds/css ggds-css-subset --scan templates --out static/ggds/site.min.css
# from the download
node ggds/0.5.0/subset.mjs --scan templates --out ggds/0.5.0/site.min.css
# or name the components
npx --package=@ggds/css ggds-css-subset header footer button field error-summary --out static/ggds/site.min.cssA form page with the parts every page needs (skip link, banner, header, footer, tab bar) and buttons, fields and an error summary loads about half the CSS, compressed. A website that uses most components saves less: the agency website starter uses 21 of the 27 and saves about an eighth. Add --no-tokens when the website loads @ggds/tokens separately. Run the command again whenever the website starts using another component, or put it in the website's build.
Icons work the same way. The full sprite holds all 143 icons; this makes one with only the icons your files name:
npx --package=@ggds/icons ggds-icons-subset --scan templates --out static/ggds/icons.svg
# from the download: node ggds/0.5.0/icons/subset.mjs --scan templates --out ggds/0.5.0/icons/site.svgThen use icons.svg#search where you used sprite.svg#search. An icon name your website builds while it runs (such as arrow-${direction}) cannot be found by scanning: name those icons on the command line too. With a JavaScript bundler, import single icons from @ggds/icons instead; the bundler keeps only those.
A React website has no ggds- classes in its own files: scan the built pages (--scan dist) or name the components.
4. Make your images small
On most government websites, photographs, banners and PDFs weigh far more than GGDS. Save photographs as AVIF or WebP, no wider than they are shown, and give every <img> its width and height so the page does not move while it loads. Add loading="lazy" to images further down the page. Serve a JPEG to older browsers with <picture>:
<picture>
<source srcset="minister.avif" type="image/avif">
<source srcset="minister.webp" type="image/webp">
<img src="minister.jpg" alt="The Minister at the launch" width="640" height="400" loading="lazy" decoding="async">
</picture><picture>
<source srcSet="minister.avif" type="image/avif" />
<source srcSet="minister.webp" type="image/webp" />
<img src="minister.jpg" alt="The Minister at the launch" width="640" height="400" loading="lazy" decoding="async" />
</picture>Check your website
npx @ggds/check https://www.example.gov.gh reports what a live website still needs: the page parts, accessibility, security headers, its email domain and speed, with the source of every rule. npx @ggds/check ./dist checks the built pages before they go live. It is a readiness check, not a statement of NITA compliance. See @ggds/check.
The script
The optional script loads as a module, so it never holds up the page, and every component works before it has loaded and without it. Load it only on pages that use the header menu or the error summary if you want to save the request elsewhere.
Something wrong, unclear or missing? or suggest a change to it on GitHub. You need a GitHub account.