Skip to content

Commit 99cc7e4

Browse files
perf(nuxt): cache last hash to omit re-generation
1 parent 19a45c4 commit 99cc7e4

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import { createPlaceholderFromHash, lazyLoad } from 'unlazy'
33
import type { ImgHTMLAttributes } from 'vue'
4-
import { onBeforeUnmount, ref, useRuntimeConfig, watchEffect } from '#imports'
4+
import { computed, onBeforeUnmount, ref, useRuntimeConfig, watchEffect } from '#imports'
55
66
const props = withDefaults(
77
defineProps<{
@@ -53,13 +53,13 @@ const props = withDefaults(
5353
},
5454
)
5555
56-
// SSR-decoded BlurHash as PNG data URI placeholder image
5756
const { unlazy } = useRuntimeConfig().public
58-
const hash = props.thumbhash || props.blurhash
59-
const loadSSR = process.server && (props.ssr ?? unlazy.ssr)
60-
const pngPlaceholder = (loadSSR && hash)
57+
const hash = computed(() => props.thumbhash || props.blurhash)
58+
59+
// SSR-decoded BlurHash as PNG data URI placeholder image
60+
const pngPlaceholder = (process.server && (props.ssr ?? unlazy.ssr) && hash.value)
6161
? createPlaceholderFromHash({
62-
hash,
62+
hash: hash.value,
6363
hashType: props.thumbhash ? 'thumbhash' : 'blurhash',
6464
size: props.placeholderSize || unlazy.placeholderSize,
6565
ratio: props.placeholderRatio,
@@ -68,18 +68,18 @@ const pngPlaceholder = (loadSSR && hash)
6868
6969
const target = ref<HTMLImageElement | undefined>()
7070
let cleanup: () => void | undefined
71-
let hasPlaceholder = false
71+
let lastHash: string | undefined
7272
7373
watchEffect(() => {
7474
cleanup?.()
7575
7676
if (!target.value)
7777
return
7878
79-
if (!hasPlaceholder) {
79+
if (hash.value && hash.value !== lastHash) {
8080
const placeholder = createPlaceholderFromHash({
8181
image: target.value,
82-
hash: props.thumbhash || props.blurhash,
82+
hash: hash.value,
8383
hashType: props.thumbhash ? 'thumbhash' : 'blurhash',
8484
size: props.placeholderSize || unlazy.placeholderSize,
8585
ratio: props.placeholderRatio,
@@ -88,7 +88,7 @@ watchEffect(() => {
8888
if (placeholder)
8989
target.value.src = placeholder
9090
91-
hasPlaceholder = true
91+
lastHash = hash.value
9292
}
9393
9494
if (!props.lazyLoad)

0 commit comments

Comments
 (0)