perf: precompress HiCAD frontend assets

This commit is contained in:
134 2026-07-13 17:33:02 +08:00
parent e4d527f0f2
commit c8c6d9132a
2 changed files with 15 additions and 0 deletions

View File

@ -4,6 +4,7 @@
location ^~ /vendor/zoo-modeling-app/assets/ { location ^~ /vendor/zoo-modeling-app/assets/ {
root /var/www/ai-chatbot/public; root /var/www/ai-chatbot/public;
try_files $uri =404; try_files $uri =404;
gzip_static on;
add_header Cache-Control "public, max-age=31536000, immutable" always; add_header Cache-Control "public, max-age=31536000, immutable" always;
} }

View File

@ -5,12 +5,14 @@ import {
copyFileSync, copyFileSync,
existsSync, existsSync,
mkdirSync, mkdirSync,
readdirSync,
readFileSync, readFileSync,
rmSync, rmSync,
writeFileSync, writeFileSync,
} from "node:fs"; } from "node:fs";
import { cp } from "node:fs/promises"; import { cp } from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { gzipSync } from "node:zlib";
const repoUrl = "https://github.com/KittyCAD/modeling-app.git"; const repoUrl = "https://github.com/KittyCAD/modeling-app.git";
const repoRef = const repoRef =
@ -298,6 +300,17 @@ function ensureWasm() {
copyFileSync(wasmBinary, path.join(sourceDir, "public/kcl_wasm_lib_bg.wasm")); copyFileSync(wasmBinary, path.join(sourceDir, "public/kcl_wasm_lib_bg.wasm"));
} }
function precompressVersionedAssets() {
const assetsDir = path.join(outputDir, "assets");
for (const entry of readdirSync(assetsDir, { withFileTypes: true })) {
if (!entry.isFile() || !/\.(?:css|js)$/.test(entry.name)) {
continue;
}
const assetPath = path.join(assetsDir, entry.name);
writeFileSync(`${assetPath}.gz`, gzipSync(readFileSync(assetPath), { level: 9 }));
}
}
async function main() { async function main() {
ensureOfficialSource(); ensureOfficialSource();
ensureDependencies(); ensureDependencies();
@ -329,6 +342,7 @@ async function main() {
rmSync(outputDir, { force: true, recursive: true }); rmSync(outputDir, { force: true, recursive: true });
mkdirSync(path.dirname(outputDir), { recursive: true }); mkdirSync(path.dirname(outputDir), { recursive: true });
await cp(buildDir, outputDir, { recursive: true }); await cp(buildDir, outputDir, { recursive: true });
precompressVersionedAssets();
const license = path.join(sourceDir, "LICENSE"); const license = path.join(sourceDir, "LICENSE");
if (existsSync(license)) { if (existsSync(license)) {