Skip to content

Commit f5b5414

Browse files
committed
Always send CORS header on video requests in prod
1 parent 93aa63c commit f5b5414

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

dotcom-rendering/src/components/SelfHostedVideoPlayer.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,10 @@ type Props = {
131131
/**
132132
* Note that in React 19, forwardRef is no longer necessary:
133133
* https://react.dev/reference/react/forwardRef
134-
*/
135-
/**
134+
*
136135
* NB: To develop the video player locally, use `https://r.thegulocal.com/` instead of `localhost`.
137136
* This is required because CORS restrictions prevent accessing the subtitles and video file from localhost.
138137
*/
139-
140138
export const SelfHostedVideoPlayer = forwardRef(
141139
(
142140
{
@@ -185,6 +183,16 @@ export const SelfHostedVideoPlayer = forwardRef(
185183
showPlayIcon ? 'play' : 'pause'
186184
}-${atomId}`;
187185

186+
/**
187+
* We need CORS enabled to show subtitles on a video as it allows access to the VTT file.
188+
*
189+
* We don't want a client to not request CORS headers on a video request, then to request
190+
* the same video with CORS headers, as the response could be cached without the headers, which
191+
* will result in the video failing to load. Therefore, we always request CORS headers in production.
192+
*/
193+
const requestCORS =
194+
showSubtitles || process.env.NODE_ENV === 'production';
195+
188196
return (
189197
<>
190198
{/* eslint-disable-next-line jsx-a11y/media-has-caption -- Not all videos require captions. */}
@@ -194,7 +202,7 @@ export const SelfHostedVideoPlayer = forwardRef(
194202
videoStyles(width, height),
195203
showSubtitles && subtitleStyles(subtitleSize),
196204
]}
197-
crossOrigin={showSubtitles ? 'anonymous' : undefined}
205+
crossOrigin={requestCORS ? 'anonymous' : undefined}
198206
ref={ref}
199207
tabIndex={0}
200208
data-testid="self-hosted-video-player"

0 commit comments

Comments
 (0)