Silent Failures: Upgrading to YouTube API & The BOM JSON Trap
đĄ I recently faced two separate âsilent failuresâ where my playlist sync script was running successfully in GitHub Actions, but my tracklists were coming up empty on the website.
1. Bot Detection & Rate Limiting
Initially, I was using yt-dlp to scrape playlist data. However, GitHub Actions runner IPs are heavily flagged by YouTubeâs bot detection. The script would run, get blocked, return 0 tracks, and exit gracefullyâleaving the JSON untouched.
The fix: I rewrote the script to use the official YouTube Data API v3. By using an authenticated API Key (stored securely in GitHub Secrets) and native fetch, the sync is now faster, more stable, and immune to IP blocking.
2. The Byte Order Mark (BOM) Trap
After the API fix, the script still wasnât saving data. The GitHub Action was marked as a success, but I decided to dig into the logs manually (by going to the Actions tab, clicking the run, and expanding the âRun sync scriptâ step). There, the logs showed a strange error: Unexpected token '', "{ "quar"... is not valid JSON". To the naked eye, the JSON file looked perfect.
It turns out the file had been saved with a UTF-8 BOM. This is an invisible character at the very beginning of a file that tells some editors how to read the encoding. While humans canât see it, Node.js sees it as an illegal character before the opening {, causing JSON.parse() to fail instantly.
How I fixed the BOM issue:
- Opened
playlists.jsonin VS Code. - Clicked the encoding setting in the bottom right status bar.
- Changed UTF-8 with BOM to UTF-8 (plain).
- Saved and pushed the file.
đ§ Key Takeaways: First, avoid web scraping in CI/CD environments when official APIs are available. Second, if you see an âUnexpected Tokenâ error at the start of a valid JSON file, check for invisible encoding characters. Always stick to plain UTF-8 (No BOM) for configuration files in Node.js environments.