-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
25 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,25 @@ | ||
import { timeNow } from './utils'; | ||
import { allowedMethod, timeNow } from './utils'; | ||
import serveResult from './helpers'; | ||
|
||
export default async function checkCache(request: Request): Promise<Response> { | ||
if (request.method === 'GET' || request.method === 'HEAD') { | ||
const now = timeNow(); | ||
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.head | ||
response.headers.delete('Cache-Control'); | ||
response.headers.append('X-Response-Time', timeNow() - now); | ||
return response; | ||
} else { | ||
return new Response( | ||
JSON.stringify({ | ||
success: false, | ||
message: 'Method not allowed' | ||
}), | ||
{ | ||
status: 405, | ||
headers: { | ||
'Content-Type': 'application/json; charset=utf-8', | ||
} | ||
export default async function checkCache(request: Request): Promise <Response> { | ||
if (allowedMethod.indexOf(request.method) === -1) { | ||
return new Response(JSON.stringify({ success: false, message: 'Method not allowed' }), { | ||
status: 405, | ||
headers: { | ||
'Content-Type': 'application/json; charset=utf-8', | ||
} | ||
); | ||
}); | ||
} | ||
const now = timeNow(); | ||
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.head | ||
response.headers.delete('Cache-Control'); | ||
response.headers.set('X-Response-Time', timeNow() - now); | ||
return response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
name = "validev" | ||
main = "./src/index.ts" | ||
workers_dev = true | ||
compatibility_date = "2022-01-01" | ||
[placement] | ||
mode = "smart" | ||
compatibility_date = "2022-01-01" |