fix: prevent infinite loop in base URL with a relative base - #1825
Merged
Conversation
The postcss-base-url plugin rewrote url() via a `Declaration` visitor. PostCSS 8 re-visits a declaration whose value a visitor mutates, and a relative base (e.g. `/images/`) never makes the URL absolute — so the `isAbsoluteUrl` guard stays false and the base is re-prepended on every re-visit (`/images//images/…`), hanging the build/dev server and leaking memory. Absolute bases go absolute after one pass, so this only bit relative bases (common in dev). Rewrite declarations in a single `OnceExit` pass via `root.walkDecls`, which visits each declaration once and is not subject to visitor re-runs. Add regression tests for relative bases in `<style>` and inline `style`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe base-URL transformer now rewrites CSS declarations in a single traversal. Regression tests verify that relative base paths apply once in style tags and inline styles. ChangesBase-URL rewriting
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Setting a relative
url.base(e.g./images/) hangs the build / dev server — the process spins at 100% CPU and memory climbs unbounded — whenever the document contains aurl()in a<style>block or inlinestyleattribute.Minimal repro:
Cause
postcssBaseUrlrewritesurl()from a PostCSSDeclarationvisitor. PostCSS 8 re-visits a declaration whose value a visitor mutates (so other plugins can react). The plugin prepends the base only whenisAbsoluteUrl(value)is false — but with a relative base the result (/images/x.jpg) is still not absolute, so on the re-visit the guard is false again and it prepends once more:/images//images/x.jpg→/images//images//images/…, forever.Absolute bases become absolute after the first prepend, so the guard trips and the loop terminates — which is why it only surfaced with relative bases (common in dev, where the CDN isn't wanted).
Fix
Rewrite declarations in a single
OnceExitpass viaroot.walkDecls, which visits each declaration exactly once and is not subject to the visitor re-run semantics. Attribute/VML/MSO handling is unchanged.Tests
Added regression tests for a relative base with
url()in a<style>tag and in an inlinestyle— each asserts the base is prepended exactly once (url(/images/bg.jpg), never/images//images/). Fullbase.test.tssuite green (79 tests), lint clean.Summary by CodeRabbit
<style>blocks and inline styles.