Skip to content

Commit

Permalink
fix: add header "access-control-allow-origin" is not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Jan 31, 2024
1 parent f4206f3 commit 4d49deb
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,24 @@ export const handleResponse = async (responseProps: ResponseProps, shouldCache =
statusText,
} = responseProps;
headers.set('x-cache-status', shouldCache ? 'MISS' : 'HIT');
if (!headers.has('access-control-allow-origin')) {
headers.set('access-control-allow-origin', '*');
}
const response = new Response(body, {
headers,
status,
statusText,
});
const isCacheable = isJsResponse(response) || isRedirectResponse(response);
if (shouldCache && isCacheable) {
try {
const kv = await Deno.openKv();
const blob = new TextEncoder().encode(JSON.stringify({
...responseProps,
ctime: Date.now(),
headers: Object.fromEntries(headers.entries()),
}));
await kvSet(kv, ['cache', url], blob);
kv.close();
} catch (error) {
console.error(error);
}
const kv = await Deno.openKv();
const blob = new TextEncoder().encode(JSON.stringify({
...responseProps,
ctime: Date.now(),
headers: Object.fromEntries(headers.entries()),
}));
await kvSet(kv, ['cache', url], blob);
kv.close();
}
return response;
}
Expand Down

0 comments on commit 4d49deb

Please sign in to comment.