2
0
Просмотр исходного кода

⚡️ perf(progress): optimize compilation progress tracking timing

- 调整保守估计值计算方式,将基础值从+10/15改为+8/12
- 缩短服务端活动检测超时时间,从3秒改为2秒
- 提高客户端完成阈值,从80%提升至85%并增加90%强制完成条件
- 缩短超时强制完成时间,从5秒改为3秒
- 降低进度检查起始阈值,从90%改为85%并缩短检查间隔至800ms
- 优化预估总数调整策略,仅在资源完成95%时+1,减少过度估计
- 页面加载完成后提前至500ms启动进度检测,提高完成识别积极性
yourname 7 месяцев назад
Родитель
Сommit
576c0ae347
1 измененных файлов с 30 добавлено и 17 удалено
  1. 30 17
      vite-plugin-compile-progress.js

+ 30 - 17
vite-plugin-compile-progress.js

@@ -114,7 +114,7 @@ export function progressTrackingPlugin() {
                     const preloadLinks = document.querySelectorAll('link[rel="preload"], link[rel="modulepreload"]').length;  
                     const preloadLinks = document.querySelectorAll('link[rel="preload"], link[rel="modulepreload"]').length;  
                       
                       
                     const baseEstimate = scripts + links + styleSheets + preloadLinks;  
                     const baseEstimate = scripts + links + styleSheets + preloadLinks;  
-                    const conservativeEstimate = Math.max(baseEstimate + 10, 15);  
+                    const conservativeEstimate = Math.max(baseEstimate + 8, 12);  
                       
                       
                     return conservativeEstimate;  
                     return conservativeEstimate;  
                   }  
                   }  
@@ -124,13 +124,14 @@ export function progressTrackingPlugin() {
                     hasServerActivity = true;  
                     hasServerActivity = true;  
                     if (serverActivityTimer) clearTimeout(serverActivityTimer);  
                     if (serverActivityTimer) clearTimeout(serverActivityTimer);  
                       
                       
-                    // 如果3秒内没有新的服务端活动,认为服务端编译已完成或无需编译  
+                    // 如果2秒内没有新的服务端活动,认为服务端编译已完成或无需编译  
                     serverActivityTimer = setTimeout(() => {  
                     serverActivityTimer = setTimeout(() => {  
                       if (serverProgress === 0) {  
                       if (serverProgress === 0) {  
                         console.log('No server compilation detected - using client-only mode');  
                         console.log('No server compilation detected - using client-only mode');  
                         hasServerActivity = false;  
                         hasServerActivity = false;  
+                        updateProgress();  
                       }  
                       }  
-                    }, 3000);  
+                    }, 2000);  
                   }  
                   }  
   
   
                   // 检测是否完成加载  
                   // 检测是否完成加载  
@@ -141,8 +142,15 @@ export function progressTrackingPlugin() {
                     // 根据服务端活动情况调整完成条件  
                     // 根据服务端活动情况调整完成条件  
                     if (!hasServerActivity) {  
                     if (!hasServerActivity) {  
                       // 无服务端编译时,主要依赖客户端资源  
                       // 无服务端编译时,主要依赖客户端资源  
-                      if (resourcesCompleted >= estimatedTotal * 0.8 && timeSinceLastResource > 2000) {  
-                        console.log('Client-only loading complete');  
+                      if (resourcesCompleted >= estimatedTotal * 0.85 && timeSinceLastResource > 1500) {  
+                        console.log('Client-only loading complete (85% threshold)');  
+                        forceComplete();  
+                        return;  
+                      }  
+                        
+                      // 如果进度超过90%且1.5秒无新资源,强制完成  
+                      if (resourcesCompleted >= estimatedTotal * 0.9 && timeSinceLastResource > 1500) {  
+                        console.log('Client-only loading complete (90% threshold)');  
                         forceComplete();  
                         forceComplete();  
                         return;  
                         return;  
                       }  
                       }  
@@ -155,9 +163,9 @@ export function progressTrackingPlugin() {
                       }  
                       }  
                     }  
                     }  
                       
                       
-                    // 超时强制完成  
-                    if (timeSinceLastResource > 5000) {  
-                      console.log('Force completing due to timeout');  
+                    // 超时强制完成 - 缩短超时时间  
+                    if (timeSinceLastResource > 3000) {  
+                      console.log('Force completing due to timeout (3s)');  
                       forceComplete();  
                       forceComplete();  
                     }  
                     }  
                   }  
                   }  
@@ -221,9 +229,9 @@ export function progressTrackingPlugin() {
                     console.log(\`Progress: Total \${Math.round(totalProgress)}%\`);  
                     console.log(\`Progress: Total \${Math.round(totalProgress)}%\`);  
                       
                       
                     // 如果接近完成,开始检测完成状态  
                     // 如果接近完成,开始检测完成状态  
-                    if (totalProgress >= 90) {  
+                    if (totalProgress >= 85) {  
                       if (completionTimer) clearTimeout(completionTimer);  
                       if (completionTimer) clearTimeout(completionTimer);  
-                      completionTimer = setTimeout(checkCompletion, 1000);  
+                      completionTimer = setTimeout(checkCompletion, 800);  
                     }  
                     }  
                   }  
                   }  
   
   
@@ -254,9 +262,9 @@ export function progressTrackingPlugin() {
                             resourcesCompleted++;  
                             resourcesCompleted++;  
                             lastResourceTime = Date.now();  
                             lastResourceTime = Date.now();  
                               
                               
-                            // 动态调整预估总数  
-                            if (resourcesCompleted > estimatedTotal * 0.9) {  
-                              estimatedTotal = Math.max(estimatedTotal, resourcesCompleted + 3);  
+                            // 更保守的总数调整策略  
+                            if (resourcesCompleted > estimatedTotal * 0.95) {  
+                              estimatedTotal = Math.max(estimatedTotal, resourcesCompleted + 1);  
                             }  
                             }  
                               
                               
                             console.log(\`Resource completed: \${entry.name} (type: \${entry.initiatorType})\`);  
                             console.log(\`Resource completed: \${entry.name} (type: \${entry.initiatorType})\`);  
@@ -296,14 +304,19 @@ export function progressTrackingPlugin() {
                   function handlePageLoad() {  
                   function handlePageLoad() {  
                     setTimeout(() => {  
                     setTimeout(() => {  
                       if (resourcesCompleted > 0) {  
                       if (resourcesCompleted > 0) {  
-                        estimatedTotal = Math.max(resourcesCompleted + 2, estimatedTotal);  
+                        estimatedTotal = Math.max(resourcesCompleted + 1, estimatedTotal);  
                         console.log(\`Final estimated total: \${estimatedTotal}\`);  
                         console.log(\`Final estimated total: \${estimatedTotal}\`);  
                       }  
                       }  
                       updateProgress();  
                       updateProgress();  
                         
                         
-                      // 开始完成检测  
-                      setTimeout(checkCompletion, 2000);  
-                    }, 1000);  
+                      // 页面加载完成后,更积极地检测完成状态  
+                      setTimeout(() => {  
+                        if (isLoading && resourcesCompleted >= estimatedTotal * 0.8) {  
+                          console.log('Page load complete, forcing finish');  
+                          forceComplete();  
+                        }  
+                      }, 1000);  
+                    }, 500);  
                   }  
                   }  
   
   
                   // 根据文档状态初始化  
                   // 根据文档状态初始化