From 5afa24ef6b8c81ff35d1f3f511643f49fcd019ff Mon Sep 17 00:00:00 2001 From: Svyatoslav Kryukov Date: Sat, 26 Oct 2024 00:30:14 +0300 Subject: [PATCH] Fix Vary header (#138) --- lib/inertia_rails/renderer.rb | 6 +++++- .../dummy/app/controllers/inertia_render_test_controller.rb | 6 ++++++ spec/dummy/config/routes.rb | 1 + spec/inertia/rendering_spec.rb | 4 ++-- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/inertia_rails/renderer.rb b/lib/inertia_rails/renderer.rb index f82fd23..cfa3a9d 100644 --- a/lib/inertia_rails/renderer.rb +++ b/lib/inertia_rails/renderer.rb @@ -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] diff --git a/spec/dummy/app/controllers/inertia_render_test_controller.rb b/spec/dummy/app/controllers/inertia_render_test_controller.rb index 0ac3784..52d2b93 100644 --- a/spec/dummy/app/controllers/inertia_render_test_controller.rb +++ b/spec/dummy/app/controllers/inertia_render_test_controller.rb @@ -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', diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 8faf2f9..9ba617c 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -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' diff --git a/spec/inertia/rendering_spec.rb b/spec/inertia/rendering_spec.rb index 0795dd5..6f15443 100644 --- a/spec/inertia/rendering_spec.rb +++ b/spec/inertia/rendering_spec.rb @@ -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