elixir-plug / plug

Compose web applications with functions
https://hex.pm/packages/plug
Other
2.84k stars 582 forks source link

Add `Plug.Conn.register_before_chunk/2` #1154

Closed feynmanliang closed 1 year ago

feynmanliang commented 1 year ago

Resolves #1153

An example use case where we cache the response body for a regular HTTP request and individual chunks for a chunked response like SSEs:

              # register lifecycle callback to cache response
              conn
              |> register_before_send(fn conn ->
                if conn.resp_body do
                  Dreamcatcher.SemanticCache.API.put(key, conn.resp_body)
                end

                conn
              end)
              |> register_before_chunk(fn conn, chunk ->
                if chunk do
                  Dreamcatcher.SemanticCache.API.append_chunk(key, chunk)
                end

                conn
              end)
whatyouhide commented 1 year ago

I generally like the idea and can't think of a good reason not to do this.

josevalim commented 1 year ago

:green_heart: :blue_heart: :purple_heart: :yellow_heart: :heart: