@@ -11,31 +11,33 @@ const qs = (params: Record<string, string>) => {
1111}
1212
1313type ImageType = 'jpg' | 'webp'
14- // Define a type for video resolutions
15- type VideoResolution = 120 | 320 | 480 | 640 | 1280
1614
17- const resolutions : VideoResolution [ ] = [ 120 , 320 , 480 , 640 , 1280 ]
15+ const resolutions = [ 120 , 320 , 480 , 640 , 1280 ] as const
16+ type VideoResolution = ( typeof resolutions ) [ number ]
1817
1918const resolutionMap : Record < VideoResolution , string > = {
2019 120 : 'default' ,
2120 320 : 'mqdefault' ,
2221 480 : 'hqdefault' ,
2322 640 : 'sddefault' ,
2423 1280 : 'maxresdefault'
25- // 2k, 4k, 8k images don't seem to be available
26- // Source: https://longzero.com/articles/youtube-thumbnail-sizes-url/
24+ // 2k, 4k, 8k images don't seem to be available
25+ // Source: https://longzero.com/articles/youtube-thumbnail-sizes-url/
2726}
2827
29- // Function to get the poster URL based on the resolution type
28+ const resolutionSizes = resolutions
29+ . map ( ( resolution ) => `(max-width: ${ resolution } px) ${ resolution } px` )
30+ . join ( ', ' )
31+
3032function getPosterUrl (
3133 id : string ,
3234 resolution : VideoResolution = 480 ,
3335 type : ImageType = 'jpg'
3436) : string {
35- // Return the appropriate URL based on the image type
3637 if ( type === 'webp' ) {
3738 return `https://i.ytimg.com/vi_webp/${ id } /${ resolutionMap [ resolution ] } .webp`
3839 }
40+
3941 // Default to jpg
4042 return `https://i.ytimg.com/vi/${ id } /${ resolutionMap [ resolution ] } .jpg`
4143}
@@ -46,12 +48,6 @@ function generateSrcSet(id: string, type: ImageType = 'jpg'): string {
4648 . join ( ', ' )
4749}
4850
49- function generateSizes ( ) : string {
50- return resolutions
51- . map ( ( resolution ) => `(max-width: ${ resolution } px) ${ resolution } px` )
52- . join ( ', ' )
53- }
54-
5551export function LiteYouTubeEmbed ( {
5652 id,
5753 defaultPlay = false ,
@@ -107,13 +103,13 @@ export function LiteYouTubeEmbed({
107103 { /*
108104 'it seems pretty unlikely for a browser to support preloading but not WebP images'
109105 Source: https://blog.laurenashpole.com/post/658079409151016960
110- */ }
106+ */ }
111107 < link
112108 rel = 'preload'
113109 as = 'image'
114110 href = { getPosterUrl ( id ) }
115111 imageSrcSet = { generateSrcSet ( id , 'webp' ) }
116- imageSizes = { generateSizes ( ) }
112+ imageSizes = { resolutionSizes }
117113 />
118114
119115 { isPreconnected && (
@@ -128,7 +124,9 @@ export function LiteYouTubeEmbed({
128124
129125 { isPreconnected && adLinksPreconnect && (
130126 < >
131- { /* Not certain if these ad related domains are in the critical path. Could verify with domain-specific throttling. */ }
127+ { /* Not certain if these ad related domains are in the critical path.
128+ Could verify with domain-specific throttling.
129+ */ }
132130 < link rel = 'preconnect' href = 'https://static.doubleclick.net' />
133131 < link rel = 'preconnect' href = 'https://googleads.g.doubleclick.net' />
134132 </ >
@@ -149,7 +147,7 @@ export function LiteYouTubeEmbed({
149147 { /*
150148 Browsers which don't support srcSet will most likely not support webp too
151149 These browsers will then just get the default 480 size jpg
152- */ }
150+ */ }
153151 { resolutions . map ( ( resolution ) => (
154152 < source
155153 key = { resolution }
@@ -158,6 +156,7 @@ export function LiteYouTubeEmbed({
158156 type = 'image/webp'
159157 />
160158 ) ) }
159+
161160 < img
162161 src = { getPosterUrl ( id ) }
163162 className = 'notion-yt-thumbnail'
0 commit comments