Skip to content

web: share one brand CSS style sheet across elements - #27362

Merged
dominic-r merged 2 commits into
mainfrom
dominic/web-share-brand-css-sheet
Nov 2, 2026
Merged

dominic-r merged 2 commits into
mainfrom
dominic/web-share-brand-css-sheet

Conversation

@dominic-r

Copy link
Copy Markdown
Member

Details

What does this PR change?

AKElement now builds the brand's custom CSS into a CSSStyleSheet once per page, and every element and nested shadow root adopts that same sheet. The sheet lives in a small module-level cache in web/src/elements/Base.ts, keyed on the CSS text. If the server context is re-read with a different brand (injectBrand does this in tests), the next element gets a new sheet.

The other createStyleSheetUnsafe callers ($PFBase, $AKBase, the hostStyles setter) run once per module or class, not per element, so they stay as they are.

Why is this change needed?

The AKElement constructor called createStyleSheetUnsafe(brand.brandingCustomCss) for every instance. createCSSResult wraps the string in a new strings array on each call, and Lit only reuses a sheet for the same array object, so each call ended in new CSSStyleSheet().replaceSync(...). A page with N elements parsed the brand CSS N times and kept N copies. This came in with #14331, and #19892 extended it to nested shadow roots. Brands whose custom CSS inlines a base64 font pay for that parse on every element.

I measured it in headless Chromium by mounting 200 elements with about 1 MB of brand CSS. Before this change that took 200 parses into 200 distinct sheets and 359 ms. After it, the same mount takes 1 parse, 1 sheet and 15 ms.

The adopted sheets and their order don't change, and old browsers without adoptedStyleSheets take the same fallback path as before.

How was this tested?

I added web/src/elements/Base.browser.test.ts. One test checks that two elements, and the element nested in each one's shadow root, all adopt the same CSSStyleSheet object. The other checks that changing the brand CSS gives new elements a new sheet. I ran only that file in Chromium. It passes with the fix, and the sharing test fails when the constructor goes back to calling createStyleSheetUnsafe directly. I also linted and format-checked the two touched files and type-checked the web project.

I did not run the full web test suite, make web, a production build, or the e2e and visual tests, and I did not check a page by hand.

Linked issues

None. I searched and found no tracking issue.


Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)
  • I have read the AI usage policy.

Every AKElement built its own CSSStyleSheet from the brand's custom CSS,
so a page with N elements parsed the brand CSS N times and held N
copies. Lit's sheet cache never hit because createCSSResult wraps the
string in a new strings array each call. This hurts most when the brand
CSS inlines a font or image.

Build the sheet once and have every element and nested shadow root
adopt it. The cache is keyed on the CSS text so a re-read server
context, as injectBrand does in tests, still gets a matching sheet.
@dominic-r dominic-r added area:frontend Features or issues related to the browser, TypeScript, Node.js, etc backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8 labels Oct 30, 2026
@dominic-r
dominic-r requested a review from a team as a code owner October 30, 2026 03:25
@dominic-r dominic-r self-assigned this Oct 30, 2026
@netlify

netlify Bot commented Oct 30, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit 99d9256
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6ae40e42fa852502cad0a3d2
😎 Deploy Preview https://deploy-preview-27362--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.79%. Comparing base (ef6c7aa) to head (f8ecc62).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #27362   +/-   ##
=======================================
  Coverage   92.79%   92.79%           
=======================================
  Files        1207     1207           
  Lines       82839    82839           
  Branches     4686     4686           
=======================================
+ Hits        76867    76868    +1     
+ Misses       5896     5895    -1     
  Partials       76       76           
Flag Coverage Δ
conformance 30.81% <ø> (+<0.01%) ⬆️
e2e 35.61% <ø> (+<0.01%) ⬆️
integration 28.30% <ø> (+0.01%) ⬆️
rust 49.31% <ø> (ø)
unit 94.34% <ø> (+<0.01%) ⬆️
unit-migrate 94.34% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Playwright e2e

Download this run's HTML report and traces, then open the report:

gh run download 41496698623 -D playwright-artifacts
npx playwright show-report playwright-artifacts/playwright-report

Installation instructions

Instructions for docker-compose

Add the following block to your .env file:

AUTHENTIK_IMAGE=ghcr.io/goauthentik/dev-server
AUTHENTIK_TAG=gh-f8ecc62933d4d5d654b8fb180aecc8639d8b5457
# Distroless image: AUTHENTIK_TAG=gh-f8ecc62933d4d5d654b8fb180aecc8639d8b5457-distroless
AUTHENTIK_OUTPOSTS__CONTAINER_IMAGE_BASE=ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s

Afterwards, run the upgrade commands from the latest release notes.

Instructions for Kubernetes

Add the following block to your values.yml file:

authentik:
    outposts:
        container_image_base: ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
global:
    image:
        repository: ghcr.io/goauthentik/dev-server
        tag: gh-f8ecc62933d4d5d654b8fb180aecc8639d8b5457
        # Distroless image: tag: gh-f8ecc62933d4d5d654b8fb180aecc8639d8b5457-distroless

Afterwards, run the upgrade commands from the latest release notes.

@dominic-r
dominic-r enabled auto-merge (squash) November 2, 2026 18:02
@dominic-r
dominic-r merged commit 4f14fc5 into main Nov 2, 2026
170 checks passed
@dominic-r
dominic-r deleted the dominic/web-share-brand-css-sheet branch November 2, 2026 18:22
@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

🍒 Cherry-pick to version-2026.8 created: #27454

dominic-r added a commit that referenced this pull request Nov 23, 2026
 to version-2026.8) (#27454)

* web: share one brand CSS style sheet across elements (#27362)

Every AKElement built its own CSSStyleSheet from the brand's custom CSS,
so a page with N elements parsed the brand CSS N times and held N
copies. Lit's sheet cache never hit because createCSSResult wraps the
string in a new strings array each call. This hurts most when the brand
CSS inlines a font or image.

Build the sheet once and have every element and nested shadow root
adopt it. The cache is keyed on the CSS text so a re-read server
context, as injectBrand does in tests, still gets a matching sheet.

* web: set the brand through window.authentik in the brand CSS test

version-2026.8 has no #common/brand or injectBrand, so the test
imported a module that does not exist on this branch. Write the
server-rendered JSON to window.authentik instead, which globalAK reads
and converts on this branch.

---------

Co-authored-by: Dominic Roy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:frontend Features or issues related to the browser, TypeScript, Node.js, etc backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants