Instatic icon indicating copy to clipboard operation
Instatic copied to clipboard

Site Import: CSS `url()` assets are not rewritten in rules that follow a root rule removed by token extraction

Open ehnruiz opened this issue 1 month ago • 0 comments

Summary

When a stylesheet has a root-scope rule that token extraction empties and removes (a :root with only colors, only --font-* stacks, nothing at all, or a body with only color variables), every url() in the rules after it in that stylesheet keeps its original relative path. The files are still uploaded to the media library, but the published CSS points at paths that don't exist, so background images 404 with no warning. Rules before the removed rule are fine.

Steps to reproduce

  1. Import a static site with index.html linking css/site.css, plus img/a.svg and img/b.svg:
    :root { --brand: #0575d6; }
    .a { background-image: url("../img/a.svg"); }
    .b { background-image: url("../img/b.svg"); }
    
  2. Publish and open the published style-*.css.

Expected behavior

.a and .b point at the uploaded media, as they do when the :root rule is absent or keeps a non-color declaration.

Actual behavior

Both keep url("../img/a.svg") and url("../img/b.svg"), which resolve against /_instatic/css/ and 404. In the plan:

Stylesheet starts with .a / .b after buildImportPlan
no root rule url('img/a.svg') / url('img/b.svg')
:root { --brand: #0575d6; --space: 1rem; } url('img/a.svg') / url('img/b.svg')
:root { --brand: #0575d6; } url("../img/a.svg") / url("../img/b.svg")
:root { --font-body: "Inter", sans-serif; } url("../img/a.svg") / url("../img/b.svg")
:root { } url("../img/a.svg") / url("../img/b.svg")
body { --ink: #111; } url("../img/a.svg") / url("../img/b.svg")
the :root rule placed after .a and .b url('img/a.svg') / url('img/b.svg')

The cause appears to be an index mismatch:

  • normalizeRules (src/core/siteImport/assetPlan.ts:436) matches each asset ref to its rule by ref.ruleIndex (refsByRule.get(ruleIdx), line 456).
  • Those indices come from cssToStyleRules. But the rules handed to it are the ones left after extractRootColorTokens and extractRootFontTokens (src/core/siteImport/planCss.ts:86–95), which drop a root rule they empty.
  • Every later rule moves up one index, so its refs are applied to the rule before it, where the url() isn't found, and the rule itself is never normalized.

A possible fix: keep the rule indices stable through token extraction (for example, extract after asset normalization, or carry an original-index map), or bind asset refs to the rule object instead of its index.

Scale, on a real 7-page Webflow site: an empty :root {} near the top of the Webflow stylesheet left all 15 url()s after it unrewritten (social icons and section backgrounds). All 15 files were uploaded.

Version or commit

0.0.20 (f92e8dc, current main)

Deployment mode

Local, bun run start (SQLite), Bun 1.4.2, macOS

ehnruiz avatar Oct 11 '26 10:10 ehnruiz