fastify-compress icon indicating copy to clipboard operation
fastify-compress copied to clipboard

How to cache the compressed responses?

Open adaboese opened this issue 2 years ago • 1 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

I assume there is a not insignificant overhead of using compression.

Instead, it would be cheaper to cache (or even store on disk) copies of the compressed content.

Is there a hook or another method I could use to achieve this instead of cache-ing every asset adhoc?

Motivation

No response

Example

No response

adaboese avatar Jan 17 '24 21:01 adaboese

Generically, this is better done at the CDN/reverse proxy level by using the Cache-Control headers at the application level. What makes caching the compressed data hard for this module is knowing when to serve the cached response.

To know if we can serve the cached response, we need to hash the original version, but to do so, we need to have it fully in memory. If it is a small payload, it's likely better to transfer it uncompressed. Larger payloads usually must be sent as stream; otherwise, it would exhaust the memory, but if they are streams, we won't know their full hash before we have read them fully.

mcollina avatar Jan 18 '24 09:01 mcollina

See precompressed: https://github.com/fastify/fastify-static?tab=readme-ov-file#precompressed

It partially achieves your desired behavior

gurgunday avatar Apr 01 '24 15:04 gurgunday