AI Income LabJoin AI Income Lab ↗

Build guide · By · Published · Updated

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.

Diagram: a YouTube feed that survives a failed request: fetch on the server, keep saved uploads, render useful HTML.
Fetch on the server, preserve saved uploads, and render useful HTML when the live feed fails.

What you’ll need

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

  1. src/youtube-snapshot.json holds the saved videos and channel counts.
  2. scripts/refresh-videos.js refreshes the snapshot at build time. If the public endpoint fails, a validated saved snapshot remains available.
  3. scripts/prerender.js renders the homepage into HTML. Video links are therefore available before browser JavaScript runs.
  4. useYouTubeFeed in src/main.jsx initializes from that same snapshot and asks /api/youtube for fresh data.
  5. api/youtube.js talks 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

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.

AI Income Lab homepage showing the build introduction and latest-video preview on desktop.
The production homepage captured during the November 5 audit, before the starter-kit homepage link was added.

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.

CheckObserved resultWhat it establishes
Public page availability51 checked URLs returned HTTP 200The homepage, 41 guides, main information pages, robots file, and sitemap were available at audit time.
Mobile layout390px viewport and 390px document widthNo horizontal overflow on the inspected homepage and search page.
SearchSearch index returned HTTP 200; “make” produced resultsThe inspected query loaded results and a Show more control.
Consent and ClarityNo Clarity resource request before consent; collection returned HTTP 204 after consentThe inspected browser sent data after acceptance. This does not verify dashboard reporting.
Starter-kit deliveryZIP returned HTTP 200, 3,855 bytes, with a ZIP signatureThe 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.

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

See every tool I recommend →

You may also like

Written by . I’ve shipped software since 2013 and test AI tools on real builds for the AI Automation Station YouTube channel. Each guide shows the date it was last updated and lists the sources it relies on.

Build the next one with AI Income Lab

Step-by-step courses, reusable templates, and a community that answers when you get stuck.

Join AI Income Lab ↗From $29/month. Cancel anytime.
Join 2,900+ members building with AI

Not ready to join? Get the weekly build notes: new guides and tested builds in one short email.