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

YouTube placeholder cannot show some videos #234

Closed
serkodev opened this issue Sep 2, 2024 · 1 comment · Fixed by #376
Closed

YouTube placeholder cannot show some videos #234

serkodev opened this issue Sep 2, 2024 · 1 comment · Fixed by #376
Labels
bug Something isn't working

Comments

@serkodev
Copy link
Contributor

serkodev commented Sep 2, 2024

🐛 The bug

The current youtube placeholder url https://i.ytimg.com/vi_webp/${props.videoId}/sddefault.webp cannot show some videos.

vi_webp is not guaranteed to exist

https://i.ytimg.com/vi_webp/mJ8tq8AnNis/default.webp (video)

sddefault is not guaranteed to exist

https://i.ytimg.com/vi/jNQXAC9IVRw/sddefault.jpg (video)

🛠️ To reproduce

https://stackblitz.com/edit/nuxt-starter-lnbkmh?file=pages%2Findex.vue

🌈 Expected behavior

Expected the youtube placeholder can show all videos preview thumbnail image.

ℹ️ Additional context

No response

@serkodev serkodev added the bug Something isn't working label Sep 2, 2024
@codeflorist
Copy link

codeflorist commented Dec 11, 2024

I'm doing this to get the best possible quality/format during static site generation:

const defaultPlaceholder = ref<string | null | undefined>(`https://i.ytimg.com/vi/${props.videoId}/sddefault.jpg`)
const asyncKey = `youtube-placeholder:${props.videoId}`
const { data } = await useAsyncData(
	asyncKey,
	async () => {
		const possibleURLs = [
			`https://i.ytimg.com/vi_webp/${props.videoId}/maxresdefault.webp`,
			`https://i.ytimg.com/vi/${props.videoId}/maxresdefault.jpg`,
			`https://i.ytimg.com/vi_webp/${props.videoId}/sddefault.webp`,
			`https://i.ytimg.com/vi/${props.videoId}/sddefault.jpg`,
			`https://i.ytimg.com/vi_webp/${props.videoId}/hqdefault.webp`,
			`https://i.ytimg.com/vi/${props.videoId}/hqdefault.jpg`,
			`https://i.ytimg.com/vi_webp/${props.videoId}/mqdefault.webp`,
			`https://i.ytimg.com/vi/${props.videoId}/mqdefault.jpg`,
			`https://i.ytimg.com/vi_webp/${props.videoId}/default.web`,
			`https://i.ytimg.com/vi/${props.videoId}/default.jpg`,
		]
		for (const urlToTry of possibleURLs) {
			const response = await $fetch.raw(urlToTry, {
				method: 'HEAD',
				mode: 'no-cors',
				ignoreResponseError: true
			})
			if (response.status === 200) {
				return urlToTry
			}
		}
	}
)
if (data.value) {
	defaultPlaceholder.value = data.value
}

But it does not work on the client due to opaque requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants