{# requires global with handle 'favicon' containing asset field with handle 'faviconFile' #}
{# v2 #}
{% set range = range(100000, 900000) %}
{% cache globally %}
	{% set sizesIcon = [192, 48, 32, 16] %}
	{% set sizesAppleTouch = [180] %}
	{% if favicon is defined and favicon|length and favicon.one().kind == 'image' %}
		{# link icon #}
		{% for faviconSize in sizesIcon %}
			{% set icon = favicon.one() %}
			{% set shorterEdge = icon.width > icon.height ? icon.height : icon.width %}
			{% if shorterEdge >= faviconSize %}
				{% set faviconTransform = {
					width: faviconSize,
					height: faviconSize,
					quality: 100,
					position: 'center-center',
					format: 'png'
				} %}
				{{
					tag(
						'link',
						{
							rel: 'icon',
							sizes: faviconSize ~ 'x' ~ faviconSize,
							type: 'image/png',
							href: icon.getUrl(faviconTransform) ~ '?v=' ~ random(range)
						}
					)
				}}
			{% endif %}
		{% endfor %}
		{# link touch icon #}
		{% for faviconSize in sizesAppleTouch %}
			{% set icon = favicon.one() %}
			{% set shorterEdge = icon.width > icon.height ? icon.height : icon.width %}
			{% if shorterEdge >= faviconSize %}
				{% set faviconTransform = {
					width: faviconSize,
					height: faviconSize,
					quality: 100,
					position: 'center-center',
					format: 'png'
				} %}
				{{
					tag(
						'link',
						{
							rel: 'apple-touch-icon',
							sizes: faviconSize ~ 'x' ~ faviconSize,
							href: icon.getUrl(faviconTransform) ~ '?v=' ~ random(range)
						}
					)
				}}
			{% endif %}
		{% endfor %}
		{% set favIcoImage = craft.assets().id(20).one() %}
		{{
			tag(
				'link',
				{
					rel: 'shortcut icon',
					type: 'image/x-icon',
					href: favIcoImage.url ~ '?v=' ~ random(range)
				}
			)
		}}
	{% endif %}
{% endcache %}
