Skip to content

Commit 0dcc7a6

Browse files
authored
Update _worker.js
缓存无效链接响应。
1 parent 5bd1363 commit 0dcc7a6

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

_worker.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,15 @@ function authenticate(request, USERNAME, PASSWORD) {
3838
async function handleRootRequest(request, USERNAME, PASSWORD, enableAuth) {
3939
const cache = caches.default;
4040
const cacheKey = new Request(request.url);
41+
if (enableAuth) {
42+
if (!authenticate(request, USERNAME, PASSWORD)) {
43+
return new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="Admin"' } });
44+
}
45+
}
4146
const cachedResponse = await cache.match(cacheKey);
4247
if (cachedResponse) {
43-
return cachedResponse;
48+
return cachedResponse;
4449
}
45-
if (enableAuth) {
46-
if (!authenticate(request, USERNAME, PASSWORD)) {
47-
return new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="Admin"' } });
48-
}
49-
}
50-
isAuthenticated = true;
5150
const response = new Response(`
5251
<!DOCTYPE html>
5352
<html lang="zh-CN">
@@ -434,8 +433,8 @@ async function handleRootRequest(request, USERNAME, PASSWORD, enableAuth) {
434433
});
435434
</script>
436435
</body>
437-
</html>
438-
`, { headers: { 'Content-Type': 'text/html;charset=UTF-8' } });
436+
</html>
437+
`, { headers: { 'Content-Type': 'text/html;charset=UTF-8' } });
439438
await cache.put(cacheKey, response.clone());
440439
return response;
441440
}
@@ -768,7 +767,6 @@ async function handleImageRequest(request, DATABASE, TG_BOT_TOKEN) {
768767
if (cachedResponse) {
769768
return cachedResponse;
770769
}
771-
772770
const result = await DATABASE.prepare('SELECT fileId FROM media WHERE url = ?').bind(requestedUrl).first();
773771
if (result) {
774772
const fileId = result.fileId;
@@ -798,11 +796,11 @@ async function handleImageRequest(request, DATABASE, TG_BOT_TOKEN) {
798796
const responseToCache = new Response(response.body, { status: response.status, headers });
799797
await cache.put(cacheKey, responseToCache.clone());
800798
return responseToCache;
801-
} else {
802-
return new Response(null, { status: 404 });
803799
}
804800
}
805-
return new Response(null, { status: 404 });
801+
const notFoundResponse = new Response(null, { status: 404 });
802+
await cache.put(cacheKey, notFoundResponse.clone());
803+
return notFoundResponse;
806804
}
807805

808806
async function handleBingImagesRequest(request) {

0 commit comments

Comments
 (0)