Skip to content

Commit

Permalink
Fix Vary header (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov authored Oct 25, 2024
1 parent 56044fd commit 5afa24e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/inertia_rails/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def initialize(component, controller, request, response, render_method, props: n
end

def render
@response.set_header('Vary', [@request.headers['Vary'], 'X-Inertia'].compact.join(', '))
if @response.headers["Vary"].blank?
@response.headers["Vary"] = 'X-Inertia'
else
@response.headers["Vary"] = "#{@response.headers["Vary"]}, X-Inertia"
end
if @request.headers['X-Inertia']
@response.set_header('X-Inertia', 'true')
@render_method.call json: page, status: @response.status, content_type: Mime[:json]
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/controllers/inertia_render_test_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def component
render inertia: 'TestComponent'
end

def vary_header
response.headers["Vary"] = 'Accept-Language'

render inertia: 'TestComponent'
end

def lazy_props
render inertia: 'TestComponent', props: {
name: 'Brian',
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
get 'props' => 'inertia_render_test#props'
get 'view_data' => 'inertia_render_test#view_data'
get 'component' => 'inertia_render_test#component'
get 'vary_header' => 'inertia_render_test#vary_header'
get 'share' => 'inertia_share_test#share'
get 'share_with_inherited' => 'inertia_child_share_test#share_with_inherited'
get 'empty_test' => 'inertia_test#empty_test'
Expand Down
4 changes: 2 additions & 2 deletions spec/inertia/rendering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@

context 'when another Vary header is present' do
it 'has the proper headers' do
get component_path, headers: {'Vary' => 'Accept'}
get vary_header_path

expect(response.headers['X-Inertia']).to be_nil
expect(response.headers['Vary']).to eq 'Accept, X-Inertia'
expect(response.headers['Vary']).to eq 'Accept-Language, X-Inertia'
expect(response.headers['Content-Type']).to eq 'text/html; charset=utf-8'
end
end
Expand Down

0 comments on commit 5afa24e

Please sign in to comment.