Skip to content

Bundled SVG as dataurl drops fragment (id) specifier #4370

Closed

Description

@johnbartholomew

I'm using esbuild to bundle CSS (amongst other things) and embed small SVGs as data-uris. CSS clip-path supports referencing a specific clipPath id from an SVG resource by fragment specifier. This works for me (in Chrome) without bundling, however when the SVG is converted to a data-uri, the fragment specifier is dropped. Embedding it "manually" and including the fragment specifier on the data-uri works for me (in Chrome).

Example:

$ esbuild --version
0.27.2
$ esbuild --serve --outdir=. --servedir=. --bundle --loader:.svg=dataurl arrow.css
# ...

arrow.html:

<!DOCTYPE html>
<html>
	<head><link rel="stylesheet" href="arrow.css"/></head>
	<body>
		<a href="" class="with-arrow">Example</a>
	</body>
</html>

arrow.css (note the clip-path url has fragment specifier #c1):

.with-arrow::after {
	content: "";
	display: inline-block;
	width: 1em;
	height: 1em;
	clip-path: url(./clip-arrow-right.svg#c1);
	background: currentColor;
}

clip-arrow-right.svg:

<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1" viewBox="0 0 1 1"><defs><clipPath id="c1" clipPathUnits="objectBoundingBox"><path d="M.7008.4219H.0268C.012.4219 0 .4324 0 .4453V.5547C0 .5676.012.5781.0268.5781h.674v.09c0 .0418.0577.0627.0915.0331L.9843.5331c.0209-.0183.0209-.048 0-.0663L.7922.2988C.7585.2692.7008.2902.7008.3319v.09Z" /></clipPath</defs></svg>

Example bundle output for the CSS only:

$ esbuild --bundle --loader:.svg=dataurl arrow.css 
/* arrow.css */
.with-arrow::after {
  content: "";
  display: inline-block;
  width: 1em;
  height: 1em;
  clip-path: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1" viewBox="0 0 1 1"><defs><clipPath id="c1" clipPathUnits="objectBoundingBox"><path d="M.7008.4219H.0268C.012.4219 0 .4324 0 .4453V.5547C0 .5676.012.5781.0268.5781h.674v.09c0 .0418.0577.0627.0915.0331L.9843.5331c.0209-.0183.0209-.048 0-.0663L.7922.2988C.7585.2692.7008.2902.7008.3319v.09Z" /></clipPath</defs></svg>%0A');
  background: currentColor;
}

Activity

  1. evanw commented on Feb 5, 2026

    @evanw
    Owner

    Huh, that's pretty crazy. I had no idea that this was a feature. Thanks for the heads up!

  2. added a commit that references this issue on Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions