Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serveStatic): add precompressed option #3366

Merged
merged 6 commits into from
Sep 8, 2024

Conversation

inetol
Copy link
Contributor

@inetol inetol commented Sep 3, 2024

Closes #3363

Allows to serve all compatible pre-compressed files transparently to the client, otherwise fallback to serve the original file.

Screenshot from 2024-09-03 15-03-44

In case there are several files with various encodings (e.g., .gz and .br), it has priority to use brotli, then zstd and finally gzip.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@inetol inetol marked this pull request as ready for review September 3, 2024 22:25
@yusukebe
Copy link
Member

yusukebe commented Sep 5, 2024

Hi @inetol

Thank you for the PR. It's interesting. But, is there any specification of precompressed? Is this used in general? Honestly, I didn't know there was a rule of precompressed.

@inetol
Copy link
Contributor Author

inetol commented Sep 5, 2024

AFAIK, it looks more like a helper to me, nginx and Caddyserver have a dedicated directive to this but at no time it is said that it is part of a spec, so its implementation may differ.

In cases where for some reason it is not possible to compress a file at the time of the request (because its too large, because the server is not compatible or for other reasons) this may be a solution.

Copy link

codecov bot commented Sep 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.78%. Comparing base (3500404) to head (2e04502).
Report is 8 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #3366      +/-   ##
==========================================
+ Coverage   95.77%   95.78%   +0.01%     
==========================================
  Files         152      152              
  Lines        9187     9211      +24     
  Branches     2818     2681     -137     
==========================================
+ Hits         8799     8823      +24     
  Misses        388      388              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe
Copy link
Member

yusukebe commented Sep 6, 2024

@inetol

Thank you for your explanation.

I think this is a nice feature, but I'd like to know others' thoughts on whether we should accept it.

@ryuapp @nakasyou @usualoma and others, What do you feel about this feature?

@ryuapp
Copy link
Contributor

ryuapp commented Sep 7, 2024

I haven't checked the detailed implementation, but I think it's a good feature for Hono, which prioritizes performance.

As far as I searched, Fastify, Koa, and SvelteKit have similar features. It would be worth implementing.
https://github.com/fastify/fastify-static?tab=readme-ov-file#precompressed
https://github.com/koajs/static?tab=readme-ov-file#options
https://kit.svelte.dev/docs/adapter-static#options-precompress

@MathurAditya724
Copy link
Contributor

Someone from the community has created this PR for the node server serveStatic - honojs/node-server#195. IMO, this is a much clearer and more flexible approach to resolving this issue.

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

yusukebe commented Sep 8, 2024

@ryuapp @MathurAditya724 Thanks for your comment.

Looks good! I'll merge this into the next branch which I'll create for the next minor release, v4.6.0, later.

@inetol Thank you for your contribution!

@yusukebe yusukebe changed the base branch from main to next September 8, 2024 06:55
@yusukebe yusukebe merged commit c8d5e34 into honojs:next Sep 8, 2024
14 checks passed
@inetol inetol deleted the serve-precompressed branch September 8, 2024 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow serving pre-compressed files with serveStatic
4 participants