Skip to content

Commit 64bed98

Browse files
committed
feat: 静态资源支持缓存
1 parent f06b3af commit 64bed98

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

backend/src/cgi.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ async function getData() {
1313

1414
if (assets) {
1515
const path = assets === "/" ? "/index.html" : assets;
16+
const cache = assets !== "/";
1617

1718
return {
18-
api: "assets",
19-
query: { path: `/var/apps/code.editor/target/server/dist${path}` },
19+
env,
20+
api: "read",
21+
query: { path: `/var/apps/code.editor/target/server/dist${path}`, cache },
2022
body: {},
2123
};
2224
}
2325

2426
const result = {
27+
env,
2528
api: env.HTTP_API_PATH || "",
2629
query: querystring.parse(env.QUERY_STRING || ""),
2730
body: {},

backend/src/router/read.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ module.exports = async function read({ query }) {
2020
const testFd = fs.openSync(path, "r");
2121
fs.closeSync(testFd);
2222

23+
if (query.cache) {
24+
const maxAge = 365 * 24 * 60 * 60;
25+
26+
console.log(`Cache-Control: public, max-age=${maxAge}, immutable`);
27+
console.log(
28+
`Expires: ${new Date(Date.now() + maxAge * 1000).toUTCString()}`
29+
);
30+
console.log(`Last-Modified: ${stat.mtime.toUTCString()}`);
31+
console.log(`ETag: "${stat.size}-${stat.mtime.getTime()}"`);
32+
}
33+
2334
return {
2435
code: 200,
2536
msg: "操作成功",

backend/src/utils/exec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const getType = require("./type");
22

33
const router = {
4-
assets: { type: "file", run: require("../router/read") },
54
read: { type: "file", run: require("../router/read") },
65
save: { run: require("../router/save") },
76
};

0 commit comments

Comments
 (0)