Skip to content

Commit

Permalink
feat: add http cache for static and public files
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Oct 23, 2023
1 parent 86ed84a commit eae8d35
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/services/core/gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ export default class ApiService extends TcService {
res.setHeader('Content-Type', mime.getType(ext));
}

// 因为对象存储的对象名都是以文件内容hash存储的,因此过期时间可以设置很大
res.setHeader('Cache-Control', 'public, max-age=315360000'); // 10 years => 60 * 60 * 24 * 365 * 10

result.pipe(res);
} catch (err) {
this.logger.error(err);
Expand All @@ -314,6 +317,8 @@ export default class ApiService extends TcService {
authorization: false,
use: [
serve('public', {
cacheControl: true,
maxAge: '1d', // 1 day for public file
setHeaders(res: ServerResponse, path: string, stat: any) {
res.setHeader('Access-Control-Allow-Origin', '*'); // 允许跨域
},
Expand Down

0 comments on commit eae8d35

Please sign in to comment.