Skip to content

Conversation

@Jdyn
Copy link

@Jdyn Jdyn commented Sep 6, 2025

Introduces put_request_headers to merge conn headers into the request before making upstream fetch calls. Also renames put_headers to put_conn_headers for clarity.

Closes #105

I found a few ways to approach this

One way I considered was to just override the response headers after we receive the response with the original headers with the CORS plug:

defp put_headers(conn, headers) do
  headers
  |> Map.delete("transfer-encoding")
  |> Enum.reduce(conn, fn {header, values}, conn ->
    Enum.reduce(values, conn, fn value, conn ->
      Plug.Conn.put_resp_header(conn, header, value)
    end)
  end)
+  |> Phoenix.Sync.Plug.CORS.call()
end

Depends on what behavior you guys consider correct, But I assumed its more correct to merge the client request headers with the original conn request headers before sending the request, since Electric handles the headers logic upstream anyway.

I spent a lot of time trying to get a test setup for this, but couldn't figure out how to test sync_render in http mode. My tests weren't going to the paths I expected when using the sync_render in the Sandbox. Feel free to modify stuff.

Introduces put_request_headers to merge connection headers into the request before making upstream fetch calls. Also renames put_headers to put_conn_headers for clarity.
Copilot AI review requested due to automatic review settings September 6, 2025 22:30

This comment was marked as outdated.

Jdyn added 2 commits September 6, 2025 16:23
Refactors put_request_headers to correctly merge multiple header values and updates fetch_upstream to pass req_headers directly. This ensures headers are properly combined when forwarding requests upstream.
@Jdyn Jdyn requested a review from Copilot September 6, 2025 23:26
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where request headers from the client connection were not being forwarded to upstream Electric requests in the fetch_upstream function. The change ensures client headers are properly merged with the request before making upstream calls.

  • Adds a new put_req_headers function to merge client connection headers into the request
  • Renames put_headers to put_resp_headers for better clarity between request and response header handling
  • Updates the fetch_upstream flow to include client headers in upstream requests

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Jdyn Jdyn closed this Sep 10, 2025
@magnetised
Copy link
Contributor

@Jdyn I was about to have a proper look at this -- haven't had much time this week, other priorities interfering. Why did you close?

@Jdyn
Copy link
Author

Jdyn commented Sep 10, 2025

Oh, I had just figured you would have your own sol. Will re-open but also not entirely sure how elegant this solution is. Happy to make any changes to get it in. I have been using the branch in prod and it fully works for me.

@Jdyn Jdyn reopened this Sep 10, 2025
@magnetised
Copy link
Contributor

magnetised commented Sep 15, 2025

Oh, I had just figured you would have your own sol.

I wondered what your reasons for closing were - whether the patch didn't work or something.

@Jdyn rather than request a bunch of changes plus a test I did end up making my own pr based on this: #107 -- your basic approach seemed valid, I just tweaked to handle compatibility with the elixir client's handling of headers (which on review isn't great tbh).

@magnetised
Copy link
Contributor

Thanks for the PR and the issue. Closing in favour of #107

@magnetised magnetised closed this Sep 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request headers are not properly forwarded

2 participants