Skip to content

Commit 6903fe8

Browse files
fix(vue,nuxt): calc sizes when preload is set
Fixes #27
1 parent 479fb33 commit 6903fe8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/nuxt/src/runtime/components/UnLazyImage.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { createPlaceholderFromHash, lazyLoad, loadImage } from 'unlazy'
2+
import { createPlaceholderFromHash, lazyLoad, loadImage, updateSizesAttribute } from 'unlazy'
33
import type { ImgHTMLAttributes } from 'vue'
44
import type { ModuleOptions } from '../../module'
55
import { computed, onBeforeUnmount, ref, useRuntimeConfig, watchEffect } from '#imports'
@@ -104,6 +104,15 @@ watchEffect(() => {
104104
}
105105
106106
if (props.preload) {
107+
updateSizesAttribute(target.value)
108+
109+
// Calculate the `sizes` attribute for sources inside a `<picture>` element
110+
if (target.value.parentElement?.tagName.toLowerCase() === 'picture') {
111+
[...target.value.parentElement.getElementsByTagName('source')].forEach(
112+
sourceTag => updateSizesAttribute(sourceTag),
113+
)
114+
}
115+
107116
loadImage(target.value)
108117
return
109118
}

packages/vue/src/components/UnLazyImage.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { onBeforeUnmount, ref, watchEffect } from 'vue'
3-
import { lazyLoad, loadImage } from 'unlazy'
3+
import { lazyLoad, loadImage, updateSizesAttribute } from 'unlazy'
44
import type { ImgHTMLAttributes } from 'vue'
55
66
const props = defineProps<{
@@ -38,6 +38,15 @@ watchEffect(() => {
3838
return
3939
4040
if (props.preload) {
41+
updateSizesAttribute(target.value)
42+
43+
// Calculate the `sizes` attribute for sources inside a `<picture>` element
44+
if (target.value.parentElement?.tagName.toLowerCase() === 'picture') {
45+
[...target.value.parentElement.getElementsByTagName('source')].forEach(
46+
sourceTag => updateSizesAttribute(sourceTag),
47+
)
48+
}
49+
4150
loadImage(target.value)
4251
return
4352
}

0 commit comments

Comments
 (0)