[astro-purgecss] Add new option to enable CSS cache busting#1286
[astro-purgecss] Add new option to enable CSS cache busting#1286
Conversation
🦋 Changeset detectedLatest commit: e52d563 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
📝 WalkthroughWalkthroughAdds a new optional Changes
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
commit: |
There was a problem hiding this comment.
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 usinglogger.debuginstead oflogger.infofor 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
infofor 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.
Summary by CodeRabbit
New Features
Documentation
Chores
Refactor