Live data from Hacker News

Critical CSS? Not So Fast

csswizardry.com

51–60 of 90 posts

Re: Critical CSS? Not So Fast

#51
post #11

I am a fan of CSSWizardry and yet I find this post misleading. The examples shown are ways NOT to do frontend performance engineering. The current best performant way to load JS is asynchronously as documented at https://web.dev/efficiently-load-third-party-javascript/ . And the best way to load CSS is with Critical Path CSS + Async CSS as documented at https://web.dev/defer-non-critical-css/ . The easiest way to gen…

The process in the article you link to for critical CSS doesn’t work in the real world as it becomes impossible to maintain This script they use to load the async JS can also be racey and it’s far better to put the link to noncritical styles at the bottom of the page

It is easy with a little automation :). In the build process, adding a step to calculate critical path css helps. This should probably be the last step and run on production post deploy. The calculated CSS is stored in redis which will be appended in the head for subsequent requests. This tool had inspired me to build this process at multiple orgs I have worked with https://github.com/pocketjoso/penthouse.

Agree on the async JS part but I guess browser makers will solve it soon.

Re: Critical CSS? Not So Fast

#52
post #7

Nowadays it's probably better to just prompt the browser to preload CSS early with a link preload header. Any page complicated enough to need critical CSS extraction is probably also going to take 50-100ms to render and transfer to the user. Just send the headers before you render and the client will probably have finished downloading them by the time you're done. If clients are on a crappy network, critical css is e…

I don’t find the HTTP headers approach useful. Headers only appear a few bytes before the HTML so if you keep the regular link tag right at the top of the file you’re good to go; no need to mess with headers. Every HTML file should start with: doctype, html tag, head tag, charset meta tag, title, stylesheet link tag. A couple of those tags are even optional.

The header approach is easier to graft on to an existing system where the entire html document is generated in one go. Otherwise you have to deal with generating the response in chunks which can be nontrivial.

Re: Critical CSS? Not So Fast

#53
post #6

First up, agree with the author. Now if I may, a rant. It boggles the mind that we style bikeshed over CSS. Is it honestly, truly, and pragmatically worthwhile to spend all this engineering effort manipulating, culling, tree shaking, modularizing our CSS? Pre-load that, inline this, async-load that, and what have we got for it? A web full of wiggling content, slow CDNs, dozens of requests, and an experience of a mis-…

The more CSS you load the slower some rendering becomes. Even when the styles aren't used.

If you use a static page this isn't as relevant. It can become significant for single page apps, especially if you dynamically load CSS.

Re: Critical CSS? Not So Fast

#54
post #13
post #9

Earlier quoted context omitted.

> worthwhile to spend all this engineering effort manipulating, culling, tree shaking, modularizing our CSS? You don't have to ever think about this stuff if you use a component-driven frontend framework like Vue w/ Vite (or Webpack). The general design: you use one global .css file for your shared styles (app.css) and then each component has their own inline CSS. Vite will automatically extract each component JS and…

The way SPAs work is very performant but are also poor for indexing. Critical Path CSS is mostly used in user facing sites which one wants to get indexed by Google. As page speed is one of the factors considered in SEO, and rightly, one tries to optimize that. Vue, React, etc. are poor at SEO as they do not even try to do solve the problem as they are used to build apps and not sites.

I see you haven't used these libraries in at least a few years, because they all have frameworks that work with SEO by server side rendering content. React has NextJS while Vue has NuxtJS.

Re: Critical CSS? Not So Fast

#55
post #49

Earlier quoted context omitted.

> yet I find this post misleading. The examples shown are ways NOT to do frontend performance Is advice different than what you prefer "misleading?" Anyway, he doesn't actually weigh in on marking JS with `defer` or `async`. And the article is directly contesting preloading the styles, given his note on race conditions with `media` switching. Moving the CSS before the ` ` closing tag is genuinely a way to really defe…

I said misleading as the author mentions JavaScript preventing rendering which isn't the case for anybody who is looking to optimize their site for speed. There are other such assumptions. Performance engineer is most times going against the default way and standards which the author is aware which is why I am surprised with this post. I have not run into layout janks when I have used critical path CSS. I'd look into…

> the author mentions JavaScript preventing rendering which isn't the case for anybody who is looking to optimize their site for speed

Rewriting your JS to be async-ready can be a huge lift. Not every site pursuing frontend performance is ready to do that, much less has done it. I think this article certainly has an audience.

> I have not run into layout janks when I have used critical path CSS

Call me a skeptic :) if a user is able to scroll beyond the fold before the CSS loads, you've introduced CLS. Spoiler: they will always be able to.

Critical CSS and 0 CLS happens only when you create skeleton placeholder components for everything below the fold as well. There are no "tool/settings" that will do this automatically.

Re: Critical CSS? Not So Fast

#56

Earlier quoted context omitted.

This is the reason I love tailwind. There's no bloat, no headache of naming conflicts (in fact they completely did away with naming), no orphan css. CSS for a full blown website can be less than 5kb. It takes away the pain of thinking about CSS architecture, naming things, conflicts etc and you can just focus on styling.

Tell us a bit more about Tailwind. Last I looked at it a few years ago it seemed a conglomeration of antipatterns. Quick googling is not convincing. Assume that I know _nothing_ about Tailwind, sell it to me. I'd appreciate it. Thanks.

> Assume that I know _nothing_ about Tailwind, sell it to me. I'd appreciate it. Thanks.

Tailwind removes (or wilfully ignores) the "cascading" from css. It breaks all good, old css design patterns on purpose - in favor of "widget-level" styling.

It also effectivly builds its own (extendable) style language via named classes, ending up somewhere between "semantic" and "micro-styling" - but allowing for refactoring as needed. It reduces CSS to an implementation detail.

For example, in "Agile Web Development With Rails 7"[1], a number of form fields are initially styled as:

   ... class="block   shadow   rounded-md   border   border-green-400
       outline-none  px-3   py-2   mt-2   w-full " 
Later reactored to:

  ... class="input-field" via tailwind:


     .input-field {  @apply 

         block   shadow   rounded-md  
        border   border-green-400   outline-none 
         px-3   py-2   mt-2   w-full 

    }
On the surface similar to defining a css class - but effectively scoped to your own code.

In a small project, where you're publishing documents - tailwind might not be great - but in a large project, with many widgets it is a good fit.

Similar to how "movable code" (js on the client) might be a better fit than REST for web applications (as opposed to web pages).

[1] https://pragprog.com/titles/rails7/agile-web-development-wit...

Re: Critical CSS? Not So Fast

#57
post #50

Earlier quoted context omitted.

HN, which is as far removed as you can get from modern websites takes 700-900ms from initial request to render if you're in a logged in session. Whoever claims that sub second rendering isn't outstanding is just clueless.

Your location decides the latency of the request. If you are close to server you can do less than 200ms time to first byte. Getting a server near your users help. After that you have 500ms to render your webpage. If you are lazy loading images, and most assets below the fold, thats a lot of time to get a page rendered. You may take a look at my personal website ( https://www.troysk.com/ ) which is hosted via Cloudfla…

You have less CSS than HN, and hardly any JS. This doesn't add anything to the discussion of if this is easy or hard to do – your site will always have a fast FCP, even if you weren't serving with a CDN.

Note also that the parent commenter mentioned being logged in to HN. You're serving a static page, which will always be fast. There are many aspects of HN's site that cannot be cached for logged in users, which will mean a higher TTFB.

> If you are lazy loading images

> sub second render on first load even though its image heavy

Images are not render blocking, and so won't be a part of this discussion. (Nor would I consider 1MB of mostly SVGs to be "image heavy")

Your site actually has room for improvement on performance. You could likely cut your overall page weight in half (not that it would do much, being default fast).

Re: Critical CSS? Not So Fast

#58
post #43

Earlier quoted context omitted.

How is a class with a single rule, that does exactly what it's supposed to, not "semantic?" What do you think "semantic" means, in this context? Maybe you mean classes organized with a high level of abstraction that have multiple rules and are meant to be related to the document structure in a specific way?

They might mean a system like BEM, where they are semantic in that they represent concepts tightly coupled to the HTML and what it's used for. ...etc BEM is a good example of what Tailwind is a counterpoint to.

Nothing stops you from layering BEM on top of tailwind via @apply - although I'm not sure if that's a good idea. Maybe if creating a full, custom, design-system using tailwind?

Re: Critical CSS? Not So Fast

#59
post #6

First up, agree with the author. Now if I may, a rant. It boggles the mind that we style bikeshed over CSS. Is it honestly, truly, and pragmatically worthwhile to spend all this engineering effort manipulating, culling, tree shaking, modularizing our CSS? Pre-load that, inline this, async-load that, and what have we got for it? A web full of wiggling content, slow CDNs, dozens of requests, and an experience of a mis-…

> Is it [...] pragmatically worthwhile

YES! It affects e-commerce conversion rates measurably.

Great point by the author: "make sure it’s the right thing to focus on." Hobby site? Don't care about FOUC. E-commerce? It's a game-changer.

> The brief flash of your half-styled website is useless to me

Not to the rest our users though! Are you on a 3G connection in a third-world country? If not your experience may be not be shared but every user.

Re: Critical CSS? Not So Fast

#60
post #6

First up, agree with the author. Now if I may, a rant. It boggles the mind that we style bikeshed over CSS. Is it honestly, truly, and pragmatically worthwhile to spend all this engineering effort manipulating, culling, tree shaking, modularizing our CSS? Pre-load that, inline this, async-load that, and what have we got for it? A web full of wiggling content, slow CDNs, dozens of requests, and an experience of a mis-…

>The brief flash of your half-styled website is useless to me. Block rendering until the CSS is done.

I have good news for you: https://web.dev/critical-rendering-path-render-blocking-css

CSS is already a render blocker.

Post reply on HN