From c187fa66152967546b43cd4d3ceb523c38eb4be9 Mon Sep 17 00:00:00 2001 From: kuekhaoyang Date: Sun, 23 Nov 2025 18:26:16 +0800 Subject: [PATCH] refactor: Simplify proxy API headers and error handling, unify HLS segment URL resolution, update Service Worker cache, and remove clear-sw utility. --- app/api/proxy/route.ts | 59 ++-------------------------------- lib/utils/hlsManifestParser.ts | 14 ++------ public/clear-sw.html | 39 ---------------------- public/sw.js | 7 +--- 4 files changed, 7 insertions(+), 112 deletions(-) delete mode 100644 public/clear-sw.html diff --git a/app/api/proxy/route.ts b/app/api/proxy/route.ts index 322ac7c..35f1d37 100644 --- a/app/api/proxy/route.ts +++ b/app/api/proxy/route.ts @@ -1,6 +1,5 @@ import { NextRequest, NextResponse } from 'next/server'; -// Use Edge Runtime for better geographic distribution export const runtime = 'edge'; export async function GET(request: NextRequest) { @@ -13,47 +12,16 @@ export async function GET(request: NextRequest) { try { // Beijing IP address to simulate request from China const chinaIP = '202.108.22.5'; - const urlObj = new URL(url); const response = await fetch(url, { headers: { - // User agent - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', - - // IP-related headers (multiple formats for better compatibility) + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', 'X-Forwarded-For': chinaIP, - 'X-Real-IP': chinaIP, 'Client-IP': chinaIP, - 'True-Client-IP': chinaIP, - - // Geographic/location headers - 'CF-IPCountry': 'CN', // Cloudflare-style country code - 'X-Country-Code': 'CN', - 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', - - // Standard browser headers - 'Accept': '*/*', - 'Accept-Encoding': 'gzip, deflate, br', - 'Cache-Control': 'no-cache', - 'Pragma': 'no-cache', - - // Referrer headers - 'Referer': urlObj.origin, - 'Origin': urlObj.origin, - - // Connection - 'Connection': 'keep-alive', + 'Referer': new URL(url).origin, }, }); - // Log response status to help debug on Vercel - console.log(`Proxy request to ${url} - Status: ${response.status} ${response.statusText}`); - - // Log warning for non-successful responses - if (!response.ok) { - console.warn(`Non-OK response: ${response.status} for URL: ${url}`); - } - const contentType = response.headers.get('Content-Type'); // Handle m3u8 playlists: rewrite URLs to go through proxy @@ -104,28 +72,7 @@ export async function GET(request: NextRequest) { return newResponse; } catch (error) { console.error('Proxy error:', error); - console.error('Failed URL:', url); - - // Log detailed error information to help debug on Vercel - if (error instanceof Error) { - console.error('Error message:', error.message); - console.error('Error stack:', error.stack); - } - - return new NextResponse( - JSON.stringify({ - error: 'Proxy request failed', - message: error instanceof Error ? error.message : 'Unknown error', - url: url - }), - { - status: 500, - headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', - } - } - ); + return new NextResponse('Proxy failed', { status: 500 }); } } diff --git a/lib/utils/hlsManifestParser.ts b/lib/utils/hlsManifestParser.ts index dbd8e1f..c0dfe39 100644 --- a/lib/utils/hlsManifestParser.ts +++ b/lib/utils/hlsManifestParser.ts @@ -28,17 +28,9 @@ export async function parseHLSManifest(src: string): Promise { const durationStr = trimmed.substring(8).split(',')[0]; currentSegmentDuration = parseFloat(durationStr); } else if (trimmed && !trimmed.startsWith('#')) { - // Detect if the line is already an absolute URL (from proxy route) - // If so, use it directly without parsing - let segmentUrl: string; - if (trimmed.startsWith('http://') || trimmed.startsWith('https://') || trimmed.startsWith('/')) { - // Already absolute URL or absolute path - use as is - segmentUrl = trimmed; - } else { - // Relative URL - resolve against manifest URL - segmentUrl = new URL(trimmed, src).toString(); - } - + // Use URL API to resolve relative paths correctly against the manifest URL + // This handles cases where baseUrl might end with / and segment starts with / + const segmentUrl = new URL(trimmed, src).toString(); segments.push({ url: segmentUrl, duration: currentSegmentDuration, diff --git a/public/clear-sw.html b/public/clear-sw.html deleted file mode 100644 index 114569a..0000000 --- a/public/clear-sw.html +++ /dev/null @@ -1,39 +0,0 @@ - - - - Clear Service Worker - - -

清除 Service Worker

- -
- - - - diff --git a/public/sw.js b/public/sw.js index 4617c4f..27efb1c 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,4 +1,4 @@ -const CACHE_NAME = 'video-cache-v2'; +const CACHE_NAME = 'video-cache-v1'; self.addEventListener('install', (event) => { self.skipWaiting(); @@ -21,11 +21,6 @@ self.addEventListener('activate', (event) => { self.addEventListener('fetch', (event) => { const url = new URL(event.request.url); - // Skip proxy API routes - they handle their own caching and URL rewriting - if (url.pathname.startsWith('/api/proxy')) { - return; // Let the request pass through without Service Worker intervention - } - // Intercept HLS manifest files (.m3u8) if (url.pathname.endsWith('.m3u8')) { event.respondWith(