0
votes

I have a SPA (sing page app) built with React + Firebase.

This is the flow:

firebase.json

  • Rewrites everything to my cloud run express server.
  • I have a connected custom domain on my Firebase Hosting: www.example.com
"rewrites": [
  {
    "source": "**",
    "run": {
      "serviceId": "server",
      "region": "us-central1"
    }
  }
],

In Firebase Hosting docs, we can see that Firebase Hosting serves content over the CDN with gzip enabled by default.

enter image description here

Given that fact, does it make send to turn on the compression on my express server over on Cloud Run?

Example:

const app = express();      // INITIALIZE EXPRESS APP

const publicFolder = path.resolve(__dirname,"../public");

app.use(compression());
app.use(express.static(publicFolder));

I'm guessing that it makes sense, because Firebase Hosting compresses the data from the Hosting CDN to users, and my express server will compress data from Cloud Run to Hosting CDN, is this correct?

1

1 Answers

1
votes

From what the doc is saying, static content are automatically cached on the CDN and served with right compression to your users. However by default, requests handled by backend code (Cloud Functions or Cloud Run) do not cache on the CDN due to its dynamic nature.

Instead of setting compression through your app, configure caching of dynamic content by setting up Cache-Control and marking the cache as public. Once the backend content is cached, then Firebase Hosting will compress it depending on what the user can handle (gzip or Brotli).

For example:

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

Note: Only GET and HEAD requests can be cached. HTTPS requests using other methods are never cached.


See more at https://firebase.google.com/docs/hosting/manage-cache.