mirror of
https://github.com/nunocoracao/blowfish.git
synced 2026-01-30 15:31:52 +00:00
fix(render-image): improper srcset usage
- correct srcset image w descriptor value - use compressed image for src fallback
This commit is contained in:
@@ -2,21 +2,53 @@
|
|||||||
<img class="my-0 rounded-md" loading="lazy" alt="{{ .alt }}" src="{{ .src }}">
|
<img class="my-0 rounded-md" loading="lazy" alt="{{ .alt }}" src="{{ .src }}">
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{ define "RenderImageResponsive" -}}
|
{{- define "RenderImageResponsive" -}}
|
||||||
|
{{/* Responsive Image
|
||||||
|
The current setting sizes="(min-width: 768px) 50vw, 65vw" makes the iPhone 16 and 16 Pro
|
||||||
|
select a smaller image, while the iPhone 16 Pro Max selects a larger image.
|
||||||
|
|
||||||
|
Steps:
|
||||||
|
1. Check the media queries in the `sizes` property.
|
||||||
|
2. Find the first matching value. For example, on a mobile device with a CSS pixel width
|
||||||
|
of 390px and DPR = 3 (iPhone 13), given setting sizes="(min-width: 768px) 50vw, 100vw",
|
||||||
|
it matches the `100vw` option.
|
||||||
|
3. Calculate the optimal image size: 390 × 3 × 100% (100vw) = 1170.
|
||||||
|
4. Find the corresponding match in the `srcset`.
|
||||||
|
|
||||||
|
To make the browser select a smaller image on mobile devices
|
||||||
|
override the template and change the `sizes` property to "(min-width: 768px) 50vw, 30vw"
|
||||||
|
|
||||||
|
The sizes="auto" is valid only when loading="lazy".
|
||||||
|
*/}}
|
||||||
|
{{- $imgObj := .imgObj -}}
|
||||||
|
{{- $alt := .alt -}}
|
||||||
|
{{- $originalWidth := $imgObj.Width -}}
|
||||||
|
|
||||||
|
{{- $img800 := $imgObj -}}
|
||||||
|
{{- $img1280 := $imgObj -}}
|
||||||
|
{{- if gt $originalWidth 800 -}}
|
||||||
|
{{- $img800 = $imgObj.Resize "800x" -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- if gt $originalWidth 1280 -}}
|
||||||
|
{{- $img1280 = $imgObj.Resize "1280x" -}}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
{{- $srcset := printf "%s 800w, %s 1280w" $img800.RelPermalink $img1280.RelPermalink -}}
|
||||||
|
|
||||||
|
|
||||||
<img
|
<img
|
||||||
class="my-0 rounded-md"
|
class="my-0 rounded-md"
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
fetchpriority="low"
|
fetchpriority="auto"
|
||||||
alt="{{ .alt }}"
|
alt="{{ $alt }}"
|
||||||
srcset="
|
{{ with $imgObj.Width }}width="{{ . }}"{{ end }}
|
||||||
{{ (.resource.Resize "330x").RelPermalink }} 330w,
|
{{ with $imgObj.Height }}height="{{ . }}"{{ end }}
|
||||||
{{ (.resource.Resize "660x").RelPermalink }} 660w,
|
src="{{ $img800.RelPermalink }}"
|
||||||
{{ (.resource.Resize "1280x").RelPermalink }} 1280w
|
srcset="{{ $srcset }}"
|
||||||
"
|
sizes="(min-width: 768px) 50vw, 65vw"
|
||||||
data-zoom-src="{{ .resource.RelPermalink }}"
|
data-zoom-src="{{ $imgObj.RelPermalink }}">
|
||||||
src="{{ .resource.RelPermalink }}">
|
{{- end -}}
|
||||||
{{- end }}
|
|
||||||
|
|
||||||
{{ define "RenderImageCaption" -}}
|
{{ define "RenderImageCaption" -}}
|
||||||
{{- with .caption }}
|
{{- with .caption }}
|
||||||
@@ -44,7 +76,7 @@
|
|||||||
{{- $isSVG := eq $resource.MediaType.SubType "svg" }}
|
{{- $isSVG := eq $resource.MediaType.SubType "svg" }}
|
||||||
{{- $shouldOptimize := and (not $disableImageOptimizationMD) (not $isSVG) }}
|
{{- $shouldOptimize := and (not $disableImageOptimizationMD) (not $isSVG) }}
|
||||||
{{- if $shouldOptimize }}
|
{{- if $shouldOptimize }}
|
||||||
{{ template "RenderImageResponsive" (dict "resource" $resource "alt" $altText) }}
|
{{- template "RenderImageResponsive" (dict "imgObj" $resource "alt" $altText) -}}
|
||||||
{{- else }}
|
{{- else }}
|
||||||
{{ template "RenderImageSimple" (dict "src" $resource.RelPermalink "alt" $altText) }}
|
{{ template "RenderImageSimple" (dict "src" $resource.RelPermalink "alt" $altText) }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|||||||
@@ -31,14 +31,18 @@
|
|||||||
{{- else }}
|
{{- else }}
|
||||||
<img
|
<img
|
||||||
class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}"
|
class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}"
|
||||||
srcset="
|
loading="lazy"
|
||||||
{{ (.Resize "330x").RelPermalink }} 330w,
|
decoding="async"
|
||||||
{{ (.Resize "660x").RelPermalink }} 660w,
|
fetchpriority="auto"
|
||||||
{{ (.Resize "1024x").RelPermalink }} 1024w,
|
|
||||||
{{ (.Resize "1320x").RelPermalink }} 2x"
|
|
||||||
src="{{ (.Resize "660x").RelPermalink }}"
|
|
||||||
data-zoom-src="{{ (.Resize "1320x").RelPermalink }}"
|
|
||||||
alt="{{ $altText }}"
|
alt="{{ $altText }}"
|
||||||
|
{{ with .Width }}width="{{ . }}"{{ end }}
|
||||||
|
{{ with .Height }}height="{{ . }}"{{ end }}
|
||||||
|
src="{{ (.Resize "800x").RelPermalink }}"
|
||||||
|
srcset="
|
||||||
|
{{- (.Resize "800x").RelPermalink }} 800w,
|
||||||
|
{{- (.Resize "1280x").RelPermalink }} 1280w"
|
||||||
|
sizes="(min-width: 768px) 50vw, 65vw"
|
||||||
|
data-zoom-src="{{ .RelPermalink }}"
|
||||||
/>
|
/>
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- else }}
|
{{- else }}
|
||||||
|
|||||||
Reference in New Issue
Block a user