refactor(css): split style.css into ordered fragments

static/style.css was 47,530 lines. It is now nine files under static/css/:
tokens.css holds the :root custom properties, light theme and density classes,
and style-part-01..08 carry the rest in their original order. Nothing was
rewritten; every line moved verbatim and the <link> order in index.html
reproduces the original file byte for byte.

Cut points are brace-depth zero and outside block comments, so no rule or
comment is split. All 47,531 source lines are accounted for across the
fragments.

The computed-style baseline recorded before the split is reproduced exactly,
which is the evidence that the cascade is unchanged rather than an argument
that it should be.

Three things the split broke and this fixes:

- The harness self-test swapped two conflicting .attach-strip blocks inside
  style.css to prove the digest is order-sensitive. It rewrote one hardcoded
  URL, so with the file gone it silently measured an unmodified page. It now
  searches every stylesheet, rewrites whichever holds the pair, and fails
  loudly if none does.
- Four tests asserted the cache-bust token by matching /static/style.css?v=.
  They check the real invariant now, that every app stylesheet shares one
  token with app.js, through stylesheet_cache_version().
- The three panel stylesheets carried their own tokens, so a browser could
  hold half an old cascade and half a new one. All app stylesheets now bust
  together.
This commit is contained in:
Léo
2026-09-29 17:37:25 +02:00
parent 1323fab0bd
commit 03d55dfe3c
19 changed files with 47709 additions and 47556 deletions
+23 -2
View File
@@ -55,7 +55,10 @@ function swapRuleOccurrences(css, selector) {
}
const matches = blocks.filter(b => b.selector === selector && b.end !== undefined);
if (matches.length < 2) {
throw new Error(`swap-rule: need two top-level blocks for "${selector}", found ${matches.length}`);
// The selector is not in this sheet, or appears once. The stylesheet is
// split across several files, so that is expected for most of them: the
// caller decides whether any sheet matched at all.
return null;
}
const [a, b] = matches;
const textA = css.slice(a.start, a.end);
@@ -186,6 +189,7 @@ async function main() {
const missing = {};
try {
let swapped = 0;
for (const page of job.pages) {
snapshot[page.name] = {};
let shippedStylesheets = null;
@@ -217,9 +221,23 @@ async function main() {
});
if (job.swapRule) {
// The cascade is spread over several files, so find the one that
// actually holds two top-level blocks of the selector and rewrite
// only that one. Every other sheet passes through untouched.
await tab.route('**/static/**/*.css*', async route => {
const response = await route.fetch();
const original = await response.text();
const body = swapRuleOccurrences(original, job.swapRule);
if (body === null) return route.fulfill({ response, body: original });
swapped += 1;
await route.fulfill({ response, body, headers: { ...response.headers(), 'content-type': 'text/css; charset=utf-8' } });
});
await tab.route('**/static/style.css*', async route => {
const response = await route.fetch();
const body = swapRuleOccurrences(await response.text(), job.swapRule);
const original = await response.text();
const body = swapRuleOccurrences(original, job.swapRule);
if (body === null) return route.fulfill({ response, body: original });
swapped += 1;
await route.fulfill({ response, body, headers: { ...response.headers(), 'content-type': 'text/css; charset=utf-8' } });
});
}
@@ -253,6 +271,9 @@ async function main() {
await context.close();
}
}
if (job.swapRule && swapped === 0) {
throw new Error(`swap-rule: no stylesheet had two top-level blocks for "${job.swapRule}"`);
}
} finally {
await browser.close();
}
+22
View File
@@ -61,3 +61,25 @@ def stylesheet_link_tags() -> str:
def app_css() -> str:
"""The whole cascade as one string, in load order."""
return "\n".join(p.read_text(encoding="utf-8") for p in stylesheet_paths())
def stylesheet_cache_version() -> str:
"""The single ``?v=`` token every app stylesheet link carries.
The stylesheet is split across several files that must be busted together:
shipping one fragment under a stale token serves a browser half of an old
cascade and half of a new one. Tests that used to read the version off
``style.css`` ask for it here instead, so they keep checking the invariant
rather than a filename.
"""
versions = set()
for url in stylesheet_urls():
m = re.search(r"\?v=([^&]+)$", url)
if not m:
raise AssertionError(f"app stylesheet has no cache-bust token: {url}")
versions.add(m.group(1))
if len(versions) != 1:
raise AssertionError(
f"app stylesheets disagree on their cache-bust token: {sorted(versions)}"
)
return versions.pop()
+2 -3
View File
@@ -1,6 +1,6 @@
from pathlib import Path
import re
from tests.helpers.stylesheets import app_css
from tests.helpers.stylesheets import app_css, stylesheet_cache_version
ROOT = Path(__file__).resolve().parent.parent
@@ -65,9 +65,8 @@ def test_compare_cache_key_bumped_for_shuffle_notice():
index = _read("static/js/compare/index.js")
app_versions = re.findall(r"/static/app\.js\?v=([A-Za-z0-9_-]+)", html)
style_version = re.search(r"/static/style\.css\?v=([A-Za-z0-9_-]+)", html)
assert app_versions and len(set(app_versions)) == 1
assert style_version and style_version.group(1) == app_versions[0]
assert stylesheet_cache_version() == app_versions[0]
assert re.search(r"compare/index\.js\?v=[A-Za-z0-9_-]+", app)
assert re.search(r"vote\.js\?v=[A-Za-z0-9_-]+", index)
assert re.search(r"panes\.js\?v=[A-Za-z0-9_-]+", index)
+2 -2
View File
@@ -1,7 +1,7 @@
from pathlib import Path
import re
from tests.helpers.stylesheets import app_css
from tests.helpers.stylesheets import app_css, stylesheet_cache_version
ROOT = Path(__file__).resolve().parent.parent
@@ -49,7 +49,7 @@ def test_gallery_static_cache_key_bumped():
app = _read("static/app.js")
service_worker = _read("static/sw.js")
assert re.search(r"/static/style\.css\?v=[A-Za-z0-9_-]+", html)
assert stylesheet_cache_version()
html_version = re.search(r"/static/js/gallery\.js\?v=([A-Za-z0-9_-]+)", html)
app_version = re.search(r"gallery\.js\?v=([A-Za-z0-9_-]+)", app)
sw_version = re.search(r"/static/js/gallery\.js\?v=([A-Za-z0-9_-]+)", service_worker)
@@ -1,7 +1,7 @@
from pathlib import Path
import re
from tests.helpers.stylesheets import app_css
from tests.helpers.stylesheets import app_css, stylesheet_cache_version
ROOT = Path(__file__).resolve().parent.parent
@@ -54,9 +54,8 @@ def test_notes_mobile_checklist_asset_versions_are_bumped():
assert re.search(r"notes\.js\?v=[A-Za-z0-9_-]+", app)
app_versions = re.findall(r"/static/app\.js\?v=([A-Za-z0-9_-]+)", html)
style_version = re.search(r"/static/style\.css\?v=([A-Za-z0-9_-]+)", html)
assert app_versions and len(set(app_versions)) == 1
assert style_version and style_version.group(1) == app_versions[0]
assert stylesheet_cache_version() == app_versions[0]
def test_drawing_edits_mark_notes_dirty_and_keep_one_gallery_image():
+2 -3
View File
@@ -3,7 +3,7 @@
from pathlib import Path
import re
from tests.helpers.stylesheets import app_css
from tests.helpers.stylesheets import app_css, stylesheet_cache_version
ROOT = Path(__file__).resolve().parent.parent
@@ -78,6 +78,5 @@ def test_research_panel_uses_one_versioned_module_instance():
panel_version = re.search(r"research/panel\.js\?v=([^'\"]+)", app)
asset_version = re.search(r"/static/app\.js\?v=([^'\"]+)", index)
style_version = re.search(r"/static/style\.css\?v=([^'\"]+)", index)
assert panel_version and renderer.count(f"research/panel.js?v={panel_version.group(1)}") == 2
assert asset_version and style_version and asset_version.group(1) == style_version.group(1)
assert asset_version and asset_version.group(1) == stylesheet_cache_version()