Skip to content

[astro-purgecss] Add new option to enable CSS cache busting#1286

Merged
mhdcodes merged 6 commits intomainfrom
astro-purgecss-add-strategy
Feb 13, 2026
Merged

[astro-purgecss] Add new option to enable CSS cache busting#1286
mhdcodes merged 6 commits intomainfrom
astro-purgecss-add-strategy

Conversation

@mhdcodes
Copy link
Copy Markdown
Member

@mhdcodes mhdcodes commented Feb 13, 2026

Summary by CodeRabbit

  • New Features

    • Added a cache-busting CSS strategy to force rebuild/hash updates for improved cache invalidation.
  • Documentation

    • Expanded README with a "Strategies" section, usage examples, and SSR guidance for shared CSS.
    • Minor formatting tweak to contributing instructions.
  • Chores

    • Added a changeset for a minor version bump.
  • Refactor

    • Small build-time optimization to avoid unnecessary file operations.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Feb 13, 2026

🦋 Changeset detected

Latest commit: e52d563

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
astro-purgecss Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 13, 2026

📝 Walkthrough

Walkthrough

Adds a new optional strategy ("default" | "cache-buster") to astro-purgecss, implements conditional Vite plugin registration to inject a cache-busting marker during build when selected, expands README with strategy guidance and SSR notes, adds a changeset, and optimizes CSS file write path comparison.

Changes

Cohort / File(s) Summary
Changeset
\.changeset/tall-areas-cheer.md
New changeset added documenting a minor version bump and the CSS cache-buster option.
Documentation
CONTRIBUTING.md, packages/astro-purgecss/README.md
Formatting tweak in contributing guide; README extended with `strategy?: 'default'
Core Feature
packages/astro-purgecss/src/index.ts
Adds PurgeStrategy type and strategy option, imports randomUUID, uses purgecssOptions, computes skipRehash including cache-buster case, and conditionally registers a Vite plugin to inject a cache-buster marker during build.
Utilities
packages/astro-purgecss/src/utils.ts
Optimizes writeCssFile by short-circuiting when old and new file paths are identical to avoid unnecessary filesystem ops.
Package Manifest
packages/astro-purgecss/package.json
Small package metadata edit (1 line).

Sequence Diagram(s)

sequenceDiagram
    participant Astro as Astro Build
    participant Config as Plugin Config
    participant PurgeCSS as PurgeCSS Process
    participant VitePlugin as Vite Plugin
    participant FS as File System

    Astro->>Config: Read `strategy` from options
    Config-->>Astro: Return 'default' or 'cache-buster'

    alt strategy == "cache-buster"
        Astro->>VitePlugin: Register cache-buster plugin on build hooks
        VitePlugin->>VitePlugin: Prepare to inject UUID header
    end

    Astro->>PurgeCSS: Run purge with `purgecssOptions`
    PurgeCSS->>FS: Write purged CSS files (maybe renamed)
    FS-->>PurgeCSS: File write results

    alt strategy == "cache-buster"
        VitePlugin->>FS: Inject UUID comment into CSS assets
        FS-->>VitePlugin: Asset updated
    end

    FS-->>Astro: Assets finalized, build complete
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I nibble code and stir the air,
I tuck a UUID in styles with care,
No stale cache will dare to stay,
Default or bouncy — hop away! 🎨✨

🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a new strategy option to enable CSS cache busting as an alternative to the default rehashing behavior.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch astro-purgecss-add-strategy

No actionable comments were generated in the recent review. 🎉


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Feb 13, 2026

Open in StackBlitz

npm i https://pkg.pr.new/codiume/orbit/astro-gtm@1286
npm i https://pkg.pr.new/codiume/orbit/astro-purgecss@1286
npm i https://pkg.pr.new/codiume/orbit/astro-seo-meta@1286
npm i https://pkg.pr.new/codiume/orbit/astro-seo-schema@1286

commit: e52d563

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@packages/astro-purgecss/README.md`:
- Around line 243-254: Update the README example that uses output: 'hybrid' to
reflect Astro v5 changes: replace the `output: 'hybrid'` usage with `output:
'server'` and add a note to control static pages via per-route `prerender`
instead of hybrid mode; update the example block that shows the purgecss
integration (the code using defineConfig and purgecss({ strategy: 'cache-buster'
})) so it validates under Astro v5 by using `output: 'server'` and mention
per-route `prerender` where appropriate.
- Line 137: Change the sentence fragment "best for static site generation
(`output: 'static'`)" in the README to start with a capital letter—replace
"best" with "Best" so the sentence reads "Best for static site generation
(`output: 'static'`)" (edit the occurrence that follows "This ensures the
filename hash...").

In `@packages/astro-purgecss/src/index.ts`:
- Around line 56-66: The regex in the transform hook is testing the raw Vite
module id (variable id) which can include query strings like ?astro&type=style,
so /\. (css|scss|sass|less|styl)$/ will miss those; update the transform
implementation (the transform function that logs via logger.info and builds the
cache-buster using randomUUID()) to strip the query string from id (e.g., use
the part before '?') before running the file-extension regex test, then continue
to log and return the injected code only when the stripped filename matches the
stylesheet extensions.
🧹 Nitpick comments (2)
packages/astro-purgecss/src/index.ts (2)

61-61: Consider using logger.debug instead of logger.info for per-file transform logs.

This logs for every CSS file processed by Vite during the build. In projects with many CSS files/components, this will produce significant noise. Reserve info for summary-level messages.

♻️ Suggested change
-              logger.info(`Injecting cache-buster CSS into: ${id}`);
+              logger.debug(`Injecting cache-buster CSS into: ${id}`);

50-69: Conditional hook registration via object spread works but is somewhat unconventional.

The ...(condition && { hookName: fn }) pattern is valid JS but can be surprising in a hooks object. Consider extracting the hooks object for readability, or add a brief inline comment explaining why the hook is conditionally registered. This is not blocking — just a readability note.

@mhdcodes mhdcodes added the astro-purgecss Issues related to astro-purgecss package label Feb 13, 2026
@mhdcodes mhdcodes merged commit ee6536c into main Feb 13, 2026
8 checks passed
@mhdcodes mhdcodes deleted the astro-purgecss-add-strategy branch February 13, 2026 18:17
@github-actions github-actions bot mentioned this pull request Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

astro-purgecss Issues related to astro-purgecss package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[astro-purgecss] new css filename is not replaced in js files [astro-purgecss] Missing CSS file with hybrid static/SSR site

1 participant