Search code examples
firebasepattern-matchingglobfirebase-hosting

Exclude sw.js from the long term caching with glob pattern matching


In order to enable long term caching, per the Firebase documentation, I have this in the headers configuration:

"headers": [
  {
    "source": "**/*.@(jpg|jpeg|gif|png|js|css)",
    "headers": [
      {
        "key": "Cache-Control",
        "value": "max-age=172800"
      }
    ]
  }
]

But I'm also serving a sw.js (service worker) for the app, which I don't want to be cached at all. First thing that comes to mind is to exclude it from the glob matching, which I need help for. Otherwise, if you think there's a better way, do tell. Thanx.


Solution

  • Modified the pattern and added new one that specifies no-cache for the sw.js

    "headers": [
      {
        "source": "**/!(sw).@(jpg|jpeg|gif|png|js|css)",
        "headers": [{ "key": "Cache-Control", "value": "max-age=31536000" }]
      },
      {
        "source": "**/sw.js",
        "headers": [{ "key": "Cache-Control", "value": "no-cache" }]
      }
    ]