Menu

My static site ships the same stylesheet 290 times. Inline CSS is why none of it can be cached.
πŸ“°

My static site ships the same stylesheet 290 times. Inline CSS is why none of it can be cached.

DEV Community: cssΒ·Melisa WongΒ·3 days ago
#lqYNJAEx
#devtocss#devto#dev#pages#inline
Long article (1594 words) β€” no AI summary yet.
Reading 0:00
15s threshold

I generate a static site from a build script: a few hundred pages, one template, per-page text, and one inline <style> block in every <head> . That last part is a caching decision I never consciously made. I just never removed the block.

Last week I ran three measurements on the output directory: how much of the CSS is duplicated, how much of each block is unreferenced on the page that ships it, and what a multi-page visit actually transfers. Only the third one changed my mind. The first two are the reason I could not see the problem.

1. The duplication factor

The audit is about thirty lines. Walk the HTML files, pull out every <style> block, hash it, group pages by hash, then divide total CSS bytes by the bytes of one page per group.

const blocks = new Map ( ) ; for ( const f of files ) { const html = fs . readFileSync ( f , 'utf8' ) ; const css = [ . . . html . matchAll ( /< style [ ^> ] *> ( [ \ s \ S ] *? ) < \ / style >/ gi ) ] . map ( m => m [ 1 ] ) . join ( '\n' ) ; const key = sha256 ( css ) .…

Continue reading β€” create a free account

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