简体   繁体   English

如何修复审计灯塔建议以提高绩效

[英]How to fix audit lighthouse suggestions for Improved performance

I ran an audit in chrome for a web application I am developing for udacity mobile web specialist project and I am getting a score of 85 for performance. 我正在为udacity移动Web专家项目开发的Web应用程序进行chrome审核,并且性能得分为85。 I need to get a score of 90 and above to pass the project. 我需要获得90分以上的分数才能通过该项目。

Here are the diagnostics - 这是诊断信息-

  1. Uses inefficient cache policy on static assets 14 assets found 对发现的14个资产使用无效的缓存策略

    A long cache lifetime can speed up repeat visits to your page. 较长的缓存生存期可以加快对页面的重复访问。

  2. Has significant main thread work 6,520 ms 具有显着的主线程工作6,520 ms

    Consider reducing the time spent parsing, compiling and executing JS. 考虑减少花在解析,编译和执行JS上的时间。 You may find delivering smaller JS payloads helps with this. 您可能会发现提供较小的JS有效负载对此有所帮助。

  3. JavaScript boot-up time is too high 3,810 ms JavaScript启动时间太长3,810毫秒

    Consider reducing the time spent parsing, compiling, and executing JS. 考虑减少花在解析,编译和执行JS上的时间。 You may find delivering smaller JS payloads helps with this 您可能会发现提供较小的JS负载有助于解决此问题

Here is part of my service worker script. 这是我的服务工作者脚本的一部分。 - --

importScripts("/js/idb.js");
importScripts("/js/dbhelper.js");
const staticCacheName = 'restaurant-1';
const resourcesToCache = [
'/',
'index.html',
'restaurant.html',
'css/styles.css',
'js/idb.js',
'js/dbhelper.js',
'js/restaurant_info.js',
'js/main.js',
'sw.js',
'img/1_small.jpg',
'img/1_medium.jpg',
'img/1_large.jpg',
'img/2_small.jpg',
'img/2_medium.jpg',
'img/2_large.jpg',
'img/3_small.jpg',
'img/3_medium.jpg',
'img/3_large.jpg',
'img/4_small.jpg',
'img/4_medium.jpg',
'img/4_large.jpg',
'img/5_small.jpg',
'img/5_medium.jpg',
'img/5_large.jpg',
'img/6_small.jpg',
'img/6_medium.jpg',
'img/6_large.jpg',
'img/7_small.jpg',
'img/7_medium.jpg',
'img/7_large.jpg',
'img/8_small.jpg',
'img/8_medium.jpg',
'img/8_large.jpg',
'img/9_small.jpg',
'img/9_medium.jpg',
'img/9_large.jpg',
'img/10_small.jpg',
'img/10_medium.jpg',
'img/10_large.jpg',
'https://unpkg.com/leaflet@1.3.1/dist/images/marker-icon.png',
'https://unpkg.com/leaflet@1.3.1/dist/leaflet.css',
'https://unpkg.com/leaflet@1.3.1/dist/leaflet.js'
];

For 2 and 3. - Whenever I try to compress or minify my javascripts, I always get error like - Unexpected token:. 对于2和3。-每当我尝试压缩或缩小我的JavaScript时,我总是会收到类似-错误的令牌:的错误。 I know for sure the js isn't having errors. 我知道js肯定没有错误。

How can I fix these so I could get a perfomance score of 90 or above ? 我该如何解决这些问题,使我的性能得分达到90或以上?

Fixed. 固定。 I had to change part of my service worker code (sw.js) 我必须更改部分服务人员代码(sw.js)

from

self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request, { ignoreSearch: true }).then(response => {
  return response || fetch(event.request).then(res => {
    return caches.open(staticCacheName).then(cache => {
      cache.put(event.request, res.clone());
      return res;
    })
  });
})
);
});

to

self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request, { ignoreSearch: true }).then(response => {
  return response || fetch(event.request).then(res => {
    if (!res || res.status !== 200 || res.type !== 'basic') {
      return res;
    }
    return caches.open(staticCacheName).then(cache => {
      cache.put(event.request, res.clone());
      return res;
    })
  });
})
);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM