Skip to content

Keep plugin stylesheet removal from restyling the whole page - #6149

Closed
ymichael wants to merge 1 commit into
mainfrom
bb/plugin-css-removal-restyle-thr_yvsvx8anai
Closed

ymichael wants to merge 1 commit into
mainfrom
bb/plugin-css-removal-restyle-thr_yvsvx8anai

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

A plugin's stylesheet is removed 1.5 s after its last view closes (apps/app/src/lib/plugin-css.ts, from #2002). In Chrome, removing any stylesheet that contains @layer, which every plugin sheet does, makes the browser re-resolve every CSS @property rule in the document. That restyles every element. The host app's own CSS always has @property rules (95 from Tailwind on main), so every plugin view close restyled the whole page.

How the sheet is removed doesn't matter. Removing the <link>, disabling it, setting media="not all", emptying a <style>, or dropping an adopted sheet all cost the same, about 58 ms at 16,000 elements. What matters is CSS @property anywhere in the document, combined with @layer in the removed sheet. Properties registered from script with CSS.registerProperty() take no part in the cascade, so removing a sheet doesn't re-resolve them.

What changed

  • New apps/app/src/lib/stylesheet-property-registry.ts: moveStylesheetPropertiesToRegistry registers every top-level @property rule in the given sheets with CSS.registerProperty(), using the same name, syntax, inherits and initial value, and deletes the rule from its sheet.
    • A later identical rule is also deleted.
    • A conflicting rule, an unreadable sheet, or a rule the registry rejects is left as it was.
  • apps/app/src/main.tsx calls it once before the first render, when CSS.registerProperty exists, while the DOM is still empty. The @supports fallback layer for browsers without @property stays in the CSS.
  • No change to the loader's lifecycle: plugin sheets are attached, kept through the 1.5 s grace period, and removed exactly as before.

Script registrations take precedence over @property. A plugin sheet that registers one of the host's names with different descriptors can no longer override the host's registration while it is attached. No bundled plugin does this; every bundled registration is identical to the host's.

Depends on #6148 for the plugin-view gains. On main, each plugin sheet carries its own @property rules and redeclares Tailwind theme variables on :root. Removing either restyles the whole page by itself. #6148 removes both from served plugin sheets, and this PR removes the remaining trigger in the host. Each is independent and safe alone.

How you verified

Dev instances on the same 70-thread fixture (about 1,420 elements), headless Chrome 1280×800, Performance-domain RecalcStyleDuration and trace UpdateLayoutTree.elementCount, median of 7.

Removing a plugin sheet from the open app (the same <link> the loader removes):

Sheet main this PR #6148 #6148 + this PR
Automations 1,162 el / 26.0 ms 1,162 / 30.1 1,162 / 29.3 78 / 12.6
Storage & retention 1,162 / 27.7 1,162 / 26.5 1,162 / 30.9 78 / 9.2
Plugin Guide 1,162 / 29.2 1,162 / 28.0 1,162 / 30.5 78 / 14.3
Thread list n/a n/a 1,162 / 25.3 792 / 19.1
Navigation n/a n/a 1,162 / 28.2 130 / 15.9

Opening and closing plugin views (route change, sheet insert or removal, and render; style recalc ms):

View main open / close this PR #6148 #6148 + this PR
Automations 88.7 / 83.1 83.8 / 82.6 66.0 / 82.8 45.0 / 45.6
Storage & retention 74.0 / 83.4 77.9 / 81.0 64.2 / 62.7 41.4 / 42.2
Plugin Guide 214.0 / 81.1 204.9 / 78.4 190.9 / 62.3 158.4 / 42.0

On main, every open and close includes one full-document restyle (largest single recalc 1,154–1,162 elements). With both PRs, the largest is 92–132 elements, except the Plugin Guide's own first render.

Page open (sidebar plus composer): main 71.5 ms / 3,785 elements restyled; this PR alone 54.1 ms / 2,600; #6148 43.6 ms / 1,585; both 44.4 ms / 1,579.

Larger page (16,000 elements, real host CSS and the Automations sheet as served by #6148): Chrome removal goes from 63.9 ms, restyling all 16,006 elements, to 35.6 ms, restyling none. WebKit costs about 62 ms for any sheet change either way, so it is unchanged.

Open, close and reopen of Automations on main, this PR, and both stacked, in Chrome and WebKit:

  • The <link> is present on open and still present 0.4 s after close (grace period). It is gone by 2.6 s, back on reopen, and gone again after the second close.
  • The 619 Automations-scoped rules are active only while the sheet is attached.
  • The plugin element's computed radius and background are identical while open.
  • Unstyled host elements still read the registered initial --tw-shadow: 0 0 #0000.
  • The document's CSS @property count falls by exactly the host's 95, and to 0 on top of Avoid full-page restyles when plugin stylesheets load or unload #6148.

Animations, both PRs stacked compared with main, all identical:

  • Navigation and Thread list popover enter and exit keyframes, sampled at 0 % and 50 %.
  • Spinner, pulse, popover and dialog-enter animations in five plugin scopes.
  • The Automations "Run started" toast transition.
  • Compact-viewport drawer transitions.

Tests and commands:

  • stylesheet-property-registry.test.ts covers four cases: registering and deleting rules, deleting identical duplicates while keeping a conflicting rule, keeping a rule the registry rejects, and skipping unreadable sheets.
  • pnpm exec turbo run lint typecheck test --filter=@bb/app: 6,327 tests passed.
  • pnpm exec turbo run build --filter=@bb/app --filter=@bb/bundled-plugins

AGENT GENERATED

🤖 Generated with Claude Code

In Chrome, removing a stylesheet that contains @layer re-resolves every
@Property rule in the document and restyles every element. Plugin sheets
come and go with their views, so each close restyled the whole page about
1.5 s later. The app now registers its stylesheets' @Property rules with
CSS.registerProperty before the first render and removes them from the
CSS, which leaves nothing for a removal to re-resolve.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael

Copy link
Copy Markdown
Collaborator Author

Folded into #6148, which now includes both stylesheet insertion and removal fixes.

@ymichael ymichael closed this Oct 23, 2026
ymichael added a commit that referenced this pull request Oct 27, 2026
## Human comments

## What was wrong

Plugin stylesheets repeated Tailwind `@property` registrations and
inherited theme variables on `:root`, so inserting a sheet restyled the
entire document. Removing a layered plugin stylesheet also made Chrome
re-resolve the host's CSS `@property` rules and restyle the whole page.
Fixing only insertion or only the host registrations left the other
trigger intact.

Unconditionally stripping plugin CSS on the server also created version
skew: a tab loaded before an update retains its old host CSS while
fetching new plugin CSS. The old host lacks about 50 registrations and
330 theme variables supplied by the expanded host sheet.

## What changed

This combines #6148 and #6149, rebased onto `origin/main` at
`2f61afc227` (#6157). Conflict resolution preserved main's runtime-slot
metadata, retired Git API migration-warning coverage, and this PR's CSS
regression coverage; no CSS behavior changes were needed.

- Generate the complete Tailwind registrations, fallback declarations
and theme values into the host stylesheet. Strip only identical
host-provided rules from plugin CSS; preserve different or unknown
declarations. Plugin build artifacts remain complete and compatible with
older servers.
- Before the first app render, move the host's top-level CSS property
registrations into `CSS.registerProperty()` and delete the equivalent
CSS rules. Keep conflicting rules, rejected registrations and unreadable
sheets. Keep the loader's existing attach/release lifecycle and
1.5-second removal grace period.
- Make stripping opt-in from the app: preload, prefetch and stylesheet
URLs carry `host-tailwind=<hash of generated host CSS>`. Both asset
routes serve the complete original CSS for absent or mismatched markers.
A future host CSS change gets a different marker automatically. Bundle
hashes cover both CSS variants, and compressed response caches
distinguish them.
- Document the request behavior and stale-tab reproduction in
`docs/debugging-and-qa.md`. No daemon wire changes or new CLI/SDK
controls; browser, desktop and mobile use the same app loader.

Script registrations take precedence over CSS `@property`; a plugin's
conflicting registration can no longer override a host registration. No
bundled plugin depends on that behavior. Browsers without
`CSS.registerProperty` retain the CSS rules and fallback layer.

## How you verified

### Combined performance results

The existing PR measurements below used the same 70-thread fixture
(about 1,420 elements), Chrome at 1280×800, Performance-domain style
duration and trace element counts, median of seven. The after column
includes **both** changes.

| Operation | Main | Combined |
| --- | --- | --- |
| Page open (sidebar + composer) | 71.5 ms / 3,785 elements | 44.4 ms /
1,579 elements |
| Automations open / close | 88.7 / 83.1 ms | 45.0 / 45.6 ms |
| Storage & retention open / close | 74.0 / 83.4 ms | 41.4 / 42.2 ms |
| Plugin Guide open / close | 214.0 / 81.1 ms | 158.4 / 42.0 ms |
| Remove Automations stylesheet | 26.0 ms / 1,162 elements | 12.6 ms /
78 elements |
| Remove Storage & retention stylesheet | 27.7 ms / 1,162 elements | 9.2
ms / 78 elements |
| Remove Plugin Guide stylesheet | 29.2 ms / 1,162 elements | 14.3 ms /
78 elements |
| Remove Automations stylesheet, 16,000-element page | 63.9 ms / 16,006
elements | 35.6 ms / 0 elements |

The prior insertion-only measurements were Automations 38.2 → 17.3 ms
(1,234 → 86 elements), Plugin Guide 39.4 → 16.4 ms (1,234 → 86), and
GitHub PR Status 35.7 → 3.5 ms (1,346 → 26). WebKit broadly restyles on
sheet changes and showed no material improvement. Prior combined
verification covered open/close/reopen, popover and dialog animations,
spinners, the Automations toast, and compact drawer transitions in
Chrome and WebKit.

### Stale tab across a server update

Reproduced in real headless Chrome 155 at 1280×800 using main
`efe987702b87ca4bfe9df905b4b63ede478e12c7`, then switching the same
isolated server to the combined branch and opening Automations and
Plugin Guide through their navigation buttons.

**Test condition:** normal `AppUpdateHost` revision detection
automatically reloads the tab. To exercise the requested client that has
not received that update status yet, a local proxy held
`/api/v1/system/app-update` at main's actual response and retained
main's immutable app assets. All plugin metadata, JS and CSS came from
the real switched server. No app CSS or plugin styles were injected into
the tab. The stale tabs' `performance.timeOrigin` and
`index-BR1pQoni.css` host URL stayed unchanged across the server switch.

| View / observation | Unconditionally stripped server | Opt-in server,
old tab | Opt-in server, fresh tab |
| --- | --- | --- | --- |
| Automations content | Matches fresh load | Matches fresh load, zero
changed content pixels | Reference |
| Plugin Guide heading's italic “bb” | **600**, missing
`--font-weight-bold`; fresh load is **700** | **700**, variable present
| **700** |
| Guide stylesheet | Stripped despite old host | Complete, 56
`@property` rules and theme layer | Stripped, 0 `@property` rules and no
`:root` |

Before the fix, the Guide heading differed by 440 screenshot pixels.
After the fix its heading matches, and the Guide's computed styles and
element geometry match across the old and fresh tabs; tiny rasterization
differences remain in scaled fixture strokes. Screenshots and
observations are retained in the task evidence directory
(`/tmp/bb-6148-evidence/`).

Old-tab timestamps: before case `1792785386806`, after case
`1792785387394.9`; both retained main's CSS. The fresh app adds
`host-tailwind=90105b453d213b90e1c3f988b900bdc9fbb0a5f2677466f83df3227db69320f9`;
the old app sends no marker.

### Regression coverage and checks

- Extended the existing real asset-route test. An unmarked request
failed on the unpatched branch because the response omitted the original
registrations/theme. It now passes. Both hash and plugin-ID routes cover
matching, absent and mismatched markers, interleaved gzip requests, and
immutable caching.
- Existing frontend lifecycle tests now verify the capability URL
through preload, prefetch, attach, replacement and grace-period
handling. Property-registry coverage from #6149 is included.
- `pnpm exec turbo run lint typecheck --filter=@bb/app
--filter=@bb/server --filter=@bb/plugin-build`: passed.
- Focused Turbo app tests: **46 passed**. Focused Turbo server asset
tests: **13 passed**.
- Production `pnpm start:worktree` build: passed, including app, server,
host daemon, SDK and bundled plugins.
- `pnpm exec turbo run test --filter=@bb/plugin-build`: **235 passed, 1
skipped**.
- [Full GitHub
CI](https://github.com/get-bb/bb/actions/runs/40545551643) passed on
rebased head `f501ca183c4c7fd0e6960c13169e2f817f5a47ba`: **47 successful
checks, 4 conditional skips, zero failures or pending checks**. Skips:
iOS, Android, Node Compatibility smoke, and Windows dependency warming.
All executed checks passed on their first attempt. Local affected
lint/typechecks, 46 app tests, 13 server asset tests, and 235
plugin-build tests also passed (1 plugin-build test skipped).

> AGENT GENERATED

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant