Commit bc672ef2 authored by nanahira's avatar nanahira

add etag to apps.json

parent 93546306
Pipeline #40983 canceled with stages
in 53 seconds
......@@ -11,15 +11,21 @@ export class UpdateController {
constructor(private readonly updateService: UpdateService) {}
@Get('apps.json')
@ApiOperation({ summary: '获取 apps.json', description: '懒得解释这是啥了……' })
@Header('Cache-Control', 'public, max-age=600, stale-while-revalidate=600, stale-if-error=604800')
getAppsJson() {
return this.updateService.getAppsJson();
async getAppsJson(@Res({ passthrough: true }) res: Response) {
const result = await this.updateService.getAppsJson();
this.addETag(res, JSON.stringify(result));
res.setHeader('Cache-Control', 'public, max-age=600, stale-while-revalidate=600, stale-if-error=604800');
return result;
}
private addETag(res: Response, data: any) {
const hash = createHash('sha512').update(JSON.stringify(data)).digest('hex');
res.setHeader('ETag', `"${hash}"`);
}
private async cacheResult<T>(res: Response, prom: Promise<T>) {
const result = await prom;
const hash = createHash('sha512').update(JSON.stringify(result)).digest('hex');
res.setHeader('ETag', `"${hash}"`); // minio-compatibility requires quotes
this.addETag(res, result);
if (res.getHeader('Cache-Control') === undefined) res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment