Skip to content

Commit 444d6fc

Browse files
fix(nuxt): hash placeholder decoding in client
1 parent 667f281 commit 444d6fc

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,41 +57,44 @@ const props = withDefaults(
5757
5858
const { unlazy } = useRuntimeConfig().public
5959
const target = ref<HTMLImageElement | undefined>()
60+
let isLoaded = false
6061
let cleanup: () => void | undefined
61-
// const now = performance.now()
6262
6363
// SSR-decoded BlurHash as PNG data URI placeholder image
64-
const loadSSR = process.server && (props.ssr ?? unlazy.ssr)
6564
const pngPlaceholder = computed(() => {
66-
if (!loadSSR || (!props.blurhash && !props.thumbhash))
67-
return
68-
69-
return props.blurhash
70-
? createPngDataUriFromBlurHash(props.blurhash, {
71-
size: props.placeholderSize || unlazy.placeholderSize,
72-
ratio: props.placeholderRatio,
73-
})
74-
: createPngDataUriFromThumbHash(props.thumbhash!)
65+
if (
66+
(process.client || (props.ssr ?? unlazy.ssr))
67+
&& (props.blurhash || props.thumbhash)
68+
) {
69+
return props.blurhash
70+
? createPngDataUriFromBlurHash(props.blurhash, {
71+
size: props.placeholderSize || unlazy.placeholderSize,
72+
ratio: props.placeholderRatio,
73+
})
74+
: createPngDataUriFromThumbHash(props.thumbhash!)
75+
}
7576
})
7677
77-
// if (loadSSR && process.dev)
78-
// console.log(`[unlazy] BlurHash decoded in ${performance.now() - now}ms`)
79-
8078
onMounted(() => {
8179
if (!target.value)
8280
return
8381
8482
watchEffect(() => {
8583
cleanup?.()
8684
85+
if (pngPlaceholder.value && !isLoaded)
86+
target.value!.src = pngPlaceholder.value
87+
8788
if (!props.lazyLoad)
8889
return
8990
90-
cleanup = lazyLoad(target.value, {
91+
cleanup = lazyLoad(target.value!, {
9192
hash: props.thumbhash || props.blurhash,
9293
hashType: props.thumbhash ? 'thumbhash' : 'blurhash',
9394
placeholderSize: props.placeholderSize || unlazy.placeholderSize,
9495
})
96+
97+
isLoaded = true
9598
})
9699
})
97100

0 commit comments

Comments
 (0)