Skip to content

fix: prevent infinite loop in base URL with a relative base - #1825

Merged
cossssmin merged 1 commit into
masterfrom
fix/baseurl-relative-infinite-loop
Aug 3, 2026
Merged

fix: prevent infinite loop in base URL with a relative base#1825
cossssmin merged 1 commit into
masterfrom
fix/baseurl-relative-infinite-loop

Conversation

@cossssmin

@cossssmin cossssmin commented Aug 3, 2026

Copy link
Copy Markdown
Member

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 a url() in a <style> block or inline style attribute.

Minimal repro:

import { base } from '@maizzle/framework'
base('<style>a{background-image:url(x.jpg)}</style>', '/images/') // hangs forever
base('<style>a{background-image:url(x.jpg)}</style>', 'https://cdn/') // fine

Cause

postcssBaseUrl rewrites url() from a PostCSS Declaration visitor. PostCSS 8 re-visits a declaration whose value a visitor mutates (so other plugins can react). The plugin prepends the base only when isAbsoluteUrl(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 OnceExit pass via root.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 inline style — each asserts the base is prepended exactly once (url(/images/bg.jpg), never /images//images/). Full base.test.ts suite green (79 tests), lint clean.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed CSS URL rewriting to prevent base paths from being added more than once.
    • Improved handling of relative base URLs in both <style> blocks and inline styles.
    • Ensured image and other asset paths retain the correct URL structure.

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>
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 395e293c-b063-4726-8d12-5fafe7417008

📥 Commits

Reviewing files that changed from the base of the PR and between 551a17c and 470954d.

📒 Files selected for processing (2)
  • src/tests/transformers/base.test.ts
  • src/transformers/base.ts

📝 Walkthrough

Walkthrough

The 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.

Changes

Base-URL rewriting

Layer / File(s) Summary
Single-pass declaration rewriting
src/transformers/base.ts, src/tests/transformers/base.test.ts
The transformer uses a reusable declaration helper and one OnceExit pass. Tests verify that relative CSS URLs do not receive repeated base-path prefixes.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main fix: preventing an infinite loop when the base URL is relative.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/baseurl-relative-infinite-loop

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.

@cossssmin
cossssmin merged commit 20a6298 into master Aug 3, 2026
6 checks passed
@cossssmin
cossssmin deleted the fix/baseurl-relative-infinite-loop branch August 3, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant