fastify / fastify-static

Plugin for serving static files as fast as possible
MIT License
433 stars 97 forks source link

setting content-length #440

Closed gsimko closed 7 months ago

gsimko commented 7 months ago

Prerequisites

🚀 Feature Proposal

I propose to set content-length when it's available. I'm using the preCompressed option so the size is trivially available. I'm less certain whether it is doable for on-the-fly encoding. Wdyt?

Motivation

No response

Example

No response

mcollina commented 7 months ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

gurgunday commented 7 months ago

This feature would be a good addition, and it would be even better if we could do it for uncompressed files as well (by using statSync or something)

gsimko commented 7 months ago

Looks like this is not a fastify problem. Fastify-static relies on fastify-send and that one already sets content-length by using stat. By locally running the query and checking the response I could confirm that it's set.

In my case it must be some other component that's causing the trouble. I noticed that the browser uses http2 requests while I configured fastify to use http1 so there must be a proxy somewhere in the setup doing the conversion. I guess that one drops the content-length header. @gurgunday could that explain why you also observed this problem?

gurgunday commented 7 months ago

That's interesting, I don't see the header in my production app either, let me try on localhost and come back on this

gurgunday commented 7 months ago

Oh yeah, it's compress that's removing it for me, which makes sense

It might indeed be a proxy like Cloudflare that's dropping it too

Closing the issue if you don't have any other problems, feel free to comment again otherwise