HTML Video CORS: Fix Blocked Media and Captions
Published
If a video plays but its remote captions do not load, inspect the video and caption requests separately. They can fail for different reasons. For cross-origin captions, the video element needs a CORS setting and the remote responses need permission for your page’s origin.
Find the request named in the error
Open the affected page with the browser’s Console and Network panels visible. Reload, start playback, and enable captions. Copy the URL named in the CORS error, then find that request in Network. Check whether it is the MP4, a WebVTT file, or something else.
For a page at https://example.com/lesson/, a file at https://media.example.com/lesson.en.vtt is on another origin. A different scheme, hostname, or port changes the origin. Opening the file successfully in its own tab does not establish that the browser can use it from your page. MDN’s CORS guide explains how servers permit cross-origin requests.
Check the response status and body too. A missing caption path can return your homepage with a 200 status; an expired media link may return an access error. Correct those problems before changing CORS headers. The playback troubleshooting guide covers URL, codec, and autoplay failures.
Use anonymous CORS for public media and captions
A plain cross-origin video can play without a crossorigin attribute. Adding crossorigin="anonymous" changes the media fetch to CORS mode, without sending credentials to the other origin. The media server must then return an appropriate CORS response. MDN’s media crossOrigin reference documents these modes.
For a remote caption track, put the attribute on the parent <video>, not the <track>. MDN’s track reference describes the cross-origin requirement. Here is a public, credential-free example:
<video controls playsinline crossorigin="anonymous"
preload="metadata" width="1280" height="720"
style="display:block;width:100%;height:auto">
<source src="https://media.example.com/lesson.mp4"
type="video/mp4" />
<track kind="captions"
src="https://media.example.com/lesson.en.vtt"
srclang="en" label="English" default />
</video>
Replace the URLs and dimensions with your own. Configure the attribute before loading the media; placing it in the initial markup avoids changing fetch mode after a request has started. Use a reviewed VTT file. The caption guide covers its syntax and timing.
If the video stops loading after this change, inspect its response as well as the captions. A caption server with correct headers cannot supply CORS permission for an MP4 served elsewhere.
Set permission on the server returning the file
For a page whose origin is https://example.com, a caption response can include:
Access-Control-Allow-Origin: https://example.com
Vary: Origin
Content-Type: text/vtt; charset=utf-8
These are response headers from the caption host or CDN. Adding them to the page’s HTML, or to an unrelated server route, does not change the file’s response. For the MP4, use its correct media type, such as video/mp4, and the corresponding CORS permission.
The allowed origin is a scheme, hostname, and port where applicable, without a page path or trailing slash. Use one allowed origin in the header, not a comma-separated list. For public resources requested without credentials, Access-Control-Allow-Origin: * is another option. MDN’s header reference explains these values and caching with Vary: Origin.
If your server chooses the response origin from an allowlist, make sure the CDN caches the appropriate response for each request origin. Retest the public CDN URL after changing the policy; an origin-server response alone does not confirm the cached response has changed.
Keep cookie-protected media as a separate setup
If the media deliberately requires cross-origin cookies, the anonymous example is insufficient. Credentialed CORS requires an explicit allowed origin and Access-Control-Allow-Credentials: true, alongside the appropriate media request mode. A wildcard origin cannot be used for that request. Browser cookie policies still apply; see the credentialed-request guidance before configuring that flow.
Check the deployed headers with the page’s Origin
This command sends the example page origin and prints the caption response headers and body. Replace both example values:
curl --silent --show-error --location --include \
--header 'Origin: https://example.com' \
'https://media.example.com/lesson.en.vtt'
After any redirects, inspect the final response. Expect the intended VTT content, beginning with WEBVTT, and the CORS permission for the request origin. A curl response helps inspect delivery; curl does not enforce the browser’s CORS rules. Confirm the result in the browser too.
Check the MP4’s actual browser requests, including partial responses used for seeking. If playback starts but jumping ahead fails, use the HTTP range-request guide to inspect that exchange.
Retest both playback and the caption track
- Reload the deployed page with a fresh browser cache. Start the video and turn captions on using its controls.
- Confirm that the MP4 and VTT requests succeed without CORS errors. Read a caption while the matching audio plays.
- If the track loads but no text appears, check track selection, cue syntax, and timing. Header changes cannot fix an empty or mistimed VTT file.
- Repeat in your supported browsers and after a CDN policy or media-host change. Keep the failing URL, page origin, and observed response headers with the issue.