Skip to content

Commit 45006a8

Browse files
committed
refactor: Add cache buster to prevent API caching on health checks
1 parent 8aa1b35 commit 45006a8

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

controllers/healthController.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ describe('healthController', () => {
7272
res.statusCode = code;
7373
return res;
7474
});
75+
res.set = jest.fn().mockReturnValue(res);
7576
res.json = jest.fn().mockReturnValue(res);
7677
return res as Response;
7778
};
@@ -90,7 +91,12 @@ describe('healthController', () => {
9091

9192
const res = createRes();
9293
await healthController.getEmbedDomains({ query: {} } as unknown as Request, res);
93-
94+
expect(res.set).toHaveBeenCalledWith({
95+
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
96+
Pragma: 'no-cache',
97+
Expires: '0',
98+
'Surrogate-Control': 'no-store',
99+
});
94100
expect(res.status).toHaveBeenCalledWith(503);
95101
expect(res.json).toHaveBeenCalledWith({
96102
status: 'error',
@@ -116,6 +122,12 @@ describe('healthController', () => {
116122
mockedHttpClient.get.mockResolvedValue({ status: 301 } as any);
117123
const res = createRes();
118124
await healthController.getAppUrl({} as Request, res);
125+
expect(res.set).toHaveBeenCalledWith({
126+
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
127+
Pragma: 'no-cache',
128+
Expires: '0',
129+
'Surrogate-Control': 'no-store',
130+
});
119131
expect(res.status).toHaveBeenCalledWith(200);
120132
expect(res.json).toHaveBeenCalledWith({
121133
name: 'APP_URL',

controllers/healthController.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface DomainHealthResult {
1818
message?: string;
1919
}
2020

21-
const normalizeUrl = (domain: string): string => {
21+
const normaliseUrl = (domain: string): string => {
2222
return /^https?:\/\//i.test(domain) ? domain : `https://${domain}`;
2323
};
2424

@@ -35,7 +35,7 @@ export const checkDomainHealth = async (
3535
}
3636

3737
try {
38-
const response = await httpClient.get(normalizeUrl(domain), {
38+
const response = await httpClient.get(normaliseUrl(domain), {
3939
maxRedirects: 0,
4040
/* c8 ignore next */
4141
validateStatus: () => true,
@@ -76,14 +76,24 @@ const isHealthy = (results: DomainHealthResult[]): boolean => {
7676
return results.every((result) => result.status === 'success');
7777
};
7878

79+
const setNoCacheHeaders = (res: Response): void => {
80+
res.set({
81+
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
82+
Pragma: 'no-cache',
83+
Expires: '0',
84+
'Surrogate-Control': 'no-store',
85+
});
86+
};
87+
7988
const healthController = {
8089
/**
8190
* Checks the configured embed domains and returns their reachability status.
8291
* @param {Request} _req - Express request object.
8392
* @param {Response} res - Express response object.
8493
* @returns {Promise<Response>} JSON response containing domain health information.
85-
*/
94+
*/
8695
async getEmbedDomains(req: Request, res: Response): Promise<Response> {
96+
setNoCacheHeaders(res);
8797
const target = typeof req.query.target === 'string' ? req.query.target.toLowerCase() : undefined;
8898

8999
// Return 400 if there is no `MULTI_DOMAIN` configured
@@ -117,7 +127,6 @@ const healthController = {
117127
}
118128

119129
const domains = await Promise.all(checks);
120-
121130
const healthy = isHealthy(domains);
122131
return res.status(healthy ? 200 : 503).json({ domains, status: healthy ? 'success' : 'error' });
123132
},
@@ -129,6 +138,7 @@ const healthController = {
129138
* @returns {Promise<Response>} JSON response containing APP_URL health information.
130139
*/
131140
async getAppUrl(_req: Request, res: Response): Promise<Response> {
141+
setNoCacheHeaders(res);
132142
const domain = await checkDomainHealth('APP_URL', appConfig.APP_URL);
133143
const healthy = domain.status === 'success';
134144
return res.status(healthy ? 200 : 503).json(domain);

0 commit comments

Comments
 (0)