简体   繁体   English

利用浏览器缓存 - Firebase / Angular 5

[英]Leverage browser caching - Firebase/Angular 5

I ran my Angular 5 site through Google's PageSpeed Insights, and it barked about leveraging browser caching, listing the following files: 我通过Google的PageSpeed Insights运行了我的Angular 5网站,并且在利用浏览器缓存方面大肆宣传,列出了以下文件:

https://use.typekit.net/####.css (10 minutes)
https://www.googletagmanager.com/gtm.js?id=GTM-#### (15 minutes)
https://####.firebaseapp.com/assets/svgs/###.svg (60 
minutes)

Here is how my firebase.json file in formatted: 这是我的firebase.json文件格式化的方式:

    {
  "hosting": {
    "public": "dist",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ],
    "headers": [
      {
        "source": "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
        "headers": [
          {
            "key": "Access-Control-Allow-Origin",
            "value": "*"
          }
        ]
      },
      {
        "source": "**/*.@(js|css)",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=604800"
          }
        ]
      },
      {
        "source": "**/*.@(jpg|jpeg|gif|png|svg)",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=604800"
          }
        ]
      },
      {
        "source": "404.html",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=300"
          }
        ]
      }
    ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix $RESOURCE_DIR run lint"
    ],
    "source": "functions"
  }
}

Most of that code is based on the following post: How to Leverage Browser Caching in Firebase hosting 大部分代码基于以下帖子: 如何在Firebase托管中利用浏览器缓存

However, it doesn't seem to be working. 但是,它似乎没有起作用。 I'm still getting the same error when I rerun PageSpeed Insights. 当我重新运行PageSpeed Insights时,我仍然遇到同样的错误。 How do I solve this issue? 我该如何解决这个问题? I'm very new with Angular, so a specific answer would be appreciated. 我对Angular非常新,所以我将非常感谢您的具体回答。 Thanks in advance! 提前致谢!

There are 2 solutions 有两种解决方案

1. Serve the external files from your domain. 1.提供您域中的外部文件。

Download the below 3 files and serve them from your own application. 下载以下3个文件,并从您自己的应用程序中提供。 This way you can control the cache times. 这样您就可以控制缓存时间。 Make sure to keep an updated copy of 3rd party css and js when they update. 确保在更新时保留第三方css和js的更新副本。

https://use.typekit.net/####.css (10 minutes) .  
https://www.googletagmanager.com/gtm.js?id=GTM-#### (15 minutes) .   
https://####.firebaseapp.com/assets/svgs/###.svg (60 
minutes)

2. Hack fix for Google Speed test 2.针对Google Speed测试的Hack修复

Do understand the Google speed test is just a recommendation and you need not follow it strictly. 请理解Google速度测试只是一个推荐,您无需严格遵守。 Some other tool like https://www.webpagetest.org/ may give you better results. 其他一些工具,如https://www.webpagetest.org/可能会给你更好的结果。 However, you can use the below hack to include the affected css and js dynamically if the browser agent is not Google Speed Insight. 但是,如果浏览器代理不是Google Speed Insight,则可以使用以下hack动态包含受影响的css和js。 For Google speedtest, the affected resource don't load, thereby getting a better score in speed test. 对于Google speedtest,受影响的资源不会加载,从而在速度测试中获得更好的分数。

<script>
if (navigator.userAgent.indexOf("Speed Insights")==-1) {
...
...
</script>

You can safely ignore those 3 suggestions from Google PageSpeed (and lightouse ... and similar tools). 您可以放心地忽略来自Google PageSpeed(以及灯光......和类似工具)的3条建议。

Infact there is some irony on the fact that many files served from a Google CDN (fonts for example) throw that error - or similar errors - in Google PageSpeed and other tools (like Lighthouse). 事实上有一些讽刺的事实是,从谷歌CDN(例如字体)提供的许多文件会在Google PageSpeed和其他工具(如Lighthouse)中抛出该错误或类似错误。

Of course I think Google should make something about those tools, they should take into account that. 当然我认为谷歌应该对这些工具做些什么,他们应该考虑到这一点。

Google PageSpeed is not about coming first in a kind of race or making 100 in points ... is just a general (and basic) indicator of how we are doing. 谷歌PageSpeed并不是要在一场比赛中获得第一名或者获得100分......这只是我们如何做的一般(和基本)指标。 Sometimes it signals a problem you do not have, sometimes doesn't signal a problem you have. 有时它表示您没有的问题,有时并不表示您遇到的问题。

The problem is when a SEO "specialist" comes to you with the result of those checks (I think many developers can relate to that ...). 问题是当一个搜索引擎优化“专家”带来这些检查的结果时(我想许多开发人员可以与之相关......)。

Please Google do something about that! 请谷歌做点什么!

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

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