|
9 | 9 | >
|
10 | 10 |
|
11 | 11 | <img
|
| 12 | + v-if="lastSource" |
12 | 13 | ref="imgEl"
|
13 | 14 | v-bind="{
|
14 | 15 | ...baseAttrs,
|
15 | 16 | ...(isServer ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {}),
|
16 | 17 | ...imgAttrs,
|
17 | 18 | }"
|
18 |
| - :src="sources[lastSourceIndex].src" |
19 |
| - :sizes="sources[lastSourceIndex].sizes" |
20 |
| - :srcset="sources[lastSourceIndex].srcset" |
| 19 | + :src="lastSource.src" |
| 20 | + :sizes="lastSource.sizes" |
| 21 | + :srcset="lastSource.srcset" |
21 | 22 | >
|
22 | 23 | </picture>
|
23 | 24 | </template>
|
@@ -89,24 +90,30 @@ const sources = computed<Source[]>(() => {
|
89 | 90 | })
|
90 | 91 | })
|
91 | 92 |
|
92 |
| -const lastSourceIndex = computed(() => sources.value.length - 1) |
| 93 | +const lastSource = computed(() => sources.value[sources.value.length - 1]) |
93 | 94 |
|
94 | 95 | if (import.meta.server && props.preload) {
|
95 |
| - const link: NonNullable<Head['link']>[number] = { |
96 |
| - rel: 'preload', |
97 |
| - as: 'image', |
98 |
| - imagesrcset: sources.value[0].srcset, |
99 |
| - nonce: props.nonce, |
100 |
| - ...(typeof props.preload !== 'boolean' && props.preload.fetchPriority |
101 |
| - ? { fetchpriority: props.preload.fetchPriority } |
102 |
| - : {}), |
103 |
| - } |
| 96 | + useHead({ link: () => { |
| 97 | + const firstSource = sources.value[0] |
| 98 | + if (!firstSource) { |
| 99 | + return [] |
| 100 | + } |
104 | 101 |
|
105 |
| - if (sources.value?.[0]?.sizes) { |
106 |
| - link.imagesizes = sources.value[0].sizes |
107 |
| - } |
| 102 | + const link: NonNullable<Head['link']>[number] = { |
| 103 | + rel: 'preload', |
| 104 | + as: 'image', |
| 105 | + imagesrcset: firstSource.srcset, |
| 106 | + nonce: props.nonce, |
| 107 | + ...(typeof props.preload !== 'boolean' && props.preload?.fetchPriority |
| 108 | + ? { fetchpriority: props.preload.fetchPriority } |
| 109 | + : {}), |
| 110 | + } |
108 | 111 |
|
109 |
| - useHead({ link: [link] }) |
| 112 | + if (sources.value?.[0]?.sizes) { |
| 113 | + link.imagesizes = sources.value[0].sizes |
| 114 | + } |
| 115 | + return [link] |
| 116 | + } }) |
110 | 117 | }
|
111 | 118 |
|
112 | 119 | // Only passdown supported <image> attributes
|
|
0 commit comments