-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazy load embeds (especially tweets) (#322)
* lazy load tweets, tiktok embeds, and instagram embeds (manually attach scripts when loading) * don't lazy load unhandled embeds (because scripts won't run)
- Loading branch information
Showing
8 changed files
with
162 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
<script setup lang="ts"> | ||
const props = withDefaults(defineProps<{ | ||
options?: { | ||
root?: HTMLElement | ||
rootMargin?: string | ||
threshold?: number | number[] | ||
} | ||
}>(), { | ||
options: { | ||
const props = withDefaults(defineProps<{ | ||
options?: { | ||
root?: HTMLElement | ||
rootMargin?: string | ||
threshold?: number | number[] | ||
} | ||
}>(), { | ||
options() { | ||
return { | ||
root: null, | ||
rootMargin: '1000px 0px 0px 0px', | ||
threshold: 0 | ||
rootMargin: '0px 0px 400px 0px', | ||
threshold: 0, | ||
} | ||
}) | ||
const element = ref(null) | ||
const seen = ref(false) | ||
const onVisible = () => {seen.value = true} | ||
const onNotVisible = () => { /*do nothing*/ } | ||
useVisibilityTracking(element, onVisible, onNotVisible, props.options ) | ||
}, | ||
}) | ||
const element = ref(null) | ||
const seen = ref(false) | ||
function onVisible() { seen.value = true } | ||
function onNotVisible() { /* do nothing */ } | ||
useVisibilityTracking(element, onVisible, onNotVisible, props.options) | ||
</script> | ||
|
||
<template> | ||
<div ref="element"> | ||
<div v-if="seen"> | ||
<slot/> | ||
<slot /> | ||
</div> | ||
</div> | ||
</template> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,65 @@ | ||
<script setup lang="ts"> | ||
import { computed, ref } from 'vue' | ||
import { EmbedBlock } from '../../composables/types/StreamfieldBlock' | ||
import { computed } from 'vue' | ||
import type { EmbedBlock } from '../../composables/types/StreamfieldBlock' | ||
const props = defineProps<{ | ||
block: EmbedBlock | ||
}>() | ||
const embedType = computed(() => { | ||
if (props.block.value.embed.includes('blockquote class="twitter-tweet"')) { | ||
if (props.block.value.embed.includes('blockquote class="twitter-tweet"')) | ||
return 'twitter-tweet' | ||
} | ||
else if (props.block.value.embed.includes('https://www.youtube.com/embed')) { | ||
else if (props.block.value.embed.includes('blockquote class="tiktok-embed"')) | ||
return 'tiktok-embed' | ||
else if (props.block.value.embed.includes('blockquote class="instagram-media"')) | ||
return 'instagram-media' | ||
else if (props.block.value.embed.includes('https://www.youtube.com/embed')) | ||
return 'youtube-video' | ||
} | ||
return 'default' | ||
}) | ||
const minHeight = computed(() => { | ||
let height: string | ||
switch (embedType.value) { | ||
case 'tiktok-embed': | ||
height = '740' | ||
break | ||
case 'instagram-media': | ||
height = '620' | ||
break | ||
default: | ||
height = '200' | ||
} | ||
return height | ||
}) | ||
</script> | ||
|
||
<template> | ||
<StreamfieldEmbedTweet | ||
v-if="embedType === 'twitter-tweet'" | ||
:key="`${block.id}-embed-tweet`" | ||
:block="block" | ||
/> | ||
<StreamfieldEmbedDefault | ||
v-else | ||
:class="[{ youtube: embedType === 'youtube-video' }]" | ||
v-if="embedType === 'default' || embedType === 'youtube-video'" | ||
:key="`${block.id}-embed-default`" | ||
:class="[{ youtube: embedType === 'youtube-video' }]" | ||
:block="block" | ||
/> | ||
<LoadLazily | ||
v-else | ||
:style="`min-height: ${minHeight}px`" | ||
> | ||
<LazyStreamfieldEmbedTweet | ||
v-if="embedType === 'twitter-tweet'" | ||
:key="`${block.id}-embed-tweet`" | ||
:block="block" | ||
/> | ||
<LazyStreamfieldEmbedTiktok | ||
v-else-if="embedType === 'tiktok-embed'" | ||
:key="`${block.id}-tiktok-embed`" | ||
:block="block" | ||
/> | ||
<LazyStreamfieldEmbedInstagram | ||
v-else-if="embedType === 'instagram-media'" | ||
:key="`${block.id}-instagram-media`" | ||
:block="block" | ||
/> | ||
</LoadLazily> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<script setup lang="ts"> | ||
import { EmbedBlock } from '~~/composables/types/StreamfieldBlock'; | ||
import { ref } from 'vue' | ||
defineProps<{ | ||
block: EmbedBlock | ||
}>() | ||
const el = ref(null) | ||
onMounted(() => { | ||
const js = document.createElement('script') | ||
js.src = '//www.instagram.com/embed.js' | ||
el.value.appendChild(js) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div | ||
class="streamfield-embed streamfield-embed- instagram streamfield-paragraph mb-7" | ||
v-html="block.value.embed" | ||
ref="el" | ||
/> | ||
</template> | ||
|
||
<style lang="scss"> | ||
.streamfield-embed { | ||
.instagram-media { | ||
@include media('<lg') { | ||
margin: auto !important; | ||
} | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<script setup lang="ts"> | ||
import { EmbedBlock } from '~~/composables/types/StreamfieldBlock'; | ||
import { ref } from 'vue' | ||
defineProps<{ | ||
block: EmbedBlock | ||
}>() | ||
const el = ref(null) | ||
onMounted(() => { | ||
const js = document.createElement('script') | ||
js.src = 'https://www.tiktok.com/embed.js' | ||
el.value.appendChild(js) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div | ||
class="streamfield-embed streamfield-embed-tiktok streamfield-paragraph mb-7" | ||
v-html="block.value.embed" | ||
ref="el" | ||
/> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters