How to enable text compression when fetching Firestore documents from a Cloud Function?

26 Views Asked by At

How can I do text compression when retrieving Firestore documents via a Cloud Function?

Let me explain my issue:

I am using a Cloud Function (2nd gen) to return a document from Firestore like so:

  try {
    const doc = await db.doc(storyPath).get()
    if (!doc.exists) {
      res.status(404).send('No such document!')
    } else {
      res.status(200).send(doc.data())
    }
  } catch (err) {
    error('Error getting document:', err)
    res.status(500).send('Error getting document')
  }
})

However, Google Lighthouse is penalizing me with the following message:

Text-based resources should be served with compression (gzip, deflate or brotli) to minimize total network bytes.
URL
Transfer Size
Potential Savings
myapp.web.app 1st Party
384.3 KiB   335.9 KiB
…stories/aidens-first-spell_pdFmlV
384.3 KiB
335.9 KiB

I use Firebase Hosting and I see that brotli/gzip is included automatically, but what about cloud functions?

Do I need to manually include some kind of text compression ?

For additional context, here is the request/response headers for this fetched document:

Response:

Accept-Ranges:
bytes
Alt-Svc:
h3=":443";ma=86400,h3-29=":443";ma=86400,h3-27=":443";ma=86400
Cache-Control:
private
Content-Length:
393495

Content-Type:
text/html;charset=utf-8
Date:
Tue, 19 Dec 2023 14:35:29 GMT
Server:
Google Frontend
Strict-Transport-Security:
max-age=31556926; includeSubDomains; preload
Vary:
cookie,need-authorization, x-fh-requested-host, accept-encoding
X-Cache:
MISS
X-Cache-Hits:
0
X-Cloud-Trace-Context:
fd52c17b88def1d5c73787f6a8a7973c;o=1
X-Country-Code:
US
X-Orig-Accept-Language:
en-US,en;q=0.9
X-Powered-By:
Nuxt
X-Served-By:
cache-stl760066-STL
X-Timer:
S1702996529.754829,VS0,VE360

Request:

:authority:
bmyapp.web.app
:method:
GET
:path:
/users/kRUx8ZxLoUd0y7kTcxgPfwTiGu13/stories/aidens-first-spell_pdFmlV
:scheme:
https
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Accept-Encoding:
gzip, deflate, br
Accept-Language:
en-US,en;q=0.9
Cache-Control:
no-cache
Dnt:
1
Pragma:
no-cache
Sec-Ch-Ua:
"Not A(Brand";v="99", "Microsoft Edge";v="121", "Chromium";v="121"
Sec-Ch-Ua-Mobile:
?0
Sec-Ch-Ua-Platform:
"Windows"
Sec-Fetch-Dest:
document
Sec-Fetch-Mode:
navigate
Sec-Fetch-Site:
same-origin
Sec-Fetch-User:
?1
Upgrade-Insecure-Requests:
1
User-Agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0
0

There are 0 best solutions below