Skip to content

Commit 5afa24e

Browse files
authored
Fix Vary header (#138)
1 parent 56044fd commit 5afa24e

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

lib/inertia_rails/renderer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ def initialize(component, controller, request, response, render_method, props: n
2525
end
2626

2727
def render
28-
@response.set_header('Vary', [@request.headers['Vary'], 'X-Inertia'].compact.join(', '))
28+
if @response.headers["Vary"].blank?
29+
@response.headers["Vary"] = 'X-Inertia'
30+
else
31+
@response.headers["Vary"] = "#{@response.headers["Vary"]}, X-Inertia"
32+
end
2933
if @request.headers['X-Inertia']
3034
@response.set_header('X-Inertia', 'true')
3135
@render_method.call json: page, status: @response.status, content_type: Mime[:json]

spec/dummy/app/controllers/inertia_render_test_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ def component
1818
render inertia: 'TestComponent'
1919
end
2020

21+
def vary_header
22+
response.headers["Vary"] = 'Accept-Language'
23+
24+
render inertia: 'TestComponent'
25+
end
26+
2127
def lazy_props
2228
render inertia: 'TestComponent', props: {
2329
name: 'Brian',

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
get 'props' => 'inertia_render_test#props'
66
get 'view_data' => 'inertia_render_test#view_data'
77
get 'component' => 'inertia_render_test#component'
8+
get 'vary_header' => 'inertia_render_test#vary_header'
89
get 'share' => 'inertia_share_test#share'
910
get 'share_with_inherited' => 'inertia_child_share_test#share_with_inherited'
1011
get 'empty_test' => 'inertia_test#empty_test'

spec/inertia/rendering_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444

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

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

0 commit comments

Comments
 (0)