Skip to content

Commit

Permalink
fix(nuxt-picture): check before accessing index
Browse files Browse the repository at this point in the history
resolves #1448
  • Loading branch information
danielroe committed Aug 28, 2024
1 parent 6d87553 commit 917242a
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/runtime/components/NuxtPicture.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
>

<img
v-if="lastSource"
ref="imgEl"
v-bind="{
...baseAttrs,
...(isServer ? { onerror: 'this.setAttribute(\'data-error\', 1)' } : {}),
...imgAttrs,
}"
:src="sources[lastSourceIndex].src"
:sizes="sources[lastSourceIndex].sizes"
:srcset="sources[lastSourceIndex].srcset"
:src="lastSource.src"
:sizes="lastSource.sizes"
:srcset="lastSource.srcset"
>
</picture>
</template>
Expand Down Expand Up @@ -89,24 +90,30 @@ const sources = computed<Source[]>(() => {
})
})
const lastSourceIndex = computed(() => sources.value.length - 1)
const lastSource = computed(() => sources.value[sources.value.length - 1])
if (import.meta.server && props.preload) {
const link: NonNullable<Head['link']>[number] = {
rel: 'preload',
as: 'image',
imagesrcset: sources.value[0].srcset,
nonce: props.nonce,
...(typeof props.preload !== 'boolean' && props.preload.fetchPriority
? { fetchpriority: props.preload.fetchPriority }
: {}),
}
useHead({ link: () => {
const firstSource = sources.value[0]
if (!firstSource) {
return []
}
if (sources.value?.[0]?.sizes) {
link.imagesizes = sources.value[0].sizes
}
const link: NonNullable<Head['link']>[number] = {
rel: 'preload',
as: 'image',
imagesrcset: firstSource.srcset,
nonce: props.nonce,
...(typeof props.preload !== 'boolean' && props.preload?.fetchPriority
? { fetchpriority: props.preload.fetchPriority }
: {}),
}
useHead({ link: [link] })
if (sources.value?.[0]?.sizes) {
link.imagesizes = sources.value[0].sizes
}
return [link]
} })
}
// Only passdown supported <image> attributes
Expand Down

0 comments on commit 917242a

Please sign in to comment.