-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
97 lines (79 loc) · 3.16 KB
/
sw.js
File metadata and controls
97 lines (79 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const TMDB_KEY = 'fb7bb23f03b6994dafc674c074d01761';
const BASE_URL = 'https://api.themoviedb.org/3';
self.addEventListener('install', (event) => {
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(clients.claim());
});
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Only intercept calls to our /api path
if (url.pathname.startsWith('/api/')) {
event.respondWith(handleApiRequest(url));
}
});
async function handleApiRequest(url) {
const path = url.pathname;
let targetUrl = '';
// 1. Hero / Trending
if (path === '/api/hero') {
targetUrl = `${BASE_URL}/trending/movie/week?api_key=${TMDB_KEY}`;
}
else if (path === '/api/trending') {
targetUrl = `${BASE_URL}/trending/all/week?api_key=${TMDB_KEY}`;
}
// 2. Search
else if (path === '/api/search') {
const query = url.searchParams.get('query');
const type = url.searchParams.get('type') || 'multi';
targetUrl = `${BASE_URL}/search/${type}?api_key=${TMDB_KEY}&query=${encodeURIComponent(query)}`;
}
// 3. Images (Redirect directly to TMDB CDN)
else if (path.startsWith('/api/img/')) {
const imgPath = path.replace('/api/img/', '');
return Response.redirect(`https://image.tmdb.org/t/p/w500/${imgPath}`, 302);
}
// 4. Movie Details
else if (path.startsWith('/api/movie/')) {
const movieId = path.split('/').pop();
targetUrl = `${BASE_URL}/movie/${movieId}?api_key=${TMDB_KEY}&append_to_response=credits`;
}
// 5. Discover (Multi-page English filter)
else if (path.startsWith('/api/discover/')) {
const type = path.split('/').pop();
return handleDiscoverRequest(type);
}
if (targetUrl) {
try {
const response = await fetch(targetUrl);
const data = await response.json();
return new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' }
});
} catch (err) {
return new Response(JSON.stringify({ error: 'Fetch failed' }), { status: 500 });
}
}
return fetch(url);
}
// Special handler for the multi-page English filter logic
async function handleDiscoverRequest(type) {
let combinedResults = [];
try {
// Fetching 5 pages (reduced from 10 to prevent browser timeouts)
for (let page = 1; page <= 5; page++) {
const res = await fetch(`${BASE_URL}/discover/${type}?api_key=${TMDB_KEY}&sort_by=popularity.desc&page=${page}`);
const data = await res.json();
const englishOnly = data.results
.filter(item => item.original_language === 'en')
.map(item => ({ ...item, media_type: type }));
combinedResults = [...combinedResults, ...englishOnly];
}
return new Response(JSON.stringify({ results: combinedResults }), {
headers: { 'Content-Type': 'application/json' }
});
} catch (err) {
return new Response(JSON.stringify({ error: 'Discover failed' }), { status: 500 });
}
}