Skip to content

Commit

Permalink
Append 'X-Inertia' to the Vary header instead of overwriting it
Browse files Browse the repository at this point in the history
  • Loading branch information
bknoles committed Oct 15, 2024
1 parent 9dca481 commit 0b3a5cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/inertia_rails/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(component, controller, request, response, render_method, props: n
end

def render
@response.set_header('Vary', 'X-Inertia')
@response.set_header('Vary', [@request.headers['Vary'], 'X-Inertia'].compact.join(', '))
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
26 changes: 20 additions & 6 deletions spec/inertia/rendering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

context 'first load' do
let(:page) { InertiaRails::Renderer.new('TestComponent', controller, request, response, '').send(:page) }

context 'with props' do
let(:page) { InertiaRails::Renderer.new('TestComponent', controller, request, response, '', props: {name: 'Brandon', sport: 'hockey'}).send(:page) }
before { get props_path }
Expand All @@ -31,12 +31,26 @@
expect(response.status).to eq 200
end

it 'has the proper headers' do
get component_path
describe 'headers' do
context 'when no other Vary header is present' do
it 'has the proper headers' do
get component_path

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

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

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

context 'via an inertia route' do
Expand Down

0 comments on commit 0b3a5cc

Please sign in to comment.