Skip to content

Commit

Permalink
Use to_json when rendering json response
Browse files Browse the repository at this point in the history
I was trying to debug why an inertia response was 5x slower than the initial page response.

After some investigation, it turns out that render json: foo does not actually do the same thing as foo.to_json does. It does a whole lot of rails magic instead, which is both a lot slower and inconsistent.

Changing this to use page.to_json ensures consistency with the props output in inertia.html.erb, avoiding weird inconsistencies between props renders and full page renders, and is also a lot faster (especially if you are using a library like Oj to optimise json generation in the first place).
  • Loading branch information
alexspeller authored Jan 7, 2025
1 parent 8c749e3 commit 75a7c96
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/inertia_rails/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def render
end
if @request.headers['X-Inertia']
@response.set_header('X-Inertia', 'true')
@render_method.call json: page, status: @response.status, content_type: Mime[:json]
@render_method.call json: page.to_json, status: @response.status, content_type: Mime[:json]
else
return render_ssr if configuration.ssr_enabled rescue nil
@render_method.call template: 'inertia', layout: layout, locals: view_data.merge(page: page)
Expand Down

0 comments on commit 75a7c96

Please sign in to comment.