Skip to content

Commit 917242a

Browse files
committedAug 28, 2024··
fix(nuxt-picture): check before accessing index
resolves #1448
1 parent 6d87553 commit 917242a

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed
 

Diff for: ‎src/runtime/components/NuxtPicture.vue

+24-17
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
>
1010

1111
<img
12+
v-if="lastSource"
1213
ref="imgEl"
1314
v-bind="{
1415
...baseAttrs,
1516
...(isServer ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {}),
1617
...imgAttrs,
1718
}"
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"
2122
>
2223
</picture>
2324
</template>
@@ -89,24 +90,30 @@ const sources = computed<Source[]>(() => {
8990
})
9091
})
9192
92-
const lastSourceIndex = computed(() => sources.value.length - 1)
93+
const lastSource = computed(() => sources.value[sources.value.length - 1])
9394
9495
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+
}
104101
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+
}
108111
109-
useHead({ link: [link] })
112+
if (sources.value?.[0]?.sizes) {
113+
link.imagesizes = sources.value[0].sizes
114+
}
115+
return [link]
116+
} })
110117
}
111118
112119
// Only passdown supported <image> attributes

0 commit comments

Comments
 (0)
Please sign in to comment.