Skip to content

Commit

Permalink
fix: correct the calc of the public final url
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Feb 22, 2024
1 parent 35ce0f4 commit 0ac6c34
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/sjs-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export async function sjsRequestHandler(
if ([`${basePath}`, '/', ''].includes(selfUrl.pathname)) {
return Response.redirect(HOMEPAGE || upstreamOrigin, 302);
}
const finalUrl = new URL(req.headers.get('x-real-origin') ?? selfUrl);
const finalOriginUrl = new URL(req.headers.get('x-real-origin') ?? selfUrl);
const selfOriginActual = `${selfUrl.origin}${basePath}`;
const selfOriginFinal = `${finalUrl.origin}${basePath}`;
const selfOriginFinal = `${finalOriginUrl.origin}${basePath}`;
const upstreamUrl = new URL(
req.url.replace(selfOriginActual, ''),
upstreamOrigin,
Expand Down Expand Up @@ -65,11 +65,11 @@ export async function sjsRequestHandler(
pair[0],
typeof pair[1] === 'string' ? replaceOrigin(pair[1]) : pair[1],
] as [string, string]);
const canonicalUrl = replaceOrigin(req.url);
const publicSelfUrl = new URL(replaceOrigin(req.url), finalOriginUrl.origin).toString();
if (CACHE) {
performance.mark('cache-read');
const value = await retrieveCache(denoKv, [
canonicalUrl,
publicSelfUrl,
buildTarget,
]);
performance.measure('cache-read', 'cache-read');
Expand Down Expand Up @@ -109,7 +109,7 @@ export async function sjsRequestHandler(
}
const response = await createFinalResponse(
{
url: canonicalUrl,
url: publicSelfUrl,
body,
headers: cloneHeaders(
upstreamResponse.headers,
Expand Down
1 change: 0 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ export const createFinalResponse = async (
performance.mark('cache-write');
await saveCache(denoKv, [url, buildTarget], responseProps);
performance.measure('cache-write', 'cache-write');
headers.set('x-debug-cache-key', JSON.stringify([url, buildTarget]));
}
const shouldSetCacheClientRedirect = CACHE_CLIENT_REDIRECT &&
isFastPathRedirect;
Expand Down

0 comments on commit 0ac6c34

Please sign in to comment.