Skip to content

Commit 75a7c96

Browse files
authored
Use to_json when rendering json response
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).
1 parent 8c749e3 commit 75a7c96

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/inertia_rails/renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def render
4141
end
4242
if @request.headers['X-Inertia']
4343
@response.set_header('X-Inertia', 'true')
44-
@render_method.call json: page, status: @response.status, content_type: Mime[:json]
44+
@render_method.call json: page.to_json, status: @response.status, content_type: Mime[:json]
4545
else
4646
return render_ssr if configuration.ssr_enabled rescue nil
4747
@render_method.call template: 'inertia', layout: layout, locals: view_data.merge(page: page)

0 commit comments

Comments
 (0)