aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortdro <tdro@noreply.example.com>2022-06-27 22:24:02 -0400
committertdro <tdro@noreply.example.com>2022-06-27 22:24:02 -0400
commitcd3fcf7bb2c77ab627e0bb28eb2348c3401b600b (patch)
tree55b9332a93cc2e24426f922315903d9c387bc99a
parent13d8c73a9e3e76b3fbb1493ad6f304ba258a2aa3 (diff)
downloadcanory-cd3fcf7bb2c77ab627e0bb28eb2348c3401b600b.tar.gz
canory-cd3fcf7bb2c77ab627e0bb28eb2348c3401b600b.tar.bz2
canory-cd3fcf7bb2c77ab627e0bb28eb2348c3401b600b.zip
themes/default/layouts/_default/_markup/render-image: Try not to fetch again
Messy but let's try to cache remote images and not fetch again unless necessary. Use webp to check for existence. TODO: will have clean this up later.
-rw-r--r--themes/default/layouts/_default/_markup/render-image.html67
1 files changed, 43 insertions, 24 deletions
diff --git a/themes/default/layouts/_default/_markup/render-image.html b/themes/default/layouts/_default/_markup/render-image.html
index b588842..b3328e0 100644
--- a/themes/default/layouts/_default/_markup/render-image.html
+++ b/themes/default/layouts/_default/_markup/render-image.html
@@ -1,32 +1,40 @@
{{- $source := $.Destination | safeURL -}}
{{- $public := print "public/" $source -}}
-{{- $remote := resources.GetRemote $source -}}
{{- $local := "" -}}
+{{- $cache := "" -}}
+{{- $exists := "" -}}
{{- $width := "" -}}
{{- $height := "" -}}
-{{- $cache := "" -}}
{{- $dimensions := "600x360" -}}
-{{- $immutable := print (.Page.RelPermalink | humanize | urlize) "-" ($source | sha256 | truncate 8 "") -}}
+{{- $author := .Page.Section -}}
+{{- $relURL := strings.TrimPrefix .Page.Site.BaseURL .Page.Permalink -}}
+{{- $immutable := print ($relURL | humanize | urlize) "-" ($source | sha256 | truncate 8 "") -}}
+{{- $fileCache := print (partial "function-paths-media.html") "/" $author "/" $immutable -}}
-{{- with $remote -}}
- {{- with .Err -}}
- {{- if fileExists $public -}}
- {{- $local = resources.Get $public -}}
- {{- with $local -}}
- {{- $local = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $.Page.Section "/" (path.Base .)) -}}
+{{- if not (fileExists (print "public/" $fileCache (path.Ext $source))) -}}
+ {{- with $remote := resources.GetRemote $source -}}
+ {{- with .Err -}}
+ {{- if fileExists $public -}}
+ {{- $local = resources.Get $public -}}
+ {{- with $local -}}
+ {{- $local = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $author "/" (path.Base .)) -}}
+ {{- end -}}
+ {{- $source = print ("" | absURL) $source -}}
+ {{- else -}}
+ {{- $404image := resources.Get "data/media/404.png" -}}
+ {{- with $404image -}}
+ {{- $width = .Width -}}
+ {{- $height = .Height -}}
+ {{- end -}}
+ {{- $source = $404image.RelPermalink -}}
{{- end -}}
- {{ $source = print ("" | absURL) $source }}
{{- else -}}
- {{- $404image := resources.Get "data/media/404.png" -}}
- {{- with $404image -}}
- {{- $width = .Width -}}
- {{- $height = .Height -}}
- {{- end -}}
- {{ $source = $404image.RelPermalink }}
+ {{- $cache = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $author "/" (path.Base .)) -}}
{{- end -}}
- {{- else -}}
- {{- $cache = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $.Page.Section "/" (path.Base .)) -}}
- {{ end }}
+ {{- end -}}
+{{- else -}}
+ {{- $exists = resources.Get (print "public/" $fileCache (path.Ext $source)) -}}
+ {{- $exists = $exists.Content | resources.FromString (print (partial "function-paths-media.html") "/" $author "/" (path.Base $exists)) -}}
{{ end }}
{{- /* This comment removes trailing newlines and white spaces. */ -}}
<figure>
@@ -34,24 +42,35 @@
<picture>
{{- with $local }}
{{- $local = .Fit (print $dimensions " webp") }}
- {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $.Page.Section "/" $immutable ".webp") }}
+ {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $author "/" $immutable ".webp") }}
<source srcset="{{- $local.RelPermalink -}}" type="image/webp" />
{{- $local = .Fit $dimensions }}
- {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $.Page.Section "/" $immutable (path.Ext $local)) }}
+ {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $author "/" $immutable (path.Ext $local)) }}
{{- $width = $local.Width -}}
{{- $height = $local.Height -}}
{{- $writeToFile := $local.Permalink }}
{{- end }}
{{- with $cache }}
{{- $cache = .Fit (print $dimensions " webp") }}
- {{- $cache = $cache | resources.Copy (print $.Page.Section "/media/" $immutable ".webp") }}
+ {{- $cache = $cache | resources.Copy (print $author "/media/" $immutable ".webp") }}
<source srcset="{{- $cache.RelPermalink -}}" type="image/webp" />
{{- $cache = .Fit $dimensions }}
- {{- $cache = $cache | resources.Copy (print (partial "function-paths-media.html") "/" $.Page.Section "/" $immutable (path.Ext $cache)) }}
+ {{- $cache = $cache | resources.Copy (print (partial "function-paths-media.html") "/" $author "/" $immutable (path.Ext $cache)) }}
{{- $width = $cache.Width -}}
{{- $height = $cache.Height -}}
{{- $cache = $cache.RelPermalink }}
- {{- end }}
+ {{- end -}}
+ {{- with $exists }}
+ {{- if fileExists (print "public/" $fileCache ".webp") -}}
+ <source srcset="/{{- print $fileCache ".webp" -}}" type="image/webp" />
+ {{- end -}}
+ {{- if fileExists (print "public/" $author "/media/" $immutable ".webp") -}}
+ <source srcset="/{{- print $author "/media/" $immutable ".webp" -}}" type="image/webp" />
+ {{- end -}}
+ {{- $width = .Width -}}
+ {{- $height = .Height -}}
+ {{- $cache = .RelPermalink }}
+ {{- end -}}
<img loading="lazy"
src="{{ or $cache $source }}"
alt="{{ $.Text | htmlUnescape }}"