Skip to content

Commit

Permalink
fix: ensure clearing of performance is done after response is created
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Feb 27, 2024
1 parent 3078dcb commit 2346ab3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
19 changes: 17 additions & 2 deletions src/sjs-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export async function sjsRequestHandler(
req: Request,
): Promise<Response> {
const performance = new ScopedPerformance();
const clearPerformance = () => {
performance.clearMarks();
performance.clearMeasures();
};
performance.mark('total');
const BASE_PATH = Deno.env.get('BASE_PATH');
const CACHE = Deno.env.get('CACHE') === 'true';
Expand All @@ -29,6 +33,7 @@ export async function sjsRequestHandler(
const basePath = `/${BASE_PATH}/`.replace(/\/+/g, '/');
const upstreamOrigin = `${UPSTREAM_ORIGIN}/`.replace(/\/+$/, '/');
if ([`${basePath}`, '/', ''].includes(selfUrl.pathname)) {
clearPerformance();
return Response.redirect(HOMEPAGE || upstreamOrigin, 302);
}
const finalOriginUrl = new URL(req.headers.get('x-real-origin') ?? selfUrl);
Expand Down Expand Up @@ -88,12 +93,15 @@ export async function sjsRequestHandler(
false,
);
if (CACHE_CLIENT_REDIRECT && isRedirect(response.status)) {
return createFastPathResponse(
const fastResponse = await createFastPathResponse(
response,
performance,
buildTarget,
);
clearPerformance();
return fastResponse;
}
clearPerformance();
return response;
}
performance.measure('cache-miss', { start: performance.now() });
Expand Down Expand Up @@ -127,7 +135,14 @@ export async function sjsRequestHandler(
CACHE,
);
if (CACHE && CACHE_CLIENT_REDIRECT && isRedirect(response.status)) {
return createFastPathResponse(response, performance, buildTarget);
const fastResponse = await createFastPathResponse(
response,
performance,
buildTarget,
);
clearPerformance();
return fastResponse;
}
clearPerformance();
return response;
}
18 changes: 5 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,13 @@ export const saveCache = async (
//settledKv.close();
};

const buildDebugPerformance = (
performance: Performance,
shouldClear = true,
): string => {
const serverTiming = performance.getEntriesByType('measure')
const buildDebugPerformance = (performance: Performance): string => (
performance.getEntriesByType('measure')
.map(({ name, duration }) =>
`${name}${duration ? `;dur=${duration}` : ''}`
)
.join(',');
if (shouldClear) {
performance.clearMarks();
performance.clearMeasures();
}
return serverTiming;
};
.join(',')
);

export const createFinalResponse = async (
responseProps: ResponseProps,
Expand Down Expand Up @@ -200,7 +192,7 @@ export const createFinalResponse = async (
performance.measure('total', 'total');
headers.set(
'server-timing',
buildDebugPerformance(performance, !isFastPathRedirect),
buildDebugPerformance(performance),
);

const response = new Response(body, {
Expand Down

0 comments on commit 2346ab3

Please sign in to comment.