Skip to content

Commit e03caf8

Browse files
authored
fix: add cache-control, remove cache for error responses (#114)
1 parent 85ce86c commit e03caf8

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/api/util/cache.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ export async function handleTokenCache(request: FastifyRequest, reply: FastifyRe
1717
if (ifNoneMatch && ifNoneMatch.includes(etag)) {
1818
await reply.header('Cache-Control', CACHE_CONTROL_MUST_REVALIDATE).code(304).send();
1919
} else {
20-
void reply.header('ETag', `"${etag}"`);
20+
void reply.headers({ 'Cache-Control': CACHE_CONTROL_MUST_REVALIDATE, ETag: `"${etag}"` });
2121
}
2222
}
2323
}
2424

25+
export function setReplyNonCacheable(reply: FastifyReply) {
26+
reply.removeHeader('Cache-Control');
27+
reply.removeHeader('Etag');
28+
}
29+
2530
/**
2631
* Retrieve the token's last modified date as a UNIX epoch so we can use it as the response ETag.
2732
* @returns Etag string

src/api/util/errors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import {
1111
TokenNotFoundError,
1212
TokenNotProcessedError,
1313
} from '../../pg/errors';
14+
import { setReplyNonCacheable } from './cache';
1415

1516
export const TokenErrorResponseSchema = {
1617
404: TokenNotFoundResponse,
1718
422: Type.Union([TokenNotProcessedResponse, TokenLocaleNotFoundResponse]),
1819
};
1920

2021
export async function generateTokenErrorResponse(error: any, reply: FastifyReply) {
22+
setReplyNonCacheable(reply);
2123
if (error instanceof TokenNotFoundError) {
2224
await reply.code(404).send(Value.Create(TokenNotFoundResponse));
2325
} else if (error instanceof TokenNotProcessedError) {

0 commit comments

Comments
 (0)