Repository navigation
web: share one brand CSS style sheet across elements - #27362
Conversation
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.
✅ Deploy Preview for authentik-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Playwright e2eDownload 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-reportInstallation instructionsInstructions for docker-composeAdd the following block to your 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)sAfterwards, run the upgrade commands from the latest release notes. Instructions for KubernetesAdd the following block to your 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-distrolessAfterwards, run the upgrade commands from the latest release notes. |
|
🍒 Cherry-pick to |
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]>
Details
What does this PR change?
AKElementnow builds the brand's custom CSS into aCSSStyleSheetonce per page, and every element and nested shadow root adopts that same sheet. The sheet lives in a small module-level cache inweb/src/elements/Base.ts, keyed on the CSS text. If the server context is re-read with a different brand (injectBranddoes this in tests), the next element gets a new sheet.The other
createStyleSheetUnsafecallers ($PFBase,$AKBase, thehostStylessetter) run once per module or class, not per element, so they stay as they are.Why is this change needed?
The
AKElementconstructor calledcreateStyleSheetUnsafe(brand.brandingCustomCss)for every instance.createCSSResultwraps 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 innew 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
adoptedStyleSheetstake 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 sameCSSStyleSheetobject. 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 callingcreateStyleSheetUnsafedirectly. 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
make all)make docs)