Skip to content

Instantly share code, notes, and snippets.

@abiyug
Last active January 28, 2026 12:39
Show Gist options
  • Select an option

  • Save abiyug/f79baf76b656ebfcbf43aaac5b7456d2 to your computer and use it in GitHub Desktop.

Select an option

Save abiyug/f79baf76b656ebfcbf43aaac5b7456d2 to your computer and use it in GitHub Desktop.
Fix for YouTube "Error 153" in R Markdown: Resolving the video player configuration error when viewing knitted HTML files in external browsers (Firefox/Chrome) vs. the RStudio viewer.

Gist Title: Fixing YouTube "Error 153" in R Markdown HTML Outputs

Filename: README.md

## The Problem
When knitting an R Markdown (`.Rmd`) file containing a YouTube iframe embed, the video plays fine in the RStudio internal viewer but fails in external browsers (Firefox, Chrome, Safari) with the following error:
> **"Video player configuration error (ID: 153)"**

## Why it Happens
YouTube recently tightened security (Refereer Policies). 
- When you open an HTML file via `file:///C:/...`, the browser sends a **null origin**.
- YouTube rejects "origin-less" requests to prevent clickjacking.
- **RStudio works** because it serves the file through a local web server (`http://127.0.0.1`), which provides a valid origin.

## The Fixes

### 1. Use a Local Web Server (Development)
Instead of opening the `.html` file directly from your folder, use the `{servr}` package in your R console:

```r
# Install if needed
install.packages("servr")

# Run this to serve your current directory
servr::httd()

Now, open the link provided (e.g., http://127.0.0.1:4321) in your browser. The video will play because the protocol is now http instead of file.

2. Deploy to a Web Host (Production)

If you host your file on Netlify, GitHub Pages, or RPubs, the error will disappear automatically. These platforms serve files over https, providing the "Referer" header YouTube requires.

3. Update your Iframe Code

Ensure your embed code includes the referrerpolicy attribute to help the handshake:

<iframe 
  width="560" 
  height="315" 
  src="[https://www.youtube-nocookie.com/embed/VIDEO_ID](https://www.youtube-nocookie.com/embed/VIDEO_ID)" 
  referrerpolicy="strict-origin-when-cross-origin" 
  allowfullscreen>
</iframe>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment