552 words
3 minutes
Removing Unused CSS With PurgeCSS

Removing Unused CSS With PurgeCSS#

Cleaning up CSS has always been an important part of my web development workflow. Unused CSS is one of the most common issues that can slow down a site and harm the user experience. Not only does it inflate the bundle size, but it also negatively impacts Core Web Vitals, which Google uses to evaluate SEO performance. That’s why I turned to PurgeCSS, a tool designed to strip away all unused CSS rules and improve performance.

Why Is Unused CSS a Problem?#

In my experience, unused CSS often creeps into projects when working with CSS frameworks like TailwindCSS or Bootstrap. It’s easy to only use a fraction of the provided utilities, leaving behind a bloated stylesheet. Here’s why that’s an issue:

  • Increased Bundle Size: Sending unnecessary CSS to the client results in longer loading times and higher bandwidth consumption.
  • Developer Confusion: Bloated CSS makes it harder to identify and maintain the styles that are actually in use.
  • SEO and Core Web Vitals: Tools like Google Lighthouse flag unused CSS as a problem, which can hurt your SEO rankings.

To address this, I started using PurgeCSS, and it’s been a game changer.

What Is PurgeCSS?#

PurgeCSS is a tool that analyzes your HTML, JavaScript, and other files to find out which CSS selectors are actually being used. It then removes the rest. What I love about PurgeCSS is its flexibility—it’s highly customizable and works with many frameworks, including Vue, React, Next.js, Hugo, and more.

In my case, I used PurgeCSS with a Next.js project. Here’s how I set it up.

Setting Up PurgeCSS in a Next.js App#

Initializing a Next.js Project#

First, I created a new Next.js app using the following command:

npx create-next-app@latest

I followed the prompts and named my project purge-css-next-js-demo. I also opted to use TailwindCSS for styling. Once my app was ready, I added Bootstrap as an example framework:

npm install bootstrap

I then imported Bootstrap in my layout.js file:

import "bootstrap/dist/css/bootstrap.css";

Finally, I set up a simple homepage to test the setup:

export default function HomePage() {
  return (
    <div className="container">
      <div className="row">
        <div className="col-md-6">
          <h1>Welcome to My Website</h1>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
          <button className="btn btn-primary">Learn More</button>
        </div>
      </div>
    </div>
  );
}

Installing PurgeCSS#

Next, I installed the PostCSS plugin for PurgeCSS:

npm install --save-dev @fullhuman/postcss-purgecss

Since configuring PostCSS disables Next.js’s default behavior, I also installed additional plugins to restore it:

npm install postcss-flexbugs-fixes postcss-preset-env

Configuring PurgeCSS#

I created a postcss.config.js file in my project’s root directory with the following configuration:

module.exports = {
  plugins: [
    "postcss-flexbugs-fixes",
    [
      "postcss-preset-env",
      {
        autoprefixer: {
          flexbox: "no-2009"
        },
        stage: 3,
        features: {
          "custom-properties": false
        }
      }
    ],
    [
      "@fullhuman/postcss-purgecss",
      {
        content: ["./src/app/**/*.{js,jsx,ts,tsx}"],
        defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
        safelist: {
          standard: ["html", "body"]
        }
      }
    ]
  ]
};

This setup ensures that PurgeCSS scans my files for used CSS and removes everything else. I also added a safelist to preserve essential selectors like html and body.

Seeing the Results#

To test PurgeCSS, I built the project:

npm run build

The difference in CSS file size was remarkable. Without PurgeCSS, the CSS bundle was around 189 KB. With PurgeCSS enabled, it dropped to just 10 KB! That’s a significant improvement, especially for performance and SEO.

Why I Recommend PurgeCSS#

Using PurgeCSS has transformed the way I handle CSS in my projects. It’s simple to set up, highly effective, and makes a huge difference in performance. If you’re working on any modern web application, I can’t recommend it enough.


Removing Unused CSS With PurgeCSS
https://jorkaefdev.vercel.app/posts/development/purge-css/
Author
Jorkaef Jarez
Published at
2025-01-23