Spaces:
Paused
Paused
perf: 초기 진입 속도 개선 — gzip + 부팅 selectFav 병렬화
Browse files측정: HTML 다운 1.4s + 모든 API ~800ms baseline + 부팅 직렬
await(pull → selectFav → loadEpisodes → playEpisode). 4초 이상 깔림.
서버:
- serveStatic에 gzip 응답 추가 (text/html, css, js, json, svg).
index.html 182KB → ~40KB 예상. Vary: Accept-Encoding 헤더 동봉.
클라이언트 boot:
- await pullFavsFromServer 제거. localStorage favs로 즉시 selectFav
시작 (대부분의 재방문은 같은 디바이스 = 동일 lastFavId).
- pullFavsFromServer는 백그라운드. 끝나고 서버 lastFavId가 달라졌고
사용자가 그 사이 다른 카드를 안 눌렀으면 자동 전환.
- refreshLatestLabels도 selectFav와 병렬.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- index.html +26 -11
- server.js +21 -2
index.html
CHANGED
|
@@ -3819,20 +3819,35 @@ function setupPWA() {
|
|
| 3819 |
setupPWA();
|
| 3820 |
loadDomain().catch(e => console.warn('boot loadDomain:', e));
|
| 3821 |
loadRanking('daily');
|
| 3822 |
-
try {
|
| 3823 |
-
await pullFavsFromServer();
|
| 3824 |
-
renderFavs();
|
| 3825 |
-
} catch (e) { console.warn('boot pullFavs:', e); }
|
| 3826 |
// 부팅 시 보관함 NEW 자동 감지 — 사용자 클릭 없이도 latestLabel 갱신
|
| 3827 |
refreshLatestLabels().catch(e => console.warn('boot latest-labels:', e));
|
| 3828 |
-
|
| 3829 |
-
|
| 3830 |
-
|
| 3831 |
-
|
| 3832 |
-
|
| 3833 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3834 |
}
|
| 3835 |
-
}
|
| 3836 |
})();
|
| 3837 |
</script>
|
| 3838 |
</body>
|
|
|
|
| 3819 |
setupPWA();
|
| 3820 |
loadDomain().catch(e => console.warn('boot loadDomain:', e));
|
| 3821 |
loadRanking('daily');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3822 |
// 부팅 시 보관함 NEW 자동 감지 — 사용자 클릭 없이도 latestLabel 갱신
|
| 3823 |
refreshLatestLabels().catch(e => console.warn('boot latest-labels:', e));
|
| 3824 |
+
|
| 3825 |
+
// localStorage의 lastFavId로 즉시 selectFav 시작 (서버 pull 기다리지 않음).
|
| 3826 |
+
// pullFavsFromServer는 백그라운드로 돌려 다른 디바이스 변경을 반영.
|
| 3827 |
+
// 서버가 다른 lastFavId를 들고 있고 사용자가 그 사이 다른 카드를 안 눌렀으면 새로 선택.
|
| 3828 |
+
const bootedLastFavId = lastFavId;
|
| 3829 |
+
(async () => {
|
| 3830 |
+
try {
|
| 3831 |
+
if (bootedLastFavId && favs.find(f => f.id == bootedLastFavId)) {
|
| 3832 |
+
await selectFav(+bootedLastFavId, { silent: true });
|
| 3833 |
+
} else {
|
| 3834 |
+
// 부팅 자동 선택 안 했을 때만 이어보기 빅 배너 표시
|
| 3835 |
+
showResumeHero();
|
| 3836 |
+
}
|
| 3837 |
+
} catch (e) { console.warn('boot selectFav:', e); }
|
| 3838 |
+
})();
|
| 3839 |
+
|
| 3840 |
+
// 백그라운드 pull — 끝나면 favs 재렌더, 서버 lastFavId가 달라졌고
|
| 3841 |
+
// 사용자가 부팅 후 다른 카드를 누르지 않았으면 그쪽으로 자동 전환.
|
| 3842 |
+
pullFavsFromServer().then(() => {
|
| 3843 |
+
renderFavs();
|
| 3844 |
+
if (lastFavId && lastFavId !== bootedLastFavId && currentFavId == bootedLastFavId) {
|
| 3845 |
+
// 사용자가 부팅 중 클릭 안 했으면 서버 기준 lastFavId로 갱신
|
| 3846 |
+
if (favs.find(f => f.id == lastFavId)) {
|
| 3847 |
+
selectFav(+lastFavId, { silent: true }).catch(() => {});
|
| 3848 |
+
}
|
| 3849 |
}
|
| 3850 |
+
}).catch(e => console.warn('boot pullFavs:', e));
|
| 3851 |
})();
|
| 3852 |
</script>
|
| 3853 |
</body>
|
server.js
CHANGED
|
@@ -2029,6 +2029,15 @@ const MIME = {
|
|
| 2029 |
'.svg': 'image/svg+xml',
|
| 2030 |
'.webmanifest': 'application/manifest+json; charset=utf-8',
|
| 2031 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2032 |
function serveStatic(req, res) {
|
| 2033 |
let p = decodeURIComponent(url.parse(req.url).pathname);
|
| 2034 |
if (p === '/' || p === '') p = '/index.html';
|
|
@@ -2037,8 +2046,18 @@ function serveStatic(req, res) {
|
|
| 2037 |
if (!fp.startsWith(ROOT) || !fs.existsSync(fp) || fs.statSync(fp).isDirectory()) {
|
| 2038 |
res.writeHead(404); return res.end('not found');
|
| 2039 |
}
|
| 2040 |
-
|
| 2041 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2042 |
}
|
| 2043 |
|
| 2044 |
// ───── 서버 ─────
|
|
|
|
| 2029 |
'.svg': 'image/svg+xml',
|
| 2030 |
'.webmanifest': 'application/manifest+json; charset=utf-8',
|
| 2031 |
};
|
| 2032 |
+
// gzip 압축 대상 MIME — 텍스트류만. 이미지/폰트는 이미 압축 형식.
|
| 2033 |
+
const GZIP_TYPES = new Set([
|
| 2034 |
+
'text/html; charset=utf-8',
|
| 2035 |
+
'text/css; charset=utf-8',
|
| 2036 |
+
'application/javascript; charset=utf-8',
|
| 2037 |
+
'application/json; charset=utf-8',
|
| 2038 |
+
'application/manifest+json; charset=utf-8',
|
| 2039 |
+
'image/svg+xml',
|
| 2040 |
+
]);
|
| 2041 |
function serveStatic(req, res) {
|
| 2042 |
let p = decodeURIComponent(url.parse(req.url).pathname);
|
| 2043 |
if (p === '/' || p === '') p = '/index.html';
|
|
|
|
| 2046 |
if (!fp.startsWith(ROOT) || !fs.existsSync(fp) || fs.statSync(fp).isDirectory()) {
|
| 2047 |
res.writeHead(404); return res.end('not found');
|
| 2048 |
}
|
| 2049 |
+
const type = MIME[path.extname(fp)] || 'application/octet-stream';
|
| 2050 |
+
const acceptEnc = String(req.headers['accept-encoding'] || '');
|
| 2051 |
+
const wantsGzip = /\bgzip\b/.test(acceptEnc) && GZIP_TYPES.has(type);
|
| 2052 |
+
const headers = { 'Content-Type': type, Vary: 'Accept-Encoding' };
|
| 2053 |
+
if (wantsGzip) {
|
| 2054 |
+
headers['Content-Encoding'] = 'gzip';
|
| 2055 |
+
res.writeHead(200, headers);
|
| 2056 |
+
fs.createReadStream(fp).pipe(zlib.createGzip({ level: 6 })).pipe(res);
|
| 2057 |
+
} else {
|
| 2058 |
+
res.writeHead(200, headers);
|
| 2059 |
+
fs.createReadStream(fp).pipe(res);
|
| 2060 |
+
}
|
| 2061 |
}
|
| 2062 |
|
| 2063 |
// ───── 서버 ─────
|