-
viralpraxis authored
Currently, calling `head` to set the response code silently removes a previously set response body [0]. This can lead to unnecessary and potentially expensive rendering, as shown in the following snippet: ```ruby def index render json: User.all.as_json # expensive rendering head :ok # Overwrites `response_body` to `` end ``` This behavior is inconsistent with calling `render` twice or calling `redirect_to` after `render`: both of which raise a `DoubleRenderError` [1, 2]. Since this change could break existing endpoints when upgrading Rails, it might make sense to introduce a configuration option would allow developers to choose whether this scenario raises an exception or a warning. [0] https://github.com/rails/rails/blob/b721c62de11c2c29fa2b8ef51b00d95dce163afc/actionpack/lib/action_controller/metal/head.rb#L50 [1] https://github.com/rails/rails/blob/b721c62de11c2c29fa2b8ef51b00d95dce163afc/actionpack/lib/action_controller/metal/redirecting.rb#L103-L105 [2] https://github.com/rails/rails/blob/b721c62de11c2c29fa2b8ef51b00d95dce163afc/actionpack/lib/action_controller/metal/rendering.rb#L171-L174
viralpraxis authoredCurrently, calling `head` to set the response code silently removes a previously set response body [0]. This can lead to unnecessary and potentially expensive rendering, as shown in the following snippet: ```ruby def index render json: User.all.as_json # expensive rendering head :ok # Overwrites `response_body` to `` end ``` This behavior is inconsistent with calling `render` twice or calling `redirect_to` after `render`: both of which raise a `DoubleRenderError` [1, 2]. Since this change could break existing endpoints when upgrading Rails, it might make sense to introduce a configuration option would allow developers to choose whether this scenario raises an exception or a warning. [0] https://github.com/rails/rails/blob/b721c62de11c2c29fa2b8ef51b00d95dce163afc/actionpack/lib/action_controller/metal/head.rb#L50 [1] https://github.com/rails/rails/blob/b721c62de11c2c29fa2b8ef51b00d95dce163afc/actionpack/lib/action_controller/metal/redirecting.rb#L103-L105 [2] https://github.com/rails/rails/blob/b721c62de11c2c29fa2b8ef51b00d95dce163afc/actionpack/lib/action_controller/metal/rendering.rb#L171-L174
Loading