Introduction
While making some performance adjustments at work, I accidentally discovered a strange phenomenon. After investigating further, I found a bug that seemed to have gone unnoticed by anyone, and I found the cause quite interesting. So I thought I’d write a post to share it with everyone.
This post is not very technical, so you can read it with a story-telling mindset, which will make it more interesting.
The Beginning of the Story
The origin of the story is that I was making some adjustments to a website at work, trying to improve its loading speed. When it comes to performance optimization, there are many things that can be done. For example, with regard to the server, the following are more relevant:
- Use HTTP/2
- Use gzip or brotli for compression
- Use Cache (to speed up revisits)
- Use CDN
- Reduce TTFB time
However, all of the above require assistance from the backend or SRE, and are not very relevant to the frontend. With regard to the frontend, there are many aspects to consider. For example, from the perspective of “reducing resources,” the following can be done:
- Image format adjustment (compression + webp or other formats)
- JS size (ugligy, code splitting, dynamic import)
- CSS size (minify, remove unnecessary CSS)
From the perspective of “accelerating the loading of important resources,” preload or preconnect hints can be added to indicate to the browser which things should be loaded first.
You can also look at it from the perspective of “reducing JS execution time.” For example, if you are writing React, you can use shouldComponentUpdate, PureComponent, or memo to reduce unnecessary re-renders.
Since the title of this post is “styled components,” the main topic is, of course, centered around CSS.