Skip to content

Commit d048f5d

Browse files
authored
Merge pull request #136 from inertiajs/always-return-vary-header
Always return vary header
2 parents 6e0fc5a + 0b3a5cc commit d048f5d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/inertia_rails/renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def initialize(component, controller, request, response, render_method, props: n
1616
end
1717

1818
def render
19+
@response.set_header('Vary', [@request.headers['Vary'], 'X-Inertia'].compact.join(', '))
1920
if @request.headers['X-Inertia']
20-
@response.set_header('Vary', 'X-Inertia')
2121
@response.set_header('X-Inertia', 'true')
2222
@render_method.call json: page, status: @response.status, content_type: Mime[:json]
2323
else

spec/inertia/rendering_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
context 'first load' do
77
let(:page) { InertiaRails::Renderer.new('TestComponent', controller, request, response, '').send(:page) }
8-
8+
99
context 'with props' do
1010
let(:page) { InertiaRails::Renderer.new('TestComponent', controller, request, response, '', props: {name: 'Brandon', sport: 'hockey'}).send(:page) }
1111
before { get props_path }
@@ -31,6 +31,28 @@
3131
expect(response.status).to eq 200
3232
end
3333

34+
describe 'headers' do
35+
context 'when no other Vary header is present' do
36+
it 'has the proper headers' do
37+
get component_path
38+
39+
expect(response.headers['X-Inertia']).to be_nil
40+
expect(response.headers['Vary']).to eq 'X-Inertia'
41+
expect(response.headers['Content-Type']).to eq 'text/html; charset=utf-8'
42+
end
43+
end
44+
45+
context 'when another Vary header is present' do
46+
it 'has the proper headers' do
47+
get component_path, headers: {'Vary' => 'Accept'}
48+
49+
expect(response.headers['X-Inertia']).to be_nil
50+
expect(response.headers['Vary']).to eq 'Accept, X-Inertia'
51+
expect(response.headers['Content-Type']).to eq 'text/html; charset=utf-8'
52+
end
53+
end
54+
end
55+
3456
context 'via an inertia route' do
3557
before { get inertia_route_path }
3658

0 commit comments

Comments
 (0)