2

I'm rewriting multiple paths in Firebase Hosting to a Cloud Function that always returns the same result. I need to invoke the function once, cache its result, and return it for any subsequent requests to any of these paths. However, as the documentation states, the cached content is served based on:

  • The hostname
  • The path
  • The query string
  • The content of the request headers specified in the Vary header

So, if a different URL is requested, the function will be invoked again. But is there a way to avoid that? Setting the Cache-Control header does prevent the function from invoking again when the same URL is requested, but not when a different one is.


Here is my Hosting and Functions configuration:

firebase.json:

{
  "hosting": {
    "rewrites": [
      {
        "source": "**",
        "function": "myFunction"
      }
    ]
  }
}

functions/index.ts:

import * as functions from "firebase-functions"

export const myFunction = functions.https.onRequest((req, res) => {
    res.set("Cache-Control", "public, max-age=31536000")
    res.send("This is a Cloud Function.")
})
Max
  • 739
  • 2
  • 8
  • 23

2 Answers2

2

The caching behavior of Firebase Hosting (and web browsers) is always dependent on the URL path.

There are a couple ways you could try to work around this, depending on your goals:

  1. Use a redirect (for instance, to /) instead of the rewrite. The the cloud function can then serve content only on a known (and cacheable) path.
  2. Serve a static page instead of a cloud function on every path, then have that static page use javascript to call your cloud function on a known, cached path. The initial static page wouldn't be cached, but it should be faster than a function.
Kiana
  • 1,415
  • 11
  • 17
  • Thank you for your suggestions, however, in my case, I need this functionality to work around a Firebase Hosting limitation that it only allows a single 404 page, which thus cannot be localized: https://stackoverflow.com/q/62177351/11105280. But this is the right answer anyway. – Max Jul 22 '20 at 10:06
0

The integration between Firebase Hosting and Cloud Functions/Cloud Run allows your (Cloud Functions/Cloud Run) code to control the content of the URL that was requested from Firebase Hosting.

You can modify other hosted files, but will have to call the Firebase Hosting API for this. For an example of this, see How to update a file that I deployed to Firebase Hosting?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Sorry, I think you misunderstood my question. The idea is that the function always returns the same result, and it would make sense to cache its result once and return it for any URL that is requested from Firebase Hosting to minimize the number of function invocations and the user's waiting time. – Max Jul 17 '20 at 15:50
  • Ah, I see. I don't know of any way to do the mapping of unique URLs to the same response earlier. I'd recommend normalizing the URLs on the client, so that requests that would give the same response are always going to the same URL – Frank van Puffelen Jul 17 '20 at 16:11
  • Normalizing URLs on the client might be a good idea, however, in my case, I'm trying to reuse the response for localizing a 404 page as described in this answer: https://stackoverflow.com/a/62177352/11105280, so this is not an option. – Max Jul 19 '20 at 16:00