Skip to content
Permalink
Browse files

Include cache Server-Timing in response header for performance evalua…

…tion
  • Loading branch information
amodm committed Apr 29, 2020
1 parent ee3986f commit 7dc5f944e6a0332af0137e8c0c35b6c600e62ad1
Showing with 6 additions and 2 deletions.
  1. +6 −2 index.js
@@ -38,10 +38,14 @@ async function notFoundHandler() {
async function cachedData(request, key, decorator) {
const isBrowser = (request.headers.get("accept") || "").toLowerCase().includes("html");
try {
let data= await Store.get(key);
const cacheGetStart = Date.now();
let data = await Store.get(key);
const cacheGetTimeTaken = Date.now() - cacheGetStart;
if (decorator) data = decorator(data);
if (isBrowser) data = JSON.stringify(typeof data === 'string' ? JSON.parse(data) : data, null, 2);
return rawResponse(data);
const response = rawResponse(data);
response.headers.append("Server-Timing", `cache;dur=${cacheGetTimeTaken}`);
return response;
} catch (err) {
return errorResponse({ "message": err })
}

0 comments on commit 7dc5f94

Please sign in to comment.