Revert "[fix] solution/site: 옛 해시 자산을 30일 남긴다 — 배포와 전체 재굽기를 뗀다"
This reverts commit 8f6ea16f65.
This commit is contained in:
1 parent
9a4bc0e123
commit
b797535b17
4 files changed
+11
-156
No files matched your search
@@ -479,115 +479,18 @@ function writeReport(payloadFile: string, report: RenderReport) {
|
||||
renameSync(tmp, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* 옛 자산 보관 기간.
|
||||
*
|
||||
* ★ 왜 지우지 않고 남기나 — HTML 은 자산 경로를 **파일명 해시까지 박아** 굽는다
|
||||
* (`/assets/index-DvNTmLhy.css`). 렌더러를 배포하면 이름이 바뀌는데, 그 순간 옛 파일을
|
||||
* 지우면 아직 다시 굽지 않은 사이트는 CSS·JS 가 **404** 다. 예전 구현이 그랬다.
|
||||
*
|
||||
* ★ 더 나쁜 건 크롤러다. 구글은 HTML 을 가져간 뒤 렌더를 **나중에** 돌린다. 그 사이에
|
||||
* 자산이 사라지면 스타일도 스크립트도 없는 페이지를 렌더한 것으로 기록한다 — 하필
|
||||
* 신규 도메인이 평가받는 시기에 그렇게 된다. 유예 창이 필요하다는 게 업계 통념이고
|
||||
* (Vercel 은 검색봇에 한해 스큐 보호 창을 60일로 늘린다), 우리 창은 0초였다.
|
||||
*
|
||||
* 한 벌이 400KB 안팎이라 한 달치를 남겨도 10MB 남짓이다 — 싸게 사는 안전이다.
|
||||
*/
|
||||
const ASSET_RETENTION_DAYS = 30;
|
||||
/** 하루에 여러 번 배포해도 직전 빌드는 반드시 남는다(보관 기간과 무관). */
|
||||
const ASSET_MIN_BUILDS = 2;
|
||||
/**
|
||||
* 어떤 빌드가 어떤 파일을 깔았는지. **보관 기간의 근거는 이 파일이다.**
|
||||
*
|
||||
* ★ 파일 mtime 으로 나이를 재지 않는다 — 복사·동기화가 시각을 갈아 버리면 옛 파일이
|
||||
* 영원히 젊어지거나 산 파일이 지워진다. 점(.)으로 시작해 업로드에서 빠진다
|
||||
* (azure_static 이 dotfile 을 거른다).
|
||||
*/
|
||||
const ASSET_LEDGER = '.builds.json';
|
||||
|
||||
/** 자산 대장 한 줄 — 한 번의 번들 빌드가 깐 파일 목록. */
|
||||
interface AssetBuild {
|
||||
/** 이 번들을 마지막으로 깐 시각(ISO). 같은 번들로 다시 구우면 갱신된다. */
|
||||
at: string;
|
||||
/** assets/ 기준 상대 경로. */
|
||||
files: string[];
|
||||
}
|
||||
|
||||
/** 디렉토리 안의 파일을 상대 경로로 편다(하위 디렉토리 포함). */
|
||||
function listRelativeFiles(dir: string, prefix = ''): string[] {
|
||||
const found: string[] = [];
|
||||
for (const entry of readdirSync(dir, {withFileTypes: true})) {
|
||||
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
|
||||
if (entry.isDirectory()) found.push(...listRelativeFiles(join(dir, entry.name), rel));
|
||||
else if (entry.isFile()) found.push(rel);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
function readAssetLedger(assetsDir: string): AssetBuild[] {
|
||||
try {
|
||||
const parsed = JSON.parse(readFileSync(join(assetsDir, ASSET_LEDGER), 'utf-8')) as {
|
||||
builds?: AssetBuild[];
|
||||
};
|
||||
return Array.isArray(parsed.builds) ? parsed.builds : [];
|
||||
} catch {
|
||||
// 없거나 깨졌으면 이번 빌드부터 다시 센다. 옛 파일은 그대로 남는다 —
|
||||
// 대장을 못 읽었다고 지우는 쪽으로 실패하면 사이트가 깨진다.
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 보관 기간이 지난 옛 해시 파일만 지운다.
|
||||
*
|
||||
* ★ 발행할 때마다 이 함수가 돈다(사이트 하나만 구울 때도). 번들이 그대로면 대장에 줄이
|
||||
* 늘지 않고 맨 앞 줄의 시각만 갱신된다 — 안 그러면 발행 횟수만큼 대장이 자란다.
|
||||
*/
|
||||
function pruneAssets(assetsDir: string, current: string[]) {
|
||||
const signature = (files: string[]) => [...files].sort().join('\n');
|
||||
const previous = readAssetLedger(assetsDir);
|
||||
const head: AssetBuild = {at: new Date().toISOString(), files: current};
|
||||
const builds =
|
||||
previous[0] && signature(previous[0].files) === signature(current)
|
||||
? [head, ...previous.slice(1)]
|
||||
: [head, ...previous];
|
||||
|
||||
const cutoff = Date.now() - ASSET_RETENTION_DAYS * 24 * 60 * 60 * 1000;
|
||||
const kept = builds.filter(
|
||||
(build, index) => index < ASSET_MIN_BUILDS || Date.parse(build.at) >= cutoff,
|
||||
);
|
||||
const alive = new Set(kept.flatMap((build) => build.files));
|
||||
|
||||
let removed = 0;
|
||||
for (const file of listRelativeFiles(assetsDir)) {
|
||||
if (file === ASSET_LEDGER || alive.has(file)) continue;
|
||||
rmSync(join(assetsDir, file), {force: true});
|
||||
removed += 1;
|
||||
}
|
||||
|
||||
writeFileSync(
|
||||
join(assetsDir, ASSET_LEDGER),
|
||||
JSON.stringify({schemaVersion: 1, builds: kept}, null, 2),
|
||||
'utf-8',
|
||||
);
|
||||
if (removed > 0) {
|
||||
console.log(` ✓ 보관 기간(${ASSET_RETENTION_DAYS}일)이 지난 자산 ${removed}개 정리`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 클라이언트 번들과 public/ 을 대상 디렉토리에 깐다.
|
||||
*
|
||||
* ★ assets/ 를 통째로 지우고 다시 깔지 않는다(예전 구현). 옛 해시 파일은 보관 기간까지
|
||||
* 남겨야 한다 — 근거는 ASSET_RETENTION_DAYS 주석. 권한 때문에 통째로 지웠던 것인데,
|
||||
* copyDirectoryFiles 가 **파일마다** 먼저 rmSync 하므로 그 문제는 그대로 해결된다.
|
||||
* Docker bind mount 에서 이전 빌드가 남긴 파일 권한이 호스트와 어긋날 수 있어
|
||||
* assets/ 는 통째로 교체한다(낡은 해시 파일과 권한을 함께 제거).
|
||||
*/
|
||||
function writeSharedAssets(destRoot: string) {
|
||||
const assetsSrc = join(CLIENT_DIR, 'assets');
|
||||
if (existsSync(assetsSrc)) {
|
||||
const assetsDest = join(destRoot, 'assets');
|
||||
rmSync(assetsDest, {recursive: true, force: true});
|
||||
copyDirectoryFiles(assetsSrc, assetsDest);
|
||||
pruneAssets(assetsDest, listRelativeFiles(assetsSrc));
|
||||
}
|
||||
const publicDir = join(SITE_ROOT, 'public');
|
||||
if (existsSync(publicDir)) {
|
||||
|
||||
Reference in new issue
Block a user