Firebase托管自动缓存在.js文件/service-worker.js中使用Bundle哈希更改进行Busting

时间:2018-02-20 14:21:53

标签: reactjs firebase webpack create-react-app firebase-hosting

目前我在firebase.json文件中使用此设置,service-worker.js文件将导致捆绑包从缓存中清除并下载新鲜但是需要硬盘控制+ R刷新才能执行此操作,它没有自动发生,我的通配符模式匹配** / *。@(js | css)现在导致我在/service-worker.js上的无缓存规则也不起作用,但它不是每当我部署下一个版本时,强制用户强制刷新控制+ R,我需要一个更好的解决方案,而不是将我的捆绑上的max-age设置为0.

有什么想法吗?

"hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "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=86400"
          }
        ]
      },
      {
        "source": "**/*.@(jpg|jpeg|gif|png)",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=604800"
          }
        ]
      },
      {
        "source": "/service-worker.js",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "no-cache"
          }
        ]
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

我有类似的问题,但我想出了如何。在我的案例中注意Service Worker文件是sw.js。你可以在下面找到我的配置。我在为匹配所有JS和CSS文件的通配符规则之前添加了sw.js的单独规则:

{
  "hosting": {
    "public": "public",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ],
    "headers": [
      {
        "source": "**/*.@(jpg|jpeg|gif|png|svg|ico)",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=7200"
          }
        ]
      },
      {
        "source": "sw.js",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=0"
          }
        ]
      },
      {
        "source": "*/*.@(js|css)",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=3600"
          }
        ]
      },
      {
        "source": "manifest.json",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=86400"
          }
        ]
      }
    ]
  }
}