Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update youtube placeholder default image url #235

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions playground/pages/third-parties/youtube/nuxt-scripts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const videoid = ref('d_IFKP1Ofq0')
function changeVideo() {
videoid.value = 'N8siuNjyV7A'
}

function changeVideoFallback() {
videoid.value = 'jNQXAC9IVRw'
}
</script>

<template>
Expand All @@ -28,5 +32,12 @@ function changeVideo() {
>
change video
</UButton>

<UButton
class="ml-5"
@click="changeVideoFallback"
>
change video needs fallback
</UButton>
</div>
</template>
10 changes: 9 additions & 1 deletion src/runtime/components/ScriptYouTubePlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ const rootAttrs = computed(() => {
}) as HTMLAttributes
})

const fallbackPlaceHolder = computed(() => `https://i.ytimg.com/vi/${props.videoId}/hqdefault.jpg`)
const placeholder = computed(() => `https://i.ytimg.com/vi_webp/${props.videoId}/sddefault.webp`)
const isFallbackPlaceHolder = ref(false)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type YouTubeThumbnailType =
// 120x90
'1' | '2' | '3' | 'default' |
// 320x180
'mq1' | 'mq2' | 'mq3' | 'mqdefault' |
// 480x360
'0' | 'hq1' | 'hq2' | 'hq3' | 'hqdefault' |
// 640x480
'sd1' | 'sd2' | 'sd3' | 'sddefault' |
// 1280x720
'hq720' |
// 1920x1080
'maxresdefault'
function getYouTubeThumbnail(type: YouTubeThumbnailType, webp = false) {
return `https://i.ytimg.com/${webp ? 'vi_webp' : 'vi'}/${props.videoId}/${type}.${webp ? 'webp' : 'jpg'}`
}

Just draft a function to get YouTube thumbnail here. I think we can use this for placeholder or provide for user to use?

if (import.meta.server) {
// dns-prefetch https://i.vimeocdn.com
Expand All @@ -146,14 +148,20 @@ if (import.meta.server) {

const placeholderAttrs = computed(() => {
return defu(props.placeholderAttrs, {
src: placeholder.value,
src: isFallbackPlaceHolder.value ? fallbackPlaceHolder.value : placeholder.value,
alt: '',
loading: props.aboveTheFold ? 'eager' : 'lazy',
style: {
width: '100%',
objectFit: 'contain',
height: '100%',
},
onLoad(payload) {
const img = payload.target as HTMLImageElement
if (img.naturalWidth === 120 && img.naturalHeight === 90) {
isFallbackPlaceHolder.value = true
}
},
} satisfies ImgHTMLAttributes)
})
</script>
Expand Down