Skip to content

Commit

Permalink
Update _worker.js
Browse files Browse the repository at this point in the history
缓存无效链接响应。
  • Loading branch information
0-RTT authored Sep 29, 2024
1 parent 5bd1363 commit 0dcc7a6
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions _worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ function authenticate(request, USERNAME, PASSWORD) {
async function handleRootRequest(request, USERNAME, PASSWORD, enableAuth) {
const cache = caches.default;
const cacheKey = new Request(request.url);
if (enableAuth) {
if (!authenticate(request, USERNAME, PASSWORD)) {
return new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="Admin"' } });
}
}
const cachedResponse = await cache.match(cacheKey);
if (cachedResponse) {
return cachedResponse;
return cachedResponse;
}
if (enableAuth) {
if (!authenticate(request, USERNAME, PASSWORD)) {
return new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="Admin"' } });
}
}
isAuthenticated = true;
const response = new Response(`
<!DOCTYPE html>
<html lang="zh-CN">
Expand Down Expand Up @@ -434,8 +433,8 @@ async function handleRootRequest(request, USERNAME, PASSWORD, enableAuth) {
});
</script>
</body>
</html>
`, { headers: { 'Content-Type': 'text/html;charset=UTF-8' } });
</html>
`, { headers: { 'Content-Type': 'text/html;charset=UTF-8' } });
await cache.put(cacheKey, response.clone());
return response;
}
Expand Down Expand Up @@ -768,7 +767,6 @@ async function handleImageRequest(request, DATABASE, TG_BOT_TOKEN) {
if (cachedResponse) {
return cachedResponse;
}

const result = await DATABASE.prepare('SELECT fileId FROM media WHERE url = ?').bind(requestedUrl).first();
if (result) {
const fileId = result.fileId;
Expand Down Expand Up @@ -798,11 +796,11 @@ async function handleImageRequest(request, DATABASE, TG_BOT_TOKEN) {
const responseToCache = new Response(response.body, { status: response.status, headers });
await cache.put(cacheKey, responseToCache.clone());
return responseToCache;
} else {
return new Response(null, { status: 404 });
}
}
return new Response(null, { status: 404 });
const notFoundResponse = new Response(null, { status: 404 });
await cache.put(cacheKey, notFoundResponse.clone());
return notFoundResponse;
}

async function handleBingImagesRequest(request) {
Expand Down

0 comments on commit 0dcc7a6

Please sign in to comment.