preload banner url

master
Aevann 2023-03-23 14:38:30 +02:00
parent 721bda827f
commit 0c3600e9a0
2 changed files with 19 additions and 18 deletions

View File

@ -27,7 +27,25 @@
{% set expand = false %}
{% endif %}
{% endif %}
{{macros.banner(src, href, alt, expand, class)}}
<a href="{{href|default(src)}}" rel="nofollow noopener">
{% if src %}
{% set banner_url = src %}
{% else %}
{% set path = "files/assets/images/" ~ SITE_NAME %}
{%- if not v and os_path.exists(path ~ "/cached.webp") -%}
{% set banner_url = 'cached.webp' | asset_siteimg %}
{% elif os_path.exists(path ~ "/banners") -%}
{% set banner_url = macros.random_image("assets/images/" ~ SITE_NAME ~ "/banners") %}
{% else -%}
{% set banner_url = 'banner.webp' | asset_siteimg %}
{% endif %}
{% endif %}
<link rel="preload" as="image" href="{{banner_url}}">
<img data-nonce="{{g.nonce}}" data-onclick="{{expand|default('expandImage()')}}" class="{{class|default('site-banner')}}" alt="{{alt|default('site banner')}}" src="{{banner_url}}">
</a>
{% endif %}
{% endif %}
{% endblock %}

View File

@ -4,23 +4,6 @@
{%- endif -%}
{%- endmacro -%}
{%- macro banner(src, href, alt, expand, class) %}
<a href="{{href|default(src)}}" rel="nofollow noopener">
<img data-nonce="{{g.nonce}}" data-onclick="{{expand|default('expandImage()')}}" class="{{class|default('site-banner')}}" alt="{{alt|default('site banner')}}" src="{{src|default(live_banner())}}">
</a>
{% endmacro -%}
{%- macro live_banner() -%}
{% set path = "files/assets/images/" ~ SITE_NAME %}
{%- if not v and os_path.exists(path ~ "/cached.webp") -%}
{{ 'cached.webp' | asset_siteimg -}}
{% elif os_path.exists(path ~ "/banners") -%}
{{ random_image("assets/images/" ~ SITE_NAME ~ "/banners") -}}
{% else -%}
{{ 'banner.webp' | asset_siteimg -}}
{% endif %}
{%- endmacro -%}
{%- macro random_image(path) -%}
{{- "/" ~ path ~ "/" ~ listdir('files/' ~ path)|random() ~ '?x=1' }}
{%- endmacro -%}