Skip to content

ENH: tree-shake unused AMP CSS custom properties + trim @font-face to woff2 - #14234

Open
hotinglok wants to merge 20 commits into
latestfrom
amp-css-custom-property-treeshake
Open

ENH: tree-shake unused AMP CSS custom properties + trim @font-face to woff2#14234
hotinglok wants to merge 20 commits into
latestfrom
amp-css-custom-property-treeshake

Conversation

@hotinglok

@hotinglok hotinglok commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Builds on #14229. Further reduces the size of the inline <style amp-custom> on AMP article pages to keep well under AMP's 75KB CSS limit. This is also applied to lite pages.

Adds two CSS optimisation passes for the AMP/Lite inline <style> and wires them into _document:

  • treeshakeCssCustomProperties — drops unused :root custom properties (service themes declare full palette/font-token sets, but a page uses only a fraction).
  • trimFontFaceSourcesToWoff2 — Trims @font-face src lists down to their woff2 source only. Service themes ship each face with a woff2 source plus a legacy woff
    fallback, e.g.
src:
  url('…BBCReithSans_W_Bd.woff2') format('woff2'),
  url('…BBCReithSans_W_Bd.woff') format('woff');

Impact and Testing

Against the real news theme (latin, 4 font faces, ~6.1KB theme CSS) with the actual transforms:

Transform Saving
tree-shake custom properties ~4.7–5.0KB
woff2 trim ~0.4KB

Running the following script in browser console to check the ampCustom size:

const ampCustom = document.querySelector('style[amp-custom]').textContent;
console.log('amp-custom size:', ampCustom.length, 'bytes');

On latest (as of 05/08/2026): 61822 bytes
On this branch: 57252 bytes

Tree-shake is the dominant lever: components consume theme colours via compile-time SCSS $ variables (literal hex), so the theme's --brand-*/palette custom properties have effectively no var() consumers in the rendered CSS and are safely removed.

Base automatically changed from smaller-bbc-blocks to latest July 17, 2026 12:10
@hotinglok
hotinglok marked this pull request as ready for review August 5, 2026 12:35
Copilot AI review requested due to automatic review settings August 5, 2026 12:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to further reduce AMP article inline CSS size (to stay under AMP’s 75KB amp-custom limit) by introducing two new CSS post-processing utilities: (1) tree-shaking unused :root custom properties and (2) trimming @font-face src lists to woff2-only.

Changes:

  • Added treeshakeCssCustomProperties utility (+ unit tests + README) to remove unused :root custom properties, including transitive dependency retention.
  • Added trimFontFaceSourcesToWoff2 utility (+ unit tests + README) to keep only woff2 (and local()) in @font-face src.
  • Updated _document.page.tsx to replace the shared optimiseCssPrefixes import with a new inline regex-based implementation.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
ws-nextjs-app/utilities/trimFontFaceSourcesToWoff2/README.md Documents the new woff2-trimming utility.
ws-nextjs-app/utilities/trimFontFaceSourcesToWoff2/index.ts Implements @font-face src trimming logic.
ws-nextjs-app/utilities/trimFontFaceSourcesToWoff2/index.test.ts Adds unit tests for woff2 trimming behavior.
ws-nextjs-app/utilities/treeshakeCssCustomProperties/README.md Documents the new custom-property tree-shaker.
ws-nextjs-app/utilities/treeshakeCssCustomProperties/index.ts Implements :root custom-property tree-shaking with dependency closure.
ws-nextjs-app/utilities/treeshakeCssCustomProperties/index.test.ts Adds unit tests for custom-property tree-shaking behavior.
ws-nextjs-app/pages/_document.page.tsx Inlines a regex-based optimiseCssPrefixes implementation in the document rendering path.

Comment thread ws-nextjs-app/pages/_document.page.tsx Outdated
Comment thread ws-nextjs-app/utilities/treeshakeCssCustomProperties/index.ts
Comment thread ws-nextjs-app/pages/_document.page.tsx Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants