diff --git a/lib/inertia_rails/renderer.rb b/lib/inertia_rails/renderer.rb index df88c79..5d717c1 100644 --- a/lib/inertia_rails/renderer.rb +++ b/lib/inertia_rails/renderer.rb @@ -16,8 +16,8 @@ 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 @request.headers['X-Inertia'] - @response.set_header('Vary', 'X-Inertia') @response.set_header('X-Inertia', 'true') @render_method.call json: page, status: @response.status, content_type: Mime[:json] else diff --git a/spec/inertia/rendering_spec.rb b/spec/inertia/rendering_spec.rb index 9ae756c..a161601 100644 --- a/spec/inertia/rendering_spec.rb +++ b/spec/inertia/rendering_spec.rb @@ -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 } @@ -31,6 +31,28 @@ expect(response.status).to eq 200 end + 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' + 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 before { get inertia_route_path }