Skip to content

Commit 65c2720

Browse files
committed
lazy load cover images in lists only
1 parent 28ec7a4 commit 65c2720

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

layouts/_default/list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ <h1>
6565

6666
<article class="{{ $class }}">
6767
{{- $isHidden := (.Param "cover.hiddenInList") | default (.Param "cover.hidden") | default false }}
68-
{{- partial "cover.html" (dict "cxt" . "IsHome" true "isHidden" $isHidden) }}
68+
{{- partial "cover.html" (dict "cxt" . "IsSingle" false "isHidden" $isHidden) }}
6969
<header class="entry-header">
7070
<h2>
7171
{{- .Title }}

layouts/_default/single.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h1 class="post-title">
2222
{{- end }}
2323
</header>
2424
{{- $isHidden := (.Param "cover.hiddenInSingle") | default (.Param "cover.hidden") | default false }}
25-
{{- partial "cover.html" (dict "cxt" . "IsHome" false "isHidden" $isHidden) }}
25+
{{- partial "cover.html" (dict "cxt" . "IsSingle" true "isHidden" $isHidden) }}
2626
{{- if (.Param "ShowToc") }}
2727
{{- partial "toc.html" . }}
2828
{{- end }}

layouts/partials/cover.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{{- with .cxt}} {{/* Apply proper context from dict */}}
22
{{- if (and .Params.cover.image (not $.isHidden)) }}
33
{{- $alt := (.Params.cover.alt | default .Params.cover.caption | plainify) }}
4+
{{- $loading := cond $.IsSingle "eager" "lazy" }}
45
<figure class="entry-cover">
56
{{- $responsiveImages := (.Params.cover.responsiveImages | default site.Params.cover.responsiveImages) | default true }}
6-
{{- $addLink := (and site.Params.cover.linkFullImages (not $.IsHome)) }}
7+
{{- $addLink := (and site.Params.cover.linkFullImages $.IsSingle) }}
78
{{- $pageBundleCover := (.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
89
{{- $globalResourcesCover := (resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
910
{{- $cover := (or $pageBundleCover $globalResourcesCover)}}
@@ -17,24 +18,24 @@
1718
{{- end -}}
1819
{{- $prod := (hugo.IsProduction | or (eq site.Params.env "production")) }}
1920
{{- if (and (in $processableFormats $cover.MediaType.SubType) ($responsiveImages) (eq $prod true)) }}
20-
<img loading="lazy" srcset="{{- range $size := $sizes -}}
21+
<img loading="{{$loading}}" srcset="{{- range $size := $sizes -}}
2122
{{- if (ge $cover.Width $size) -}}
2223
{{ printf "%s %s" (($cover.Resize (printf "%sx" $size)).Permalink) (printf "%sw ," $size) -}}
2324
{{ end }}
2425
{{- end -}}{{$cover.Permalink }} {{printf "%dw" ($cover.Width)}}"
2526
sizes="(min-width: 768px) 720px, 100vw" src="{{ $cover.Permalink }}" alt="{{ $alt }}"
2627
width="{{ $cover.Width }}" height="{{ $cover.Height }}">
2728
{{- else }}{{/* Unprocessable image or responsive images disabled */}}
28-
<img loading="lazy" src="{{ (path.Join .RelPermalink .Params.cover.image) | absURL }}" alt="{{ $alt }}">
29+
<img loading="{{$loading}}" src="{{ (path.Join .RelPermalink .Params.cover.image) | absURL }}" alt="{{ $alt }}">
2930
{{- end }}
3031
{{- else }}{{/* For absolute urls and external links, no img processing here */}}
3132
{{- if $addLink }}<a href="{{ (.Params.cover.image) | absURL }}" target="_blank"
3233
rel="noopener noreferrer">{{ end -}}
33-
<img loading="lazy" src="{{ (.Params.cover.image) | absURL }}" alt="{{ $alt }}">
34+
<img loading="{{$loading}}" src="{{ (.Params.cover.image) | absURL }}" alt="{{ $alt }}">
3435
{{- end }}
3536
{{- if $addLink }}</a>{{ end -}}
3637
{{/* Display Caption */}}
37-
{{- if not $.IsHome }}
38+
{{- if $.IsSingle }}
3839
{{ with .Params.cover.caption }}<p>{{ . | markdownify }}</p>{{- end }}
3940
{{- end }}
4041
</figure>

0 commit comments

Comments
 (0)