Skip to content

Commit

Permalink
feat: lazy load images (#1969)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Apr 26, 2023
1 parent 1ceb3e2 commit 23c1dfe
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 48 deletions.
44 changes: 3 additions & 41 deletions components/common/CommonBlurhash.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<script setup lang="ts">
import { decode } from 'blurhash'
const { blurhash, src, srcset, shouldLoadImage = true } = defineProps<{
blurhash?: string | null | undefined
const { blurhash = '', src, srcset, shouldLoadImage = true } = defineProps<{
blurhash?: string
src: string
srcset?: string
shouldLoadImage?: boolean
Expand All @@ -11,44 +9,8 @@ const { blurhash, src, srcset, shouldLoadImage = true } = defineProps<{
defineOptions({
inheritAttrs: false,
})
const isLoaded = ref(false)
const placeholderSrc = $computed(() => {
if (!blurhash)
return ''
const pixels = decode(blurhash, 32, 32)
return getDataUrlFromArr(pixels, 32, 32)
})
function loadImage() {
const img = document.createElement('img')
img.onload = () => {
isLoaded.value = true
}
img.src = src
if (srcset)
img.srcset = srcset
setTimeout(() => {
isLoaded.value = true
}, 3_000)
}
onMounted(() => {
if (shouldLoadImage)
loadImage()
})
watch(() => shouldLoadImage, () => {
if (shouldLoadImage)
loadImage()
})
</script>

<template>
<img v-if="isLoaded || !placeholderSrc" v-bind="$attrs" :src="src" :srcset="srcset">
<img v-else v-bind="$attrs" :src="placeholderSrc">
<UnLazyImage v-bind="$attrs" :blurhash="blurhash" :src="src" :src-set="srcset" :lazy-load="shouldLoadImage" auto-sizes />
</template>
2 changes: 1 addition & 1 deletion components/status/StatusAttachment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ watch(shouldLoadAttachment, () => {
@click="!shouldLoadAttachment ? loadAttachment() : openMediaPreview(attachments ? attachments : [attachment], attachments?.indexOf(attachment) || 0)"
>
<CommonBlurhash
:blurhash="attachment.blurhash"
:blurhash="attachment.blurhash || ''"
class="status-attachment-image"
:src="src"
:srcset="srcset"
Expand Down
4 changes: 4 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default defineNuxtConfig({
'@vue-macros/nuxt',
'@nuxtjs/i18n',
'@nuxtjs/color-mode',
'@unlazy/nuxt',
'nuxt-vitest',
...(isDevelopment || isWindows) ? [] : ['nuxt-security'],
'~/modules/emoji-mart-translation',
Expand Down Expand Up @@ -248,6 +249,9 @@ export default defineNuxtConfig({
staleDep: {
packageManager: 'pnpm',
},
unlazy: {
ssr: false,
},
})

declare global {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"@types/js-yaml": "^4.0.5",
"@types/prettier": "^2.7.2",
"@types/wicg-file-system-access": "^2020.9.6",
"@unlazy/nuxt": "^0.8.8",
"bumpp": "^9.1.0",
"consola": "^3.0.1",
"eslint": "^8.38.0",
Expand Down
54 changes: 48 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 23c1dfe

Please sign in to comment.