Skip to content

Commit

Permalink
update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ihsangan committed May 25, 2024
1 parent fe93c4f commit c51ecb7
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,28 +211,30 @@ async function serveResult(request) {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Method': 'GET, HEAD',
'Cache-Control': 'public, max-age=43200',
'Cache-Control': 'private, max-age=30',
'Content-Length': result.length,
'Content-Type': 'application/json; charset=utf-8',
}
})
return response
}
async function checkCache(request) {
if (request.method !== 'GET' || request.method !== 'HEAD') {
if (request.method == 'GET' || request.method == 'HEAD') {
let now = Date.now()
let cache = caches.default
let response = await cache.match(request.url)
if (!response) {
response = await serveResult(request)
await cache.put(request.url, response.clone())
}
response = new Response(response.body, response)
response.headers.delete('Cache-Control')
response.headers.append('X-Response-Time', Date.now() - now)
return response
}
else {
return new Response('Method Not Allowed', {
status: 405
})
}
let now = Date.now()
let cache = caches.default
let response = await cache.match(request.url)
if (!response) {
response = await serveResult(request)
await cache.put(request.url, response.clone())
}
response = new Response(response.body, response)
response.headers.delete('Cache-Control')
response.headers.append('X-Response-Time', Date.now() - now)
return response
}

0 comments on commit c51ecb7

Please sign in to comment.