React YouTube feed tutorial: API, caching and fallbacks
Quick answer
Fetch your uploads on the server, keep a saved list as a fallback, and render that list into the HTML so the page still shows watch links and thumbnails when the YouTube request fails, then refresh it in the browser. Check the result by making the live fetch fail on purpose and confirming the videos still appear.
This is the React YouTube feed used on this site: server-side API access, cached video data, and a fallback so the page still works when a request fails. A creator homepage should still show useful videos when YouTube is slow. This site starts with a saved list of seven uploads, renders that list into HTML, and refreshes it in the browser.
Setting up your coding workspace first? The Codex on Linux video and build notes cover installation and a first project audit. This feed is a separate project you can run afterward.

What you’ll need
- A current Node.js LTS release that satisfies Vite’s requirements, and npm.
- Basic JavaScript and React familiarity. A coding assistant can explain the source, but you should still inspect and run the changes.
- No API key for the saved example. A live feed for your own channel can use public RSS, or a server-side YouTube Data API key for richer metadata.
The saved example runs locally without buying a course or an AI subscription. Live hosting and provider usage may cost money; check the plans you choose.
Run the working example
Start with the request and response
This guide describes how this site’s feed is built. The site’s source repository is private, so there is no project to clone; build the pattern below in your own React and Vite project. In a working build, the featured card shows a title, thumbnail, publication date, and a link to its internal watch page before you click Play. Non-embeddable recordings link to YouTube instead. The homepage player loads only after you click Play; dedicated watch pages include a player on load, with autoplay off.
Follow the data
src/youtube-snapshot.jsonholds the saved videos and channel counts.scripts/refresh-videos.jsrefreshes the snapshot at build time. If the public endpoint fails, a validated saved snapshot remains available.scripts/prerender.jsrenders the homepage into HTML. Video links are therefore available before browser JavaScript runs.useYouTubeFeedinsrc/main.jsxinitializes from that same snapshot and asks/api/youtubefor fresh data.api/youtube.jstalks to YouTube and caches successful results. An API key, when used, stays on the server.
Keep a useful fallback
The essential React pattern is small. Initialize from data you already have, validate the replacement, and keep the saved list when refreshing fails:
const [feed, setFeed] = useState(savedFeed);
useEffect(() => {
fetch('/api/youtube')
.then(response => {
if (!response.ok) throw new Error('Feed unavailable');
return response.json();
})
.then(next => {
if (validFeed(next)) setFeed(next);
})
.catch(() => {}); // The saved videos remain visible.
}, []);Use the repository’s validFeed helper: it checks video IDs, dates, titles, summaries, and nonnegative view counts. Don’t place arbitrary API strings into HTML with innerHTML; render text through React.
Adapt it to your channel
Change channelUrl and channelId in src/youtube.js. Replace the saved JSON with your own public video data and point the refresh script at your own feed endpoint. The sample snapshot and refresh URL belong to AI Automation Station; changing only the visible channel name would leave the wrong videos in the build.
If you use a YouTube API key, configure YOUTUBE_API_KEY on the server. Do not prefix it with VITE_ or put it in the snapshot. Keep each card’s width and height defined, provide a useful accessible Play label, and size thumbnail downloads for the viewport.
Check the result
npm test
npm run build
npm run preview- View the built homepage source and look for
/watch/links. Videos marked non-embeddable should useyoutube.com/watch?v=instead. - Block
/api/youtubein browser developer tools and reload. The saved video cards should remain visible. - On the homepage, tab to Play, activate it, and confirm the player appears only after activation. Open a dedicated watch page separately and confirm its iframe exists without a click, with autoplay off.
- Check a 320px viewport for clipped titles and horizontal scrolling.
Success means a readable, clickable feed survives a failed refresh. A cached snapshot is not a promise of real-time counts; this implementation refreshes live data daily.
When a check fails
If the local feed endpoint returns not found under Vite, the saved videos should still render: the Vite dev server does not run the Vercel API functions. If the build reports an invalid snapshot, replace it with a validated feed before retrying. If your copy shows this channel’s videos, update both the saved JSON and the refresh endpoint.
Test the pure data helpers without a network request. A passing test verifies the sample inputs; it does not prove that a third-party service is available.
Source and next step
See the feed on the homepage. Next, measure clicks after consent without treating a watch link as a subscription. To plan what to publish and measure afterward, see the YouTube channel growth workflow.
If you build this with a coding assistant, the Codex tutorial shows how to brief and verify one small change, and the Claude Code tutorial for beginners covers setup. To check an API call on its own before it reaches a page, see make your first AI API request with a local test. For a wider view of where a build like this fits, read what AI automation is and how it works.
Case study: the site you are reading
AI Automation Station uses this feed pattern in production. The problem was to keep videos and written guides discoverable even when a live data request fails, while giving visitors a useful first build to try. This is Mike’s own site, ; the observations below are from its November 5, 2026 audit.

What shipped and how it holds together
The homepage starts with saved uploads rendered into HTML, then refreshes them in the browser. The guide directory and search index are generated from the guide pages. Visitors can read a guide, download a starter kit, or join the newsletter. Optional Google, Meta, and Clarity tracking loads after the visitor accepts the consent prompt.
| Check | Observed result | What it establishes |
|---|---|---|
| Public page availability | 51 checked URLs returned HTTP 200 | The homepage, 41 guides, main information pages, robots file, and sitemap were available at audit time. |
| Mobile layout | 390px viewport and 390px document width | No horizontal overflow on the inspected homepage and search page. |
| Search | Search index returned HTTP 200; “make” produced results | The inspected query loaded results and a Show more control. |
| Consent and Clarity | No Clarity resource request before consent; collection returned HTTP 204 after consent | The inspected browser sent data after acceptance. This does not verify dashboard reporting. |
| Starter-kit delivery | ZIP returned HTTP 200, 3,855 bytes, with a ZIP signature | The package downloaded. Importing and running its Make workflow remains unverified. |
Costs to account for
The guides and starter kit are free to visitors. Running a copy of this site involves separate hosting, email, and optional API accounts. The audit did not inspect invoices, so it establishes no monthly total or cost saving.
- Hosting: Vercel build, function, and bandwidth usage depend on the owner’s plan and traffic.
- Email: Resend sends confirmation and welcome messages. Sending volume and plan determine the bill.
- Video data: the saved snapshot supports an offline fallback; live API requests still have provider quotas.
- Search and automation: transcript search, AI services, and Make scenario runs may add usage charges. Check the accounts you enable.
What did not work well enough
The audit found six newer guides missing the standard metadata, images, and navigation. Completing those pages became a concrete follow-up. It also found that the downloadable Make blueprint had not been run in a live account, and that an old newsletter confirmation could reverse a later unsubscribe. These limitations describe the November 5 audit snapshot.
November 9 follow-up: The missing guide metadata and navigation have been completed. Newsletter confirmation now rejects links older than 15 minutes when an address has opted out. Welcome delivery retries once with a provider idempotency key, and a persistent failure is flagged in the owner notification. Recent-link reuse after a later opt-out still needs shared subscription-state tracking; email deduplication is limited to the provider’s 24-hour window. The Make blueprint still needs a live-account test.
For your own version, record the same checks before and after a change. Keep availability, browser behavior, and business results separate: a successful page response does not prove a reader finished the exercise or became a paying member. Start with the free Make.com starter kit, and use consent-aware click tracking to measure interest in it.
FAQ
What happens when the YouTube request fails?
The page keeps the saved list of uploads, which is already rendered into the HTML, so watch links and thumbnails still appear.
Do I need a YouTube API key?
Not for the saved example. A live feed for your own channel can use public RSS, or a server-side YouTube Data API key for richer metadata.
Where should the YouTube API key go?
Set YOUTUBE_API_KEY on the server. Don’t prefix it with VITE_ or put it in the snapshot.
How do I use the feed for my own channel?
Change channelUrl and channelId in src/youtube.js, replace the saved JSON with your own video data, and point the refresh script at your own feed endpoint.
Sources
- This guide describes this site’s own implementation. Its source repository is private.
You may also like
Build the next one with AI Income Lab
Step-by-step courses, reusable templates, and a community that answers when you get stuck.
- Courses for builds like this one
- Copy-ready templates
- Ask questions while you build
- Weekly coaching on VIP



