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.
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.
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>