Skip to content

Commit 1589fb2

Browse files
committed
fix: do not make the progress visible under 500ms
1 parent 516e775 commit 1589fb2

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

packages/astro/src/default/components/PageLoadingIndicator.astro

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
<script>
1111
const progressEl = document.querySelector('[data-id="page-loading-progress"]') as HTMLDivElement;
1212
const storageKey = 'tk_plid';
13+
const maxDurationWithoutProgressBar = 500;
1314

14-
let expectedDuration = parseFloat(localStorage.getItem(storageKey) || '1000');
15+
let expectedDuration = parseFloat(localStorage.getItem(storageKey) || `${maxDurationWithoutProgressBar}`);
1516

1617
let intervalId: ReturnType<typeof setInterval> | undefined;
1718
let timeoutId: ReturnType<typeof setTimeout> | undefined;
@@ -23,17 +24,24 @@
2324
startTime = Date.now();
2425

2526
progressEl.style.width = '0%';
26-
progressEl.style.opacity = '1';
27+
progressEl.style.opacity = '0';
2728

2829
intervalId = setInterval(() => {
2930
const elapsedTime = Date.now() - startTime;
3031

31-
progressEl.style.width = `${Math.min(elapsedTime / expectedDuration, 1) * 100}%`;
32+
// we're past the 500ms mark and we're about to make the bar visible, extend the expectedDuration a bit
33+
if (elapsedTime > maxDurationWithoutProgressBar && expectedDuration <= maxDurationWithoutProgressBar) {
34+
// this a bit arbitrary to make it look "good"
35+
expectedDuration += 2 * maxDurationWithoutProgressBar;
36+
}
3237

33-
if (elapsedTime > expectedDuration) {
34-
clearInterval(intervalId);
38+
// if expected duration is less we don't show anything
39+
if (expectedDuration < maxDurationWithoutProgressBar) {
3540
return;
3641
}
42+
43+
progressEl.style.opacity = '1';
44+
progressEl.style.width = `${Math.min(elapsedTime / expectedDuration, 1) * 100}%`;
3745
}, 100);
3846
}
3947

0 commit comments

Comments
 (0)