Firebase Hosting Multiple Sites

March 12, 2020

Create multiple sites

Goto Firebase Console -> Hosting, then Advance -> Add another site.

Select a site name, also known as resource name.

Testing

Edit firebase.json.

  • Change hosting to array of objects.
  • Each hosting object should have a target field (known as target name).
  • Assign a different public directory for each hosting object
{
  "hosting": [
    {
      "target": "main",
      "public": "main/public",
      "ignore": [
        "firebase.json",
        "**/.*",
        "**/node_modules/**",
        "**/public-*/**"
      ],
      "trailingSlash": false
    },
    {
      "target": "app",
      "public": "app/public",
      "ignore": [
        "firebase.json",
        "**/.*",
        "**/node_modules/**",
        "**/app/public-*/**"
      ],
      "rewrites": [
        {
          "source": "/test",
          "function": "test"
        },
        {
          "source": "**",
          "function": "nuxtssr"
        }
      ],
      "headers": [
        {
          "source": "**/_nuxt/*.*",
          "headers": [{
            "key": "Cache-Control",
            "value": "public, max-age=31536000"
          }]
        }
      ],
      "trailingSlash": false
    }
  ],
}

Usage

firebase serve --only hosting:TARGET_NAME

or

firebase serve --only functions,hosting:TARGET_NAME

Deployment

Assign target name to resource name.

firebase target:apply hosting TARGET_NAME RESOURCE_NAME

The command above will update .firebaserc

{
  "projects": {
    "default": "PROJECT_ID"
  },
  "targets": {
    "PROJECT_ID": {
      "hosting": {
        "TARGET_NAME": [
          "RESOURCE_NAME"
        ],
        "TARGET_NAME02": [
          "RESOURCE_NAME02s"
        ]
      }
    }
  }
}

Deploy

firebase deploy --only hosting:TARGET_NAME

NOTE: Cloud functions are shared between multiple sites (actually, functions are independent from hosting). Each hosting site can has its own rewrites via firebase.json.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.