Proposal: Do we still need to minify our CSS?

Hi!

XWIKI-19937 asks to replace YUI Compressor, which still minifies all our CSS years after JavaScript moved to the Closure Compiler (XWIKI-13775). While choosing a replacement, I realized: do we still need CSS minification at all?

History of CSS minification in XS

CSS minification was never decided on its own. It came with JavaScript minification in 2008-2009:

  • the goal was improving page load time, when Albatross loaded about 20 JS and CSS files over HTTP/1.1 and gzip was left to the admin’s Apache config (devs list, 2007).
  • YUI was chosen because “there is nothing else that does variable minimization” (devs list, 2008), a JavaScript-only argument.
  • CSS was added as “it [would] also be nice to minify albatross / toucan css files” (XWIKI-1701, done in XWIKI-3958).

Over the years, it created a few things we now have to maintain:

  • The debugging machinery. Minified sources are hard to debug, which led to the ?minify=false parameter (2013), the debug.minify configuration (2015), the .min.css file pairs and their lookup in getSkinFile (2020), and ~20 hard-coded .min.css references.
  • Bugs. YUI’s last release is from 2013, and the yuicompressor-maven-plugin repository is archived. It breaks 13 recent CSS features (evaluation):
    • calc() with + (XWIKI-21246, 2023, reported again as XWIKI-23906, 2026);
    • SVG data URLs (XWIKI-22206, 2024);
    • 0s, keyframe 0%, @container, @scope, @supports not, relative colors…
  • Live bugs: font-size: 1.0em becomes the invalid font-size:1.0 in the notifications macro and the tag cloud, and & :hover becomes &:hover in Solr search.
  • Workarounds:
    • .001s instead of 0s in the tour (2026);
    • an implicit transition duration in the CKEditor content sheet (2026);
    • a /*! comment around the Velocity code of style.css and cssVariablesInit.css. A side effect is that these two files have never been minified.

What minification saves today

Measured with a modern minifier on all the CSS of XWiki Standard, after Velocity:

  • 23 KB gzipped across our 140 style sheets: 13.6 KB for the build-time files and 8.5 KB for the skin extensions. A page loads only some of them: on the design.xwiki.org home page, minification saves about 6 KB gzipped on a cold cache.
  • Skin files and webjars are cacheable for 30 days with versioned URLs, so a browser pays this once per upgrade.
  • With compression, even on slow 4G and over HTTP/2 or HTTP/3, that’s about a 30 ms gain on a first visit after an upgrade, and no gain as soon as the CSS is cached.
  • Most production setups compress responses at the proxy, as our admin guide recommends. Without compression the gain is about 3 times larger.
  • Our biggest sheet, style.css (249 KB, 37 KB gzipped, a third of all our CSS), is never minified (because of the workaround described above), and it wasn’t considered an issue.

Options

Both options I’ll consider do remove YUI Compressor, which means at least:

  • The yuicompressor-maven-plugin goes away from the xwiki-commons plugin management, the webjar lifecycle and the two platform modules.
  • The three CSS bundles are produced by a plain concatenation and lose their .min. We can use the maven-antrun-plugin with <concat> for this, similarly to what we do for legacy components.
    • This keeps the number of requests unchanged. YUI did this bundling, and historically it was a bigger win than the minification itself. Each extra request waits for a free connection on HTTP/1.1, so serving the 5 files of xwiki.bundle.css separately would cost about 300 ms more on slow 4G. Over HTTP/2 or HTTP/3, the requests share a single connection and the difference drops to 0 to 4 ms. A plain concatenation is cheap and simple anyway, so we can keep the bundles.
  • The ~20 hard-coded .min.css references are updated to target the actual source instead of the .min. For backward compatibility, a request for a foo.min.css that doesn’t exist serves foo.css, with a deprecation warning.

JavaScript minification is not impacted by these changes.

Half of our build-time CSS files are Velocity templates, which a real CSS parser can’t read. So if we keep minifying, it would only make sense to have it happen at runtime, after Velocity. The best engine I found for this is css4j:

  • 2 broken features against 13 for YUI, with a fallback to the source itself on a parse error;
  • 5 to 9 ms for all the XS skin extensions, against 42 to 54 ms for YUI;
  • around the same gain: 27% gzipped, against 28%;
  • already a platform dependency, and maintained.

Here are the two options I ended up with:

  1. Stop minifying CSS altogether. Skin extensions are served as written, and the admin guide states that compression is what keeps CSS small. We increase the typical CSS bytes by about 10% on a cold cache (CSS is about 10% of a cold page load, so under 1% of the page weight, measured on the design.xwiki.org home page), and contrib extensions using the webjar packaging lose their CSS minification too.
  2. Minify at runtime with css4j, on skin extensions, skin files and evaluated webjars. It keeps the ~6 KB gain per page, but needs new code in SkinAction and the WebJars handler. That code runs only on a cache miss, and costs less than YUI on skin extensions today.

Alternatives I rejected:

  • Another build-time minifier: only a custom token-based tool could actually read Velocity source and be useful.
  • ph-css: more bugs than css4j, and it refuses 7 recent features (clamp(), if()…).
  • Keeping YUI: the bugs stay, and the workarounds keep piling up as we convert LESS to CSS.

Opinion

I prefer option 1: about 6 KB gzipped per page on a cold cache isn’t worth a minifier, the workarounds and the .min machinery IMO. It simplifies the codebase instead of complexifying it, with a minimal page weight regression.

Conclusion

  • Do we still need CSS minification? Should we stop minifying CSS (option 1) or switch to css4j (option 2)?
  • Are you OK with dropping the .min.css names, with a deprecated fallback for existing references?

Thank you in advance for your help on the topic!
Lucas C.

I’m undecided here. While it is true that minification obviously doesn’t bring much for our current files, this could easily change, e.g., if we started adding a lot more comments in CSS code. Regarding css4j, I’m wondering if the gain shouldn’t be larger here, as from what I understand, we could now also minify style.css. Also, the gain shouldn’t just be transfer times but also parse times. Regarding Velocity, as we’re working towards eliminating Velocity from our CSS code, this should also only be an issue for a few generated files (for defining CSS variables from color themes) and not for most of our CSS code.

For me the trade-off is between:

  • there might be easier performance optimization possibilities than complex and error-prone minification, our time might be spent better on something else than CSS minification, and
  • css4j is an existing dependency and could provide a simple drop-in that provides a smooth migration, and the simple win from minification might be hard to replace by another optimization (like actually eliminating unused/inefficient CSS code, which might be even more error-prone if we’re not very careful).

To help me understand better, when you mention “2 broken features” for css4j, do you mean that those features triggered the fallback to the source, or would those be user-visible bugs? Also, do you have any idea how complex the implementation would be? Could we keep the existing URLs? I’m also wondering if we shouldn’t keep shipping minified files for backwards compatibility and add our own Maven plugin for that.

1. A keyframe 0% is written as 0

/* Source */
@keyframes fade { 0% { opacity: 0; } 100% { opacity: 1; } }
/* css4j */
@keyframes fade{0{opacity:0}100%{opacity:1}}

A bare 0 isn’t a valid keyframe selector, so browsers drop that keyframe. The animation then starts from the element’s current value instead of opacity: 0. Already happened with YUI.

2. @supports selector(...) loses its space

/* Source */
@supports selector(:has(a)) { .a:has(a) { color: red; } }
/* css4j */
@supportsselector(:has(a)){.a:has(a){color:red}}

@supportsselector is an unknown at-rule, so browsers drop the whole block. YUI keeps this one correct, so it would be a regression compared to YUI. Nothing in xwiki-platform uses @supports selector(...) today.


Note that both are things we could report to the CSS4J project and expect some kind of resolution on the tool, so it’s definitely nicer than YUI. Both are minor fails for us, so IMO it’s not a dealbreaker.

From what I understand, the hardest/most dangerous part would be the switch to full runtime evaluation. I have no idea how complex this would be, there’s quite a lot of moving parts to update. Moving from YUI to CSS4J on already runtime evaluation is quite simple since it’s not a new dependency of the project.

I propose to keep the existing URLs, but mark them as deprecated when they target a “minified at build” source. If we decide to not minify at all, they would just serve the unminified source (and all the minified sources would be marked as deprecated). I doubt this would break anything for backwards compatibility. If something relied on a minification quirk, I think it’s fair to assume we can’t support that use.

If we want to ship a maven plugin for backwards compatibility, then the solution I ruled out earlier of a custom tool would work just as well. Stripping spaces and comments with a 40 lines script would achieve about 80% of the minification YUI performs today. This is quite a simple solution, we’d just replace YUI with our own stripping-tool that doesn’t break velocity (i.e. leaving the newlines in place). The main issue of this is that we’d need to maintain it, not sure how hard it’d be compared to implementing a use for external tools. Let me know if you want more details on this possibility.

Thanks for the proposal @CharpentierLucas !

Before answering, I wanted to ask if you considered the option of moving some CSS to webjar-node modules where we could have access to npm tooling that could help minifying CSS while supporting modern CSS constructs.

I didn’t check, but we need either:

  • A tool that supports velocity (I doubt there’s one even on npm)
  • Changes to have all minification to be done at runtime.

If we agree that we still want minification, I’ll check out what it would entail to move the CSS to webjar-node and the best alternatives in npm to css4j :+1:

My thoughts:

Thanks,
Marius

Yes, I’m afraid those minified versions are essentially APIs and I expect various extensions to have problems if they vanish.