Search code examples
firebasegoogle-cloud-functionsfirebase-hosting

How to by-pass cloud function call and serve it from cdn from firebase hosting


I have a web application written in Firebase and I am deploying it to Firebase hosting. In the web application, I am calling a cloud function which returns kind of static data(data changes in a day or two). Is it possible to bypass the cloud function call execution and serve it directly through Firebase global CDN. This is what I already tried in firebase json.

{
  "hosting": {
    "target": "flutter",
    "public": "build/web",
    "headers": [
      {
        "source": "**",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "'public, max-age=300, s-maxage=600'"
          }
        ]
      }
    ],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      },
      {
        "source": "/getData/**",
        "destination": "/index.html"
      },
{
        "source": "https://us-central1-xxxxx.cloudfunctions.net/getData",
        "destination": "/index.html"
      }
    ]
  }
}

Thanks in advance


Solution

  • You should review the documentation on managing the cache behavior of Firebase Hosting.

    Your function needs to set a cache control header to indicate how long its response should be cached. If it does that, then Firebase Hosting will serve the cached response until the specified expiration.

    res.set('Cache-Control', 'public, max-age=300, s-maxage=600');