Skip to content

Instantly share code, notes, and snippets.

@thegulshankumar
Last active May 14, 2026 15:39
Show Gist options
  • Select an option

  • Save thegulshankumar/5e8ceac94aafebcf20ced4a6a8a59a2d to your computer and use it in GitHub Desktop.

Select an option

Save thegulshankumar/5e8ceac94aafebcf20ced4a6a8a59a2d to your computer and use it in GitHub Desktop.
CacheTube Edge
export default {
async fetch(request, env, ctx) {
const cache = caches.default;
const cacheKey = new Request(request.url, request);
let response = await cache.match(cacheKey);
if (response) {
return response;
}
// Example Channel: Google Developers
// Channel ID: UC_x5XG1OV2P6uZZ5FSM9Ttw
const youtubeFeed =
'https://www.youtube.com/feeds/videos.xml?channel_id=UC_x5XG1OV2P6uZZ5FSM9Ttw';
response = await fetch(youtubeFeed, {
cf: {
cacheTtl: 1800,
cacheEverything: true
},
headers: {
'User-Agent': 'Cloudflare Worker'
}
});
response = new Response(response.body, response);
response.headers.set(
'Content-Type',
'application/xml; charset=UTF-8'
);
response.headers.set(
'Cache-Control',
'public, max-age=0'
);
response.headers.set(
'X-Robots-Tag',
'noindex, nofollow'
);
ctx.waitUntil(
cache.put(cacheKey, response.clone())
);
return response;
}
}
@thegulshankumar

Copy link
Copy Markdown
Author

Setup steps:

  • Sign in to Cloudflare
  • Go to Build → Compute → Workers & Pages
  • Create App → Start with Hello World
  • Set worker prefix name, for example: yt
  • Deploy
  • Replace the default script with the attached worker script shared in this email
  • Deploy again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment