inertia_rails 3.1.4 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b3768cf7f6b1a5551c999666848f0df4357d13ccb7a961025c390aac4c67756
4
- data.tar.gz: 4f01d96c927dc9f81bcd519954681d9192ec9606fa3884d26e4860c8ea3999cd
3
+ metadata.gz: 76a7152d32edba5a140ed16b25efe25a6766a7d53c955f39f6b5efb2b5fc0d12
4
+ data.tar.gz: c1dc0d2bf09b2f3c71e4729a26dc6c24c78dc0fe4c5b8d09484bacee18543953
5
5
  SHA512:
6
- metadata.gz: 74b32a38b4e9f4bd10f6619467b60dd0b176226f5b83f5ed924ed095523bdbb61cdd6eb2679320f44b087fc286c32e594c49ffb9cf48506236127ea887e0db93
7
- data.tar.gz: c92169d183122b11dac0d4ea6b66b22072d0c586080f424d172561c18db2c3d1a6f2939c4a22cbadab33a23346d9c4c246de67b663929ba1bd8df76177572796
6
+ metadata.gz: 70ff7787dd839a58c652ffe5fa402be2baa2036ea54b86b2b033ecaf2062304a4f5e8433316eea192ab25009d6c5e8b1cf45dfeba71dc6f8775b361cb3055f50
7
+ data.tar.gz: 9e9072c293cd62385df9e8be3f464c7b688acc665ad4ce40daea5fd29c0414c18ba5c9f72967edfc3c7963974867d2791486e78714cea98ac212c64f38144ccb
data/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [3.2.0] - 2024-06-19
8
+
9
+ * Refactor the internals of shared Inertia data to use controller instance variables instead of module level variables that run a higher risk of being leaked between requests. Big thanks to @ledermann for the initial work many years ago and to @PedroAugustoRamalhoDuarte for finishing it up!
10
+ * Change the Inertia response to set the `Vary` header to `X-Inertia` instead of `Accept`, Thanks @osbre!
11
+ * Always set the `XSRF-TOKEN` in an `after_action` request instead of only on non-Inertia requests. This fixes a bug where logging out (and resetting the session) via Inertia would create a CSRF token mismatch on a subsequent Inertia request (until you manually hard refreshed the page). Thanks @jordanhiltunen!
12
+
7
13
  ## [3.1.4] - 2024-04-28
8
14
 
9
15
  * Reset Inertia shared data after each RSpec example where `inertia: true` is used. Thanks @coreyaus!
@@ -1,27 +1,37 @@
1
1
  require_relative "inertia_rails"
2
- require_relative "helper"
3
2
 
4
3
  module InertiaRails
5
4
  module Controller
6
5
  extend ActiveSupport::Concern
7
6
 
8
7
  included do
8
+ helper_method :inertia_headers
9
+
9
10
  before_action do
10
- # :inertia_errors are deleted from the session by the middleware
11
- InertiaRails.share(errors: session[:inertia_errors]) if session[:inertia_errors].present?
11
+ error_sharing = proc do
12
+ # :inertia_errors are deleted from the session by the middleware
13
+ if @_request && session[:inertia_errors].present?
14
+ { errors: session[:inertia_errors] }
15
+ else
16
+ {}
17
+ end
18
+ end
19
+
20
+ @_inertia_shared_plain_data ||= {}
21
+ @_inertia_shared_blocks ||= [error_sharing]
22
+ @_inertia_html_headers ||= []
12
23
  end
13
- helper ::InertiaRails::Helper
14
24
 
15
25
  after_action do
16
- cookies['XSRF-TOKEN'] = form_authenticity_token unless request.inertia? || !protect_against_forgery?
26
+ cookies['XSRF-TOKEN'] = form_authenticity_token unless !protect_against_forgery?
17
27
  end
18
28
  end
19
29
 
20
30
  module ClassMethods
21
- def inertia_share(**args, &block)
31
+ def inertia_share(hash = nil, &block)
22
32
  before_action do
23
- InertiaRails.share(**args) if args
24
- InertiaRails.share_block(block) if block
33
+ @_inertia_shared_plain_data = @_inertia_shared_plain_data.merge(hash) if hash
34
+ @_inertia_shared_blocks = @_inertia_shared_blocks + [block] if block_given?
25
35
  end
26
36
  end
27
37
 
@@ -33,6 +43,14 @@ module InertiaRails
33
43
  end
34
44
  end
35
45
 
46
+ def inertia_headers
47
+ @_inertia_html_headers.join.html_safe
48
+ end
49
+
50
+ def inertia_headers=(value)
51
+ @_inertia_html_headers = value
52
+ end
53
+
36
54
  def default_render
37
55
  if InertiaRails.default_render?
38
56
  render(inertia: true)
@@ -41,6 +59,10 @@ module InertiaRails
41
59
  end
42
60
  end
43
61
 
62
+ def shared_data
63
+ (@_inertia_shared_plain_data || {}).merge(evaluated_blocks)
64
+ end
65
+
44
66
  def redirect_to(options = {}, response_options = {})
45
67
  capture_inertia_errors(response_options)
46
68
  super(options, response_options)
@@ -80,5 +102,9 @@ module InertiaRails
80
102
  session[:inertia_errors] = inertia_errors
81
103
  end
82
104
  end
105
+
106
+ def evaluated_blocks
107
+ (@_inertia_shared_blocks || []).map { |block| instance_exec(&block) }.reduce(&:merge) || {}
108
+ end
83
109
  end
84
110
  end
@@ -3,20 +3,10 @@ require 'active_support/core_ext/module/attribute_accessors_per_thread'
3
3
  require 'inertia_rails/lazy'
4
4
 
5
5
  module InertiaRails
6
- thread_mattr_accessor :threadsafe_shared_plain_data
7
- thread_mattr_accessor :threadsafe_shared_blocks
8
- thread_mattr_accessor :threadsafe_html_headers
9
-
10
6
  def self.configure
11
7
  yield(Configuration)
12
8
  end
13
9
 
14
- # "Getters"
15
- def self.shared_data(controller)
16
- shared_plain_data.
17
- merge!(evaluated_blocks(controller, shared_blocks))
18
- end
19
-
20
10
  def self.version
21
11
  Configuration.evaluated_version
22
12
  end
@@ -35,35 +25,12 @@ module InertiaRails
35
25
 
36
26
  def self.default_render?
37
27
  Configuration.default_render
38
- end
39
-
40
- def self.html_headers
41
- self.threadsafe_html_headers || []
42
28
  end
43
29
 
44
30
  def self.deep_merge_shared_data?
45
31
  Configuration.deep_merge_shared_data
46
32
  end
47
33
 
48
- # "Setters"
49
- def self.share(**args)
50
- self.shared_plain_data = self.shared_plain_data.merge(args)
51
- end
52
-
53
- def self.share_block(block)
54
- self.shared_blocks = self.shared_blocks + [block]
55
- end
56
-
57
- def self.html_headers=(headers)
58
- self.threadsafe_html_headers = headers
59
- end
60
-
61
- def self.reset!
62
- self.shared_plain_data = {}
63
- self.shared_blocks = []
64
- self.html_headers = []
65
- end
66
-
67
34
  def self.lazy(value = nil, &block)
68
35
  InertiaRails::Lazy.new(value, &block)
69
36
  end
@@ -82,25 +49,4 @@ module InertiaRails
82
49
  self.version.respond_to?(:call) ? self.version.call : self.version
83
50
  end
84
51
  end
85
-
86
- # Getters and setters to provide default values for the threadsafe attributes
87
- def self.shared_plain_data
88
- self.threadsafe_shared_plain_data || {}
89
- end
90
-
91
- def self.shared_plain_data=(val)
92
- self.threadsafe_shared_plain_data = val
93
- end
94
-
95
- def self.shared_blocks
96
- self.threadsafe_shared_blocks || []
97
- end
98
-
99
- def self.shared_blocks=(val)
100
- self.threadsafe_shared_blocks = val
101
- end
102
-
103
- def self.evaluated_blocks(controller, blocks)
104
- blocks.flat_map { |block| controller.instance_exec(&block) }.reduce(&:merge) || {}
105
- end
106
52
  end
@@ -7,8 +7,6 @@ module InertiaRails
7
7
  def call(env)
8
8
  InertiaRailsRequest.new(@app, env)
9
9
  .response
10
- ensure
11
- ::InertiaRails.reset!
12
10
  end
13
11
 
14
12
  class InertiaRailsRequest
@@ -22,7 +20,7 @@ module InertiaRails
22
20
  status, headers, body = @app.call(@env)
23
21
  request = ActionDispatch::Request.new(@env)
24
22
 
25
- # Inertia errors are added to the session via redirect_to
23
+ # Inertia errors are added to the session via redirect_to
26
24
  request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
27
25
 
28
26
  status = 303 if inertia_non_post_redirect?(status)
@@ -97,4 +95,3 @@ module InertiaRails
97
95
  end
98
96
  end
99
97
  end
100
-
@@ -1,5 +1,3 @@
1
- require 'net/http'
2
- require 'json'
3
1
  require_relative "inertia_rails"
4
2
 
5
3
  module InertiaRails
@@ -19,7 +17,7 @@ module InertiaRails
19
17
 
20
18
  def render
21
19
  if @request.headers['X-Inertia']
22
- @response.set_header('Vary', 'Accept')
20
+ @response.set_header('Vary', 'X-Inertia')
23
21
  @response.set_header('X-Inertia', 'true')
24
22
  @render_method.call json: page, status: @response.status, content_type: Mime[:json]
25
23
  else
@@ -33,8 +31,8 @@ module InertiaRails
33
31
  def render_ssr
34
32
  uri = URI("#{::InertiaRails.ssr_url}/render")
35
33
  res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
36
-
37
- ::InertiaRails.html_headers = res['head']
34
+
35
+ @controller.inertia_headers = res['head']
38
36
  @render_method.call html: res['body'].html_safe, layout: layout, locals: (view_data).merge({page: page})
39
37
  end
40
38
 
@@ -48,7 +46,7 @@ module InertiaRails
48
46
  #
49
47
  # Functionally, this permits using either string or symbol keys in the controller. Since the results
50
48
  # is cast to json, we should treat string/symbol keys as identical.
51
- _props = ::InertiaRails.shared_data(@controller).deep_symbolize_keys.send(prop_merge_method, @props.deep_symbolize_keys).select do |key, prop|
49
+ _props = @controller.shared_data.merge.deep_symbolize_keys.send(prop_merge_method, @props.deep_symbolize_keys).select do |key, prop|
52
50
  if rendering_partial_component?
53
51
  key.in? partial_keys
54
52
  else
@@ -65,7 +65,6 @@ RSpec.configure do |config|
65
65
  }
66
66
 
67
67
  config.before(:each, inertia: true) do
68
- ::InertiaRails.reset!
69
68
  new_renderer = InertiaRails::Renderer.method(:new)
70
69
  allow(InertiaRails::Renderer).to receive(:new) do |component, controller, request, response, render, named_args|
71
70
  new_renderer.call(component, controller, request, response, inertia_wrap_render(render), **named_args)
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "3.1.4"
2
+ VERSION = "3.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inertia_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.4
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Knoles
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2024-04-28 00:00:00.000000000 Z
13
+ date: 2024-06-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties
@@ -158,7 +158,6 @@ files:
158
158
  - lib/inertia_rails.rb
159
159
  - lib/inertia_rails/controller.rb
160
160
  - lib/inertia_rails/engine.rb
161
- - lib/inertia_rails/helper.rb
162
161
  - lib/inertia_rails/inertia_rails.rb
163
162
  - lib/inertia_rails/lazy.rb
164
163
  - lib/inertia_rails/middleware.rb
@@ -194,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
193
  - !ruby/object:Gem::Version
195
194
  version: '0'
196
195
  requirements: []
197
- rubygems_version: 3.5.3
196
+ rubygems_version: 3.5.10
198
197
  signing_key:
199
198
  specification_version: 4
200
199
  summary: Inertia adapter for Rails
@@ -1,7 +0,0 @@
1
- require_relative 'inertia_rails'
2
-
3
- module InertiaRails::Helper
4
- def inertia_headers
5
- ::InertiaRails.html_headers.join.html_safe
6
- end
7
- end