@@ -11,31 +11,33 @@ const qs = (params: Record<string, string>) => {
11
11
}
12
12
13
13
type ImageType = 'jpg' | 'webp'
14
- // Define a type for video resolutions
15
- type VideoResolution = 120 | 320 | 480 | 640 | 1280
16
14
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 ]
18
17
19
18
const resolutionMap : Record < VideoResolution , string > = {
20
19
120 : 'default' ,
21
20
320 : 'mqdefault' ,
22
21
480 : 'hqdefault' ,
23
22
640 : 'sddefault' ,
24
23
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/
27
26
}
28
27
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
+
30
32
function getPosterUrl (
31
33
id : string ,
32
34
resolution : VideoResolution = 480 ,
33
35
type : ImageType = 'jpg'
34
36
) : string {
35
- // Return the appropriate URL based on the image type
36
37
if ( type === 'webp' ) {
37
38
return `https://i.ytimg.com/vi_webp/${ id } /${ resolutionMap [ resolution ] } .webp`
38
39
}
40
+
39
41
// Default to jpg
40
42
return `https://i.ytimg.com/vi/${ id } /${ resolutionMap [ resolution ] } .jpg`
41
43
}
@@ -46,12 +48,6 @@ function generateSrcSet(id: string, type: ImageType = 'jpg'): string {
46
48
. join ( ', ' )
47
49
}
48
50
49
- function generateSizes ( ) : string {
50
- return resolutions
51
- . map ( ( resolution ) => `(max-width: ${ resolution } px) ${ resolution } px` )
52
- . join ( ', ' )
53
- }
54
-
55
51
export function LiteYouTubeEmbed ( {
56
52
id,
57
53
defaultPlay = false ,
@@ -107,13 +103,13 @@ export function LiteYouTubeEmbed({
107
103
{ /*
108
104
'it seems pretty unlikely for a browser to support preloading but not WebP images'
109
105
Source: https://blog.laurenashpole.com/post/658079409151016960
110
- */ }
106
+ */ }
111
107
< link
112
108
rel = 'preload'
113
109
as = 'image'
114
110
href = { getPosterUrl ( id ) }
115
111
imageSrcSet = { generateSrcSet ( id , 'webp' ) }
116
- imageSizes = { generateSizes ( ) }
112
+ imageSizes = { resolutionSizes }
117
113
/>
118
114
119
115
{ isPreconnected && (
@@ -128,7 +124,9 @@ export function LiteYouTubeEmbed({
128
124
129
125
{ isPreconnected && adLinksPreconnect && (
130
126
< >
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
+ */ }
132
130
< link rel = 'preconnect' href = 'https://static.doubleclick.net' />
133
131
< link rel = 'preconnect' href = 'https://googleads.g.doubleclick.net' />
134
132
</ >
@@ -149,7 +147,7 @@ export function LiteYouTubeEmbed({
149
147
{ /*
150
148
Browsers which don't support srcSet will most likely not support webp too
151
149
These browsers will then just get the default 480 size jpg
152
- */ }
150
+ */ }
153
151
{ resolutions . map ( ( resolution ) => (
154
152
< source
155
153
key = { resolution }
@@ -158,6 +156,7 @@ export function LiteYouTubeEmbed({
158
156
type = 'image/webp'
159
157
/>
160
158
) ) }
159
+
161
160
< img
162
161
src = { getPosterUrl ( id ) }
163
162
className = 'notion-yt-thumbnail'
0 commit comments