Skip to content

Commit 39b4dbc

Browse files
fix: Stop StyledVideo from loading both video files (#875)
* Limits the ability of the StyledVideo component to load in both video files * fix: Empty commit to trigger CI
1 parent 48720b3 commit 39b4dbc

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/components/Atoms/AmbientVideo/AmbientVideo.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
// Normalise webpack module object ({ default }) or string to video URL
2121
const normaliseSrc = value => (typeof value === 'string' ? value : value?.default);
2222

23+
const belowLBreakpointMediaString = `(max-width: ${breakpointValues.L - 1}px)`;
24+
2325
const AmbientVideo = ({
2426
src,
2527
srcMobile,
@@ -32,8 +34,9 @@ const AmbientVideo = ({
3234
const videoRef = useRef(null);
3335
const [isPlaying, setIsPlaying] = useState(true);
3436
const isBelowL = useMediaQuery({ maxWidth: breakpointValues.L - 1 });
35-
const rawSrc = srcMobile && isBelowL ? srcMobile : src;
36-
const effectiveSrc = normaliseSrc(rawSrc);
37+
const desktopSrc = normaliseSrc(src);
38+
const mobileSrc = srcMobile ? normaliseSrc(srcMobile) : null;
39+
// <video> only supports one poster URL; keep JS breakpoint for poster / fallback stills.
3740
const rawPoster = posterMobile && isBelowL ? posterMobile : poster;
3841
const effectivePoster = rawPoster ? normaliseSrc(rawPoster) : undefined;
3942

@@ -72,7 +75,6 @@ const AmbientVideo = ({
7275
<VideoWrapper>
7376
<StyledVideo
7477
ref={videoRef}
75-
src={effectiveSrc}
7678
poster={effectivePoster}
7779
controls={showFullControls}
7880
controlsList={showFullControls ? 'nodownload nofullscreen noremoteplayback' : undefined}
@@ -82,6 +84,10 @@ const AmbientVideo = ({
8284
onPlay={handlePlay}
8385
onPause={handlePause}
8486
>
87+
{mobileSrc ? (
88+
<source src={mobileSrc} type="video/mp4" media={belowLBreakpointMediaString} />
89+
) : null}
90+
<source src={desktopSrc} type="video/mp4" />
8591
{effectivePoster ? (
8692
<FallbackImg src={effectivePoster} alt="Video playback not supported" />
8793
) : (

src/components/Molecules/PictureOrVideo/__snapshots__/PictureOrVideo.test.js.snap

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,16 @@ exports[`renders AmbientVideo when video props are provided correctly 1`] = `
122122
onPlay={[Function]}
123123
playsInline={true}
124124
poster="//images.ctfassets.net/zsfivwzfgl3t/Yq59XdwwQgjNOxky93K1Q/17c2d80dce99067b0b3508f33075cbe3/funding_4-3_2x.jpg?w=200&h=150&q=50 200w,//images.ctfassets.net/zsfivwzfgl3t/Yq59XdwwQgjNOxky93K1Q/17c2d80dce99067b0b3508f33075cbe3/funding_4-3_2x.jpg?w=400&h=300&q=50 400w,//images.ctfassets.net/zsfivwzfgl3t/Yq59XdwwQgjNOxky93K1Q/17c2d80dce99067b0b3508f33075cbe3/funding_4-3_2x.jpg?w=800&h=600&q=50 800w,//images.ctfassets.net/zsfivwzfgl3t/Yq59XdwwQgjNOxky93K1Q/17c2d80dce99067b0b3508f33075cbe3/funding_4-3_2x.jpg?w=1200&h=900&q=50 1200w,//images.ctfassets.net/zsfivwzfgl3t/Yq59XdwwQgjNOxky93K1Q/17c2d80dce99067b0b3508f33075cbe3/funding_4-3_2x.jpg?w=1440&h=1080&q=50 1440w"
125-
src="https://example.com/video-desktop.mp4"
126125
>
126+
<source
127+
media="(max-width: 1023px)"
128+
src="https://example.com/video-mobile.mp4"
129+
type="video/mp4"
130+
/>
131+
<source
132+
src="https://example.com/video-desktop.mp4"
133+
type="video/mp4"
134+
/>
127135
<img
128136
alt="Video playback not supported"
129137
className="c3"

0 commit comments

Comments
 (0)