Skip to content

Commit

Permalink
fix: watch Twemoji props to avoid double fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Yizack committed Jul 15, 2024
1 parent b32e20e commit 671a19f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/runtime/components/Twemoji.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- eslint-disable vue/multi-word-component-names -->
<script setup lang="ts">
import { ref, computed, defineComponent, h, watchEffect } from 'vue'
import { ref, computed, defineComponent, h, watch } from 'vue'
import type { EmojiDefinition } from './../assets/emojis'
import { useState } from '#imports'

Expand Down Expand Up @@ -77,8 +77,8 @@ const cdn = 'https://cdn.jsdelivr.net/gh/jdecked/twemoji@latest/assets'
const emojiLinkPNG = computed(() => `${cdn}/72x72/${codePoint.value[parsed.value]}.png`)
const emojiLinkSVG = computed(() => `${cdn}/svg/${codePoint.value[parsed.value]}.svg`)

const fetchSVG = () => $fetch(emojiLinkSVG.value, { responseType: 'text' }).then(res => res as string).catch(() => undefined)
const svgTwemojis = useState('twemojis', () => ({}) as { [key: string]: { body: string } })
const fetchSVG = () => $fetch(emojiLinkSVG.value, { responseType: 'text' }).then(res => res as string).catch(() => null)
const svgTwemojis = useState('twemojis', () => ({}) as Record<string, { body: string }>)

const component = computed (() => {
if (!svgTwemojis.value[parsed.value]) return
Expand Down Expand Up @@ -108,15 +108,17 @@ const loadSVG = async () => {
codePoint.value[parsed.value] = split.join('-')
svgFetch = await fetchSVG()
}

if (!svgFetch) return
svgTwemojis.value[parsed.value] = {
body: svgFetch.replace(/<\/*svg[^>]*>/g, ''),
}

const svgBody = svgFetch.replace(/<\/*svg[^>]*>/g, '')
svgTwemojis.value[parsed.value] = { body: svgBody }
}
watchEffect(async () => {

watch(() => props, async () => {
codePoint.value[parsed.value] = parsed.value
!props.png && await loadSVG()
})
}, { deep: true })

!props.png && await loadSVG()
</script>
Expand Down

0 comments on commit 671a19f

Please sign in to comment.