Menu

Our stylesheet declares the same 72 variables twice, and the second copy hides behind a browser sniff dressed as a feature query
📰

Our stylesheet declares the same 72 variables twice, and the second copy hides behind a browser sniff dressed as a feature query

DEV Community: css·Daniel Pertu·2 days ago
#LOIqVIzc
#devtocss#devto#dev#color#supports
Reading 0:00
15s threshold

Our site ships two stylesheets totalling 107,598 bytes. I have counted the rules in them before, from the outside. This time I read the CSSOM on a live page and asked what kind each rule is, which turned out to be the more interesting question.

const kinds = { } ; const walk = ( list ) => { for ( const r of list ) { kinds [ r . constructor . name ] = ( kinds [ r . constructor . name ] || 0 ) + 1 ; if ( r . cssRules && ! ( r instanceof CSSStyleRule ) ) walk ( r . cssRules ) ; } } ; for ( const s of document . styleSheets ) walk ( s . cssRules ) ;

Enter fullscreen mode

Exit fullscreen mode

On our FAQ page :

rule type count

CSSStyleRule 1,254

CSSSupportsRule 97

CSSPropertyRule 72

CSSFontFaceRule 13

CSSMediaRule 13

CSSContainerRule 5

CSSLayerBlockRule 4

CSSKeyframesRule 4

CSSLayerStatementRule 1

The trap in that snippet, which cost me twenty minutes

My first version recursed whenever a rule had cssRules and counted only the leaves. It reported 90 rules .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.