feat: add errorHandler if resource could not be loaded
- Fixes #7 - Fixes #6 - Add "errorHandler" config key - Add the following error-handlers: - `warning`: prints warning if resource not found under 'assets' - `warning-remote`: prints warning, but tries RemoteGet as alternative - `error`: prints error if resource not found, fails build
This commit is contained in:
+2
-1
@@ -1,3 +1,4 @@
|
|||||||
_vendor/
|
_vendor/
|
||||||
resources/
|
resources/
|
||||||
public/
|
public/
|
||||||
|
.hugo_build.lock
|
||||||
+4
-2
@@ -14,10 +14,12 @@ Params:
|
|||||||
#alt: ""
|
#alt: ""
|
||||||
noscript: true
|
noscript: true
|
||||||
|
|
||||||
|
errorHandler: warning
|
||||||
|
|
||||||
module:
|
module:
|
||||||
hugoVersion:
|
hugoVersion:
|
||||||
extended: true
|
extended: false
|
||||||
min: "0.83.0"
|
min: "0.96.0"
|
||||||
max: "1.00.0"
|
max: "1.00.0"
|
||||||
imports:
|
imports:
|
||||||
- path: "github.com/aFarkas/lazysizes"
|
- path: "github.com/aFarkas/lazysizes"
|
||||||
|
|||||||
@@ -23,4 +23,6 @@ Params:
|
|||||||
# Renderer options:
|
# Renderer options:
|
||||||
class: "my-img-class"
|
class: "my-img-class"
|
||||||
alt: "My logo"
|
alt: "My logo"
|
||||||
noscript: true
|
noscript: true
|
||||||
|
|
||||||
|
errorHandler: warning-remote
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{{ errorf "image path './assets/%s' does not exist. please check the 'assets' directory." .img }}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
{{ $params := . }}
|
||||||
|
{{ $url := .img | absURL }}
|
||||||
|
{{ warnf "image path './assets/%s' does not exist. trying remote get via %s" .img $url }}
|
||||||
|
{{ $res := false }}
|
||||||
|
{{ with $resource := resources.GetRemote $url }}
|
||||||
|
{{ with .Err }}
|
||||||
|
{{ warnf "could not get remotely: %s: %#v" .Error .Data }}
|
||||||
|
{{ else }}
|
||||||
|
{{ $res = merge $params (dict "img" $resource) }}
|
||||||
|
{{ end }}
|
||||||
|
{{ end }}
|
||||||
|
{{ return $res }}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{{ warnf "image path './assets/%s' does not exist. please check the 'assets' directory." .img }}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
Please ensure that image exists in ./assets directory.
|
Please ensure that image exists in ./assets directory.
|
||||||
` }}
|
` }}
|
||||||
|
|
||||||
|
{{/* Parse and normalize input parameters: */}}
|
||||||
{{ $params := merge site.Params.lazyimg (dict
|
{{ $params := merge site.Params.lazyimg (dict
|
||||||
"img" .
|
"img" .
|
||||||
"resizer" (default "responsive" site.Params.lazyimg.resizer)
|
"resizer" (default "responsive" site.Params.lazyimg.resizer)
|
||||||
@@ -24,25 +25,37 @@
|
|||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{ $params = merge $params $userParams }}
|
{{ $params = merge $params $userParams }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
{{/* Attempt to load image resource: */}}
|
||||||
{{ $resType := (printf "%T" $params.img) }}
|
{{ $resType := (printf "%T" $params.img) }}
|
||||||
{{ if not (in $resType "resource") }}
|
{{ $resLoaded := in $resType "resource" }}
|
||||||
{{ $params = merge $params (dict "img" (resources.Get $params.img)) }}
|
{{ if not $resLoaded }}
|
||||||
{{ end }}
|
{{ with $res := resources.Get $params.img }}
|
||||||
{{ if not $params.img }}
|
{{ $params = merge $params (dict "img" $res) }}
|
||||||
{{ errorf "image path ./assets/%s does not exist" $params.img }}
|
{{ $resLoaded = true }}
|
||||||
{{ warnf $usage }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{/* Special handling for WebP renderers: */}}
|
{{/* Handle error if not loaded yet: */}}
|
||||||
{{ if in $params.renderer "webp" }}
|
{{ if not $resLoaded }}
|
||||||
{{/* Override user-supplied target format */}}
|
{{/* Try if error-handler is able to resolve it */}}
|
||||||
{{ $resizeOptionsWebP := printf "%s webp" ($params.resizeoptions | replaceRE "(jpg|png|tif|bmp|gif)" "") }}
|
{{ $params = partial (printf "error-handler/%s" site.Params.lazyimg.errorHandler) $params }}
|
||||||
{{ $paramsWebP := merge $params (dict "resizeoptions" $resizeOptionsWebP) }}
|
{{ if $params }}
|
||||||
{{ $imagesWebP := partial (printf "resizer/%s" $params.resizer) $paramsWebP }}
|
{{ $resLoaded = in (printf "%T" $params.img) "resource" }}
|
||||||
{{ $imagesRegular := partial (printf "resizer/%s" $params.resizer) $params }}
|
{{ end }}
|
||||||
{{ partial (printf "renderer/%s" $params.renderer) (merge $params (dict "imgDict" $imagesWebP "imgDictFallback" $imagesRegular)) }}
|
{{ end }}
|
||||||
{{ else }}
|
{{/* Only proceed if resource has been loaded: */}}
|
||||||
{{ $sizes := partial (printf "resizer/%s" $params.resizer) $params }}
|
{{ if $resLoaded }}
|
||||||
{{ partial (printf "renderer/%s" $params.renderer) (merge $params (dict "imgDict" $sizes)) }}
|
{{/* Special handling for WebP renderers: */}}
|
||||||
|
{{ if in $params.renderer "webp" }}
|
||||||
|
{{/* Override user-supplied target format */}}
|
||||||
|
{{ $resizeOptionsWebP := printf "%s webp" ($params.resizeoptions | replaceRE "(jpg|png|tif|bmp|gif)" "") }}
|
||||||
|
{{ $paramsWebP := merge $params (dict "resizeoptions" $resizeOptionsWebP) }}
|
||||||
|
{{ $imagesWebP := partial (printf "resizer/%s" $params.resizer) $paramsWebP }}
|
||||||
|
{{ $imagesRegular := partial (printf "resizer/%s" $params.resizer) $params }}
|
||||||
|
{{ partial (printf "renderer/%s" $params.renderer) (merge $params (dict "imgDict" $imagesWebP "imgDictFallback" $imagesRegular)) }}
|
||||||
|
{{ else }}
|
||||||
|
{{ $sizes := partial (printf "resizer/%s" $params.resizer) $params }}
|
||||||
|
{{ partial (printf "renderer/%s" $params.renderer) (merge $params (dict "imgDict" $sizes)) }}
|
||||||
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user