perf(web): defer lyrics prefetch so it doesn't slow playback start

prefetchLyrics now fires ~1.5s after a song change instead of immediately,
so the lrclib requests no longer compete with audio-stream buffering when a
new track starts. Timer is cleared on rapid song changes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
HoneyandClaude Opus 4.8 committed 2026-06-26 16:56:11 +02:00
1 parent d65fbcd11e
commit 687650518e
1 file changed
+6 -2
+6 -2
View File
@@ -100,6 +100,7 @@ export function switchNpTab(tab) {
let _lyricsLines = [];
let _lyricsSongId = null;
let _lyricsLoadToken = 0;
let _prefetchTimer = null;
const _lyricsCache = new Map(); // "artist|title" → result object (null = not found)
const LYRICS_LOOKAHEAD_MS = 200;
@@ -283,8 +284,11 @@ export function onNpSongChange(songId) {
_lyricsLines = [];
const content = document.getElementById('np-lyrics-content');
if(content) content.innerHTML = '';
// Warm the cache the moment the song changes, so the Lyrics tab opens instantly.
prefetchLyrics(S.queue.find(x => x.id === songId) || S.queue[S.qIdx]);
// Warm the cache shortly AFTER the track starts — deferred so the lyrics requests
// don't compete with the audio stream and delay playback start.
clearTimeout(_prefetchTimer);
const song = S.queue.find(x => x.id === songId) || S.queue[S.qIdx];
_prefetchTimer = setTimeout(() => prefetchLyrics(song), 1500);
if(S.npLyricsMode) _loadNpLyrics(songId);
}