Skip to content

Commit cf96c66

Browse files
committed
Lazy-load CldVideoPlayer on click to avoid loading cld-video-player.min.js on every page visit
1 parent cce28e8 commit cf96c66

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

components/LazyVideoPlayer.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { useState } from 'react'
2+
import { CldVideoPlayer } from 'next-cloudinary'
3+
4+
export function LazyVideoPlayer({ src, width, height, poster }) {
5+
const [playing, setPlaying] = useState(false)
6+
7+
if (playing) {
8+
return <CldVideoPlayer width={width} height={height} src={src} autoPlay="always" />
9+
}
10+
11+
return (
12+
<div
13+
onClick={() => setPlaying(true)}
14+
style={{
15+
position: 'relative',
16+
cursor: 'pointer',
17+
background: '#000',
18+
aspectRatio: `${width} / ${height}`,
19+
overflow: 'hidden',
20+
}}
21+
>
22+
{poster && (
23+
<img
24+
src={poster}
25+
alt="Video tutorial preview"
26+
width={width}
27+
height={height}
28+
loading="lazy"
29+
style={{ width: '100%', height: 'auto', display: 'block' }}
30+
/>
31+
)}
32+
<div
33+
style={{
34+
position: 'absolute',
35+
inset: 0,
36+
display: 'flex',
37+
alignItems: 'center',
38+
justifyContent: 'center',
39+
}}
40+
>
41+
<div
42+
style={{
43+
width: '64px',
44+
height: '64px',
45+
background: 'rgba(0, 0, 0, 0.65)',
46+
borderRadius: '50%',
47+
display: 'flex',
48+
alignItems: 'center',
49+
justifyContent: 'center',
50+
fontSize: '24px',
51+
color: '#fff',
52+
userSelect: 'none',
53+
}}
54+
>
55+
56+
</div>
57+
</div>
58+
</div>
59+
)
60+
}

pages/index.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CldVideoPlayer } from "next-cloudinary";
1+
import { LazyVideoPlayer } from "../components/LazyVideoPlayer";
22

33
# WebPerf Snippets
44

@@ -76,10 +76,11 @@ Save frequently used snippets for quick access:
7676
### Video tutorial
7777

7878
<div style={{ marginTop: "20px", display: "block" }}>
79-
<CldVideoPlayer
79+
<LazyVideoPlayer
8080
width="1920"
8181
height="1080"
8282
src="webperf-snippets/devtools-new-snippet"
83+
poster="https://res.cloudinary.com/nucliweb/video/upload/so_auto,f_jpg,q_auto/webperf-snippets/devtools-new-snippet.jpg"
8384
/>
8485
</div>
8586

0 commit comments

Comments
 (0)