Skip to content

Commit 6985433

Browse files
workprentice[bot]workprenticegithub-actions[bot]claude
authored
docs: add width/height to customer-logo SVGs to reduce CLS (#20839)
* docs: add width/height to customer-logo SVGs to reduce CLS The customer-logo.html partial renders customer/case-study logo SVGs without width/height attributes, so the browser cannot reserve layout space for them before the stylesheet that ultimately sizes them (object-contain inside a fixed-size wrapper) has been applied. The homepage alone renders ~170 of these images. Hugo's image-processing pipeline does not expose .Width/.Height for SVG resources (confirmed: calling it throws "this method is only available for raster images"), so this parses each SVG's own declared viewBox (or, when absent, its root <svg> width/height attributes) directly from the source markup and emits those as real width/height attributes, rounded to the nearest integer since the HTML5 spec requires non-negative integers there and several source files declare fractional viewBox dimensions. When neither is present, the partial falls back to the previous unsized behavior rather than fabricate a number. Verified in an isolated Hugo harness (real site's asset pipeline does not build in this environment) against all 42 real SVGs in static/logos/customers/: all 42 now render with valid integer width/height attributes with no build errors. * docs: accept single-quoted viewBox when parsing customer-logo SVGs The viewBox regex required double quotes, so an SVG that writes its attributes single-quoted fell through to the unsized path. Match either quote character and strip the prefix/suffix positionally, since the two TrimPrefix/TrimSuffix calls can no longer key off a fixed literal. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: workprentice <257153108+workprentice@users.noreply.github.com> Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 6a997af commit 6985433

1 file changed

Lines changed: 42 additions & 1 deletion

File tree

layouts/partials/customer-logo.html

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,50 @@
9191
{{ $svgPath := printf "logos/customers/%s.svg" .logo }}
9292
{{ $fingerprintedSvg := resources.Get (printf "fingerprinted/%s" $svgPath) }}
9393
{{ if $fingerprintedSvg }}
94+
{{/* Read the SVG's own declared dimensions so the <img> can carry real width/height
95+
attributes (CLS: lets the browser reserve layout space before the stylesheet
96+
that ultimately sizes the logo via object-contain has applied). Hugo's image
97+
pipeline does not support .Width/.Height on SVG resources, so parse the source
98+
markup directly instead of guessing — first the viewBox (most logos declare
99+
one), falling back to the root <svg> element's own width/height attributes.
100+
If neither is present, degrade to the pre-existing unsized behavior rather
101+
than fabricate a number. */}}
102+
{{ $svgW := "" }}
103+
{{ $svgH := "" }}
104+
{{ with findRE `viewBox=["'][^"']*["']` $fingerprintedSvg.Content 1 }}
105+
{{/* Quote character varies across assets (most are double-quoted; at least one
106+
logo writes single-quoted attributes), so strip the `viewBox=` prefix and
107+
the closing quote positionally rather than by literal. */}}
108+
{{ $vb := substr (index . 0) 9 -1 }}
109+
{{ $vbParts := split $vb " " }}
110+
{{ if eq (len $vbParts) 4 }}
111+
{{ $wCandidate := index $vbParts 2 }}
112+
{{ $hCandidate := index $vbParts 3 }}
113+
{{ if and (findRE `^[0-9.]+$` $wCandidate 1) (findRE `^[0-9.]+$` $hCandidate 1) }}
114+
{{ $svgW = $wCandidate }}
115+
{{ $svgH = $hCandidate }}
116+
{{ end }}
117+
{{ end }}
118+
{{ end }}
119+
{{ if and (not $svgW) (not $svgH) }}
120+
{{ with findRE `<svg\b[^>]*>` $fingerprintedSvg.Content 1 }}
121+
{{ $tag := index . 0 }}
122+
{{ $wAttr := findRE `width="[0-9.]+"` $tag 1 }}
123+
{{ $hAttr := findRE `height="[0-9.]+"` $tag 1 }}
124+
{{ if and $wAttr $hAttr }}
125+
{{ $svgW = strings.TrimSuffix `"` (strings.TrimPrefix `width="` (index $wAttr 0)) }}
126+
{{ $svgH = strings.TrimSuffix `"` (strings.TrimPrefix `height="` (index $hAttr 0)) }}
127+
{{ end }}
128+
{{ end }}
129+
{{ end }}
130+
{{/* width/height are required to be non-negative integers per the HTML5 spec;
131+
several source SVGs declare fractional viewBox dimensions, so round rather
132+
than truncate to stay closest to the SVG's real aspect ratio. */}}
133+
{{ if $svgW }}{{ $svgW = math.Round (float $svgW) }}{{ end }}
134+
{{ if $svgH }}{{ $svgH = math.Round (float $svgH) }}{{ end }}
94135
<span class="{{ $logoClass }} w-full h-full flex m-auto justify-center">
95136
{{ $fingerprintedSvg = $fingerprintedSvg | fingerprint }}
96-
<img src="{{ $fingerprintedSvg.RelPermalink }}" alt="{{ .logo }} logo" loading="lazy" />
137+
<img src="{{ $fingerprintedSvg.RelPermalink }}" alt="{{ .logo }} logo" {{ with $svgW }}width="{{ . }}"{{ end }} {{ with $svgH }}height="{{ . }}"{{ end }} loading="lazy" />
97138
</span>
98139
{{ else }}
99140
<span class="{{ $logoClass }} w-full h-full flex m-auto justify-center">

0 commit comments

Comments
 (0)