【问题标题】:How to Leverage Browser Caching in Firebase hosting如何在 Firebase 托管中利用浏览器缓存
【发布时间】:2017-03-15 12:20:47
【问题描述】:

我在 Google 的 firebase 上托管了我的个人博客。我的博客基于 jekyll。 Firebase 提供了 firebase.json 文件,项目所有者可以从中修改 http 标头。

我有我的 css 文件 https://blogprime.com/assets/css/init.csshttps://blogprime.com/assets/font/fontname.woff 中的字体(http 缓存控制不起作用)

我的图像存储在 ::https://blogprime.com/assets/img/imagename.entension 中(http 缓存控制工作)。

尽管图像和 css 都有,但字体是从根目录深两个目录。

现在这是我的 .json 文件代码..

{"hosting": 
    {"public": "public",
    "headers": [
        {"source" : "**/*.@(eot|otf|ttf|ttc|woff|css)",
        "headers" : [
            {"key" : "Access-Control-Allow-Origin",
            "value" : "*"}]
        }, 
        {"source" : "**/*.@(jpg|jpeg|gif|png)",
        "headers" : [
            {"key" : "Cache-Control",
            "value" : "max-age=30672000"
            }]
        }, 
        {"source" : "404.html",
        "headers" : [
            {"key" : "Cache-Control",
            "value" : "max-age=300"
            }]
        }]
    }
}

在添加此之前,我的图像和所有内容都有 1 小时的缓存寿命......但现在只有我的 css 文件和字体文件有 1 小时的缓存寿命。

您能否告诉我如何修复我的 css 文件的“利用浏览器缓存”。我认为他们的目录链接结构存在一些问题,我有 "source" : "/*.@(eot|otf|ttf|ttc|woff|css)",**。我真的不知道如何解决它。

你可以检查谷歌页面速度测试..

https://developers.google.com/speed/pagespeed/insights/?url=https%3A%2F%2Fblogprime.com%2Fwordpress%2Fdns-prefetch-in-wordpress.html

【问题讨论】:

    标签: caching firebase http-headers firebase-hosting


    【解决方案1】:

    我只是将我的投资组合网站设为 99/100。

    谷歌说:

    我们建议至少 1 周的缓存时间,最好最多 1 周 静态资产的年份。

        "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)",
          "headers" : [ {
            "key" : "Cache-Control",
            "value" : "max-age=604800"
          } ]
        }, {
          // Sets the cache header for 404 pages to cache for 5 minutes
          "source" : "404.html",
          "headers" : [ {
            "key" : "Cache-Control",
            "value" : "max-age=300"
          } ]
        } ]
    

    使用这个,它对我有用。

    【讨论】:

    • 刚刚把它放到了我的 firebase.json 中,它把这个挑战从 TODO 转移到了已经完成。谢谢。
    • 注意:404 页面的cache-control 标头仅适用于/404.html URL。将应用您的其他静态资产的 cache-control 标头,即使它是 404s。我已经看到 firebase 为 确实 存在的资产发送 404,这些 404 响应将被用户的浏览器缓存。
    • 谢谢。只有几个可能的 mods:Google 字体的 URL 像 https://fonts.googleapis.com/css?family=Catamaran:100,200,300,400,500,600,700,800,900。还有像vendor/font-awesome/fonts/fontawesome-webfont.woff2?v=4.7.0这样的字体真棒。
    • 还可以添加.svg 以及视频和音频文件格式。
    • 如果您将 javascript/css 文件缓存 1 年,这是否意味着任何更改都不会在一年内更新/反映在您的网站上?
    猜你喜欢
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多