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

Content-Encoding header is gzip without compression middleware #3449

Closed
poeck opened this issue Sep 25, 2024 · 9 comments · Fixed by honojs/website#507
Closed

Content-Encoding header is gzip without compression middleware #3449

poeck opened this issue Sep 25, 2024 · 9 comments · Fixed by honojs/website#507
Labels

Comments

@poeck
Copy link

poeck commented Sep 25, 2024

What version of Hono are you using?

4.5.8

What runtime/platform is your app running on?

Cloudflare Workers

What steps can reproduce the bug?

  1. Create a basic hono application without any middleware
  2. Add a route
  3. Call the route and check the headers

What is the expected behavior?

The Content-Encoding should probably not be present if there is no compress middle ware applied.

What do you see instead?

The Content-Encoding is automatically set to gzip.

Additional information

When trying to stream text with streamText from hono/streaming you need to manually add c.header("Content-Encoding", "none") for it to work correctly.

@poeck poeck added the triage label Sep 25, 2024
@yusukebe yusukebe added not bug and removed triage labels Sep 25, 2024
@yusukebe
Copy link
Member

@poeck

It's not a bug. Cloudflare adds the content-encoding automatically. For instance, it reproduces with the code that does not use Hono:

export default {
  fetch: () => new Response('Hello'),
}

@poeck
Copy link
Author

poeck commented Sep 25, 2024

Oh, I see. Would it be worth considering overwriting this header for streams? I believe it's theoretically possible to stream data with gzip as well, but I couldn't get it to work quickly and it took me forever to figure out that this was the issue. 😅

To be honest, I don't know exactly how gzip works with HTTP, but is the header even correct, like is the response even gzipped? As soon as I overwrote the header with "none", the stream worked perfectly without me having to do any decoding myself (like gzip decoding, for example).

@yusukebe
Copy link
Member

yusukebe commented Sep 26, 2024

Hi @poeck

Thank you for the comment. Actually, it does not work on Cloudflare Workers without modifying the Content-Encoding header. In this case, it's good to set Identity instead of None:

c.header('Content-Encoding', 'Identity')

I can't find good documentation explaining Identity, but if it's set, it works well. It's used in the JSX Renderer.

I think it's a good idea to setContent-Encoding: 'Identity' in the streaming helper as default.

cc: @sor4chi @watany-dev What do you think of it?

@sor4chi
Copy link
Contributor

sor4chi commented Sep 26, 2024

Hi @yusukebe.
I think it's a good idea. Unexpected behavior should be avoided as much as possible.

@yusukebe
Copy link
Member

@sor4chi

I'm trying to add a change to set Content-Encoding: 'Identity' to streamText(). It's okay, but how about streamSSE()? I've tried using it on Cloudflare Workers without Content-Encoding: 'Identity', but it works well. Is it okay not to add the header? Do you have any idea?

@yusukebe
Copy link
Member

yusukebe commented Oct 1, 2024

In the streamSSE() case, it does not add a Content-Encoding header on Cloudflare Workers. So, I think it's okay that we don't add it to streamSSE(). Just streamText() is okay.

@yusukebe
Copy link
Member

yusukebe commented Oct 3, 2024

Mainly to @sor4chi

Thank you for your review of #3476.

I've re-thought about this issue.

As the issue on Cloudflare Workers SDK: cloudflare/workers-sdk#6577, the fact that streaming is not enabled on Wrangler may be a Wrangler-side problem. This means streaming works well on Cloudflare's production, but it does not work on the current Wrangler.

If so, it's not good to fix this issue on the framework (Hono) side. The Cloudflare internal team has started investigating it, but we don't know when it will be resolved.

We can make some choices

  1. Implement feat(streaming): add Identity as Content-Encoding for streamText() #3476 as a workaround and remove this if the matter is fixed on Wrangler.
  2. Do not do anything now. But add the description "add the header Content-Encoding: 'Identity' on the Wrangler" on Streaming Helerp doc.

I prefer "2". What do you think of it?

@sor4chi
Copy link
Contributor

sor4chi commented Oct 3, 2024

Okay, I think 2 is fine with me too.

@yusukebe
Copy link
Member

yusukebe commented Oct 4, 2024

@poeck @sor4chi

I've added the description on the docs honojs/website#507 to close this issue.

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants