Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: Lazy load image by default #552

Merged
merged 6 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ autoSwitchAppearance = true

enableSearch = false
enableCodeCopy = false
enableImageLazyLoading = true

# robots = ""
fingerprintAlgorithm = "sha256"
Expand Down
1 change: 1 addition & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ autoSwitchAppearance = true

enableSearch = true
enableCodeCopy = true
enableImageLazyLoading = true

# robots = ""
fingerprintAlgorithm = "sha256"
Expand Down
1 change: 1 addition & 0 deletions exampleSite/content/docs/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Many of the article defaults here can be overridden on a per article basis by sp
|`autoSwitchAppearance`|`true`|Whether the theme appearance automatically switches based upon the visitor's operating system preference. Set to `false` to force the site to always use the `defaultAppearance`.|
|`enableSearch`|`false`|Whether site search is enabled. Set to `true` to enable search functionality. Note that the search feature depends on the `outputs.home` setting in the [site configuration](#site-configuration) being set correctly.|
|`enableCodeCopy`|`false`|Whether copy-to-clipboard buttons are enabled for `<code>` blocks. The `highlight.noClasses` parameter must be set to `false` for code copy to function correctly. Read more about [other configuration files](#other-configuration-files) below.|
|`enableImageLazyLoading`|`true`|Whether image should be lazy loading.|
|`robots`|_Not set_|String that indicates how robots should handle your site. If set, it will be output in the page head. Refer to [Google's docs](https://developers.google.com/search/docs/advanced/robots/robots_meta_tag#directives) for valid values.|
|`fingerprintAlgorithm`|`"sha256"`|String that indicates which hashing algorithm is used when fingerprinting assets. Valid options include `md5`, `sha256`, `sha384` and `sha512`.|
|`header.layout`|`"basic"`|The layout of the page header and menu. Valid values are `basic`, `hamburger`, `hybrid` or `custom`. When set to `custom`, you must provide your own layout by creating a `/layouts/partials/header/custom.html` file.|
Expand Down
22 changes: 20 additions & 2 deletions layouts/_default/_markup/render-image.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{{ $url := urls.Parse .Destination }}
{{ $altText := .Text }}
{{ $caption := .Title }}
{{ $lazyLoad := $.Page.Site.Params.enableImageLazyLoading | default true }}
{{ if findRE "^https?" $url.Scheme }}
<figure>
<img class="mx-auto my-0 rounded-md" src="{{ $url.String }}" alt="{{ $altText }}" />
<img
class="mx-auto my-0 rounded-md"
src="{{ $url.String }}"
alt="{{ $altText }}"
{{ if $lazyLoad }}
loading="lazy"
{{ end }}
/>
{{ with $caption }}<figcaption class="text-center">{{ . | markdownify }}</figcaption>{{ end }}
</figure>
{{ else }}
Expand Down Expand Up @@ -32,12 +40,22 @@
{{ end }}
{{ end }}
alt="{{ $altText }}"
{{ if $lazyLoad }}
loading="lazy"
{{ end }}
/>
{{ with $caption }}<figcaption class="text-center">{{ . | markdownify }}</figcaption>{{ end }}
</figure>
{{ else }}
<figure>
<img class="mx-auto my-0 rounded-md" src="{{ $url.String }}" alt="{{ $altText }}" />
<img
class="mx-auto my-0 rounded-md"
src="{{ $url.String }}"
alt="{{ $altText }}"
{{ if $lazyLoad }}
loading="lazy"
{{ end }}
/>
{{ with $caption }}<figcaption class="text-center">{{ . | markdownify }}</figcaption>{{ end }}
</figure>
{{ end }}
Expand Down
3 changes: 3 additions & 0 deletions layouts/_default/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ <h1 class="mt-0 text-4xl font-extrabold text-neutral-900 dark:text-neutral">
{{ end }}
{{ end }}
alt="{{ $.Params.featureAlt | default $.Params.coverAlt | default "" }}"
{{ if $.Site.Params.enableImageLazyLoading | default true }}
loading="lazy"
{{ end }}
/>
{{ with $.Params.coverCaption }}
<figcaption class="mb-6 -mt-3 text-center">{{ . | markdownify }}</figcaption>
Expand Down
3 changes: 3 additions & 0 deletions layouts/partials/article-link.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
160w, {{- (.Fill "320x240 smart").RelPermalink }} 2x"
src="{{ (.Fill "160x120 smart").RelPermalink }}"
{{ end }}
{{ if $.Site.Params.enableImageLazyLoading | default true }}
loading="lazy"
{{ end }}
/>
</a>
</div>
Expand Down
3 changes: 3 additions & 0 deletions layouts/partials/author.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
height="96"
alt="{{ $.Site.Author.name | default "Author" }}"
src="{{ $authorImage.RelPermalink }}"
{{ if $.Site.Params.enableImageLazyLoading | default true }}
loading="lazy"
{{ end }}
/>
{{ end }}
{{ end }}
Expand Down
3 changes: 3 additions & 0 deletions layouts/partials/home/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
height="144"
alt="{{ $.Site.Author.name | default "Author" }}"
src="{{ $authorImage.RelPermalink }}"
{{ if $.Site.Params.enableImageLazyLoading | default true }}
loading="lazy"
{{ end }}
/>
{{ end }}
{{ end }}
Expand Down
6 changes: 6 additions & 0 deletions layouts/partials/logo.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
height="{{ div $logo.Height 2 }}"
class="max-h-[10rem] max-w-[10rem] object-scale-down object-left{{ if $logo_dark }} hidden dark:flex{{ end }}"
alt="{{ .Site.Title }}"
{{ if .Site.Params.enableImageLazyLoading | default true }}
loading="lazy"
{{ end }}
/>
{{- if $logo_dark }}
<img
Expand All @@ -17,6 +20,9 @@
height="{{ div $logo_dark.Height 2 }}"
class="max-h-[10rem] max-w-[10rem] object-scale-down object-left dark:hidden"
alt="{{ .Site.Title }}"
{{ if .Site.Params.enableImageLazyLoading | default true }}
loading="lazy"
{{ end }}
/>
{{- end}}
</a>
Expand Down
3 changes: 3 additions & 0 deletions layouts/shortcodes/figure.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<img
class="mx-auto my-0 rounded-md"
alt="{{ $altText }}"
{{ if .Site.Params.enableImageLazyLoading | default true }}
loading="lazy"
{{ end }}
{{ if findRE "^https?" $url.Scheme }}
src="{{ $url.String }}"
{{ else }}
Expand Down
3 changes: 3 additions & 0 deletions layouts/shortcodes/screenshot.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{{- if or (.Get "alt") (.Get "caption") }}
alt="{{ with .Get "alt" }}{{ . }}{{ else }}{{ .Get "caption" | markdownify | plainify }}{{ end }}"
{{- end -}}
{{ if .Site.Params.enableImageLazyLoading | default true }}
loading="lazy"
{{ end }}
width="100%"
height="auto"
style="max-width:{{ div $image.Width 2 }}px; max-height:{{ div $image.Height 2 }}px;"
Expand Down