Search code examples
firebasehttp-headersfirebase-hosting

How does Firebase Hosting behaves when firebase.json has two (or more) sets of "headers" rules that apply to a specific path?


If you set 2 headers rules on Firebase Hosting firebase.json file as a generic rule and a more specific rule, in a way that some paths will match both of them, how does Firebase Hosting behave in that situation? Does it override and merge the header values in order of the array?

For example:

firebase.json

"headers": [
  {
    "source": "/**",
    "headers": [
      { "key": "Cache-Control", "value": "max-age=3600" },
      { "key": "Vary",          "value": "User-Agent" }
    ]
  },
  {
    "source": "/api/**",
    "headers": [
      { "key": "Cache-Control",               "value": "no-cache" },
      { "key": "Access-Control-Allow-Origin", "value": "https://www.example.com" }
    ]    
  }
]

Can I assume the /api/** calls will get the overwrite / merge result from the two set of header rules in that order? I mean, the order is important here, right?

The result for /api would be:

Cache-Control: no-cache
Vary: User-Agent
Access-Control-Allow-Origin: https://www.example.com

Solution

  • I've just test and here are the results:

    It will overwrite and merge the headers in the order that they appear.

    For example:

    "headers": [
      {
        "source": "/**",
        "headers": [
          { "key": "Cache-Control", "value": "max-age=3600" },
          { "key": "Vary",          "value": "User-Agent" }
        ]
      },
      {
        "source": "/api/**",
        "headers": [
          { "key": "Cache-Control",               "value": "no-cache" },
          { "key": "Access-Control-Allow-Origin", "value": "https://www.example.com" }
        ]    
      }
    ]
    

    From the code above, for an /api/something call, it would return the following:

    Cache-Control: no-cache
    Vary: User-Agent
    Acess-Control-Allow-Origin: https://www.example.com