Skip to content

Commit f82637e

Browse files
committed
only load sponsor data once
1 parent a20861c commit f82637e

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

.vitepress/theme/components/SponsorsGroup.vue

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
<script lang="ts">
2-
// shared data across instances so we load only once
3-
let data = $ref<SponsorData>()
4-
const base = `https://sponsors.vuejs.org`
5-
</script>
6-
7-
<script setup lang="ts">
8-
import { onMounted, onUnmounted } from 'vue'
9-
102
interface Sponsor {
113
url: string
124
img: string
@@ -22,6 +14,16 @@ interface SponsorData {
2214
bronze: Sponsor[]
2315
}
2416
17+
// shared data across instances so we load only once
18+
let data = $ref<SponsorData>()
19+
let pending = false
20+
21+
const base = `https://sponsors.vuejs.org`
22+
</script>
23+
24+
<script setup lang="ts">
25+
import { onMounted, onUnmounted } from 'vue'
26+
2527
const { tier, placement = 'aside' } = defineProps<{
2628
tier: keyof SponsorData
2729
placement?: 'aside' | 'page' | 'landing'
@@ -32,17 +34,21 @@ let visible = $ref(false)
3234
3335
onMounted(async () => {
3436
// only render when entering view
35-
const observer = new IntersectionObserver((entries) => {
36-
if (entries[0].isIntersecting) {
37-
visible = true
38-
observer.disconnect()
39-
}
40-
}, { rootMargin: '0px 0px 300px 0px' })
37+
const observer = new IntersectionObserver(
38+
(entries) => {
39+
if (entries[0].isIntersecting) {
40+
visible = true
41+
observer.disconnect()
42+
}
43+
},
44+
{ rootMargin: '0px 0px 300px 0px' }
45+
)
4146
observer.observe(container)
4247
onUnmounted(() => observer.disconnect())
4348
4449
// load data
45-
if (!data) {
50+
if (!pending) {
51+
pending = true
4652
data = await (await fetch(`${base}/data.json`)).json()
4753
}
4854
})
@@ -76,7 +82,8 @@ onMounted(async () => {
7682
v-if="placement !== 'page' && tier !== 'special'"
7783
href="/sponsor/"
7884
class="sponsor-item action"
79-
>Your logo</a>
85+
>Your logo</a
86+
>
8087
</div>
8188
</template>
8289

0 commit comments

Comments
 (0)