inertia_rails 1.11.0 → 1.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7912624c7ea6f1c71a896b969b704d8ff329ee41a3d1bc7277c28900b35a6516
4
- data.tar.gz: 30098f0f5dd3cf41987cd40f139a12259d5501375b85685ab0d427c0e69d230e
3
+ metadata.gz: 168a95bf6f07afa800c4886e447a035e765a5d7247b0ba2341c9c9b700a46ef9
4
+ data.tar.gz: ff006c26b44dff8e5f6f2d0e0477a2af6322958d1ea55dd65da7bb19e79be14a
5
5
  SHA512:
6
- metadata.gz: a97f62fa00d4489ff8364e6b4f9914161a1394905e5830526ba0baa9ae6888ee933a4e7739e18aeb6969f470989fb4aba9031cb62f57b68ed89065ff433d4e54
7
- data.tar.gz: e9d7b942b9e1b7f4cdb98e9941143b0e08501586266aac08390a510126233ffeca8328e75e9b0f63aea49d1d19820d02b97e65af0fb178bb822c8bf63ad9925c
6
+ metadata.gz: 9d1afe94e402b67a10836b1508b55c14c18891dc132dd4cc9d548d759f5097855e685e76c733709bafb0eb42a88142fae9f282074122f820d5b44e039b952d88
7
+ data.tar.gz: 9c98cb05d7b30919073788cc7ee81ebd8d72b623a24e67d9c3c3e5d0d3cab71ec870e9c457fc1ce30508c72869b1182bd749cb4fa54d21470049b706cefd1ed0
data/CHANGELOG.md CHANGED
@@ -4,6 +4,18 @@ 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
+ ## [1.12.1] - 2022-05-09
8
+
9
+ * Allow inertia to take over after initial pageload when using ssr. Thanks @99monkey!
10
+
11
+ ## [1.12.0] - 2022-05-04
12
+
13
+ * SSR!
14
+
15
+ ## [1.11.1] - 2021-06-27
16
+
17
+ * Fixed thread safety in the middleware. Thanks @caifara!
18
+
7
19
  ## [1.11.0] - 2021-03-23
8
20
 
9
21
  * Fixed the install generator. `installable?` was always returning false, preventing it from actually running.
data/README.md CHANGED
@@ -1,14 +1,10 @@
1
- # Inertia.js Rails Adapter
2
-
3
- Visit [inertiajs.com](https://inertiajs.com/) to learn more.
1
+ ![image](https://user-images.githubusercontent.com/6599653/114456558-032e2200-9bab-11eb-88bc-a19897f417ba.png)
4
2
 
5
- # Note to pre Rubygems release users
6
3
 
7
- The initial version of the gem was named `inertia`; however, that name was not available on Rubygems.
4
+ # Inertia.js Rails Adapter
8
5
 
9
- The 1.0.0 version release on Rubygems is `inertia_rails`.
6
+ Visit [inertiajs.com](https://inertiajs.com/) for installation instructions, documentation, and to learn more.
10
7
 
11
- The changes required are:
8
+ *Maintained and sponsored by the team at [bellaWatt](https://bellawatt.com/)*
12
9
 
13
- 1. Use `gem 'inertia_rails'` in your Gemfile (or `gem install inertia_rails`)
14
- 2. Change any `Inertia.configure` calls to `InertiaRails.configure`
10
+ [![bellaWatt Logo](https://user-images.githubusercontent.com/6599653/114456832-5607d980-9bab-11eb-99c8-ab39867c384e.png)](https://bellawatt.com/)
@@ -0,0 +1,11 @@
1
+ <script>
2
+ export let name;
3
+ </script>
4
+
5
+ <style>
6
+ h1 { font-family: sans-serif; text-align: center; }
7
+ </style>
8
+
9
+ <h1>
10
+ Hello {name}!
11
+ </h1>
@@ -0,0 +1,19 @@
1
+ import axios from 'axios'
2
+
3
+ import { createInertiaApp } from '@inertiajs/inertia-svelte'
4
+ import { InertiaProgress } from '@inertiajs/progress'
5
+
6
+ document.addEventListener('DOMContentLoaded', () => {
7
+ const csrfToken = document.querySelector('meta[name=csrf-token]').content
8
+ axios.defaults.headers.common['X-CSRF-Token'] = csrfToken
9
+
10
+ InertiaProgress.init()
11
+
12
+ createInertiaApp({
13
+ id: 'app',
14
+ resolve: name => import(`../Pages/${name}.svelte`),
15
+ setup({ el, App, props }) {
16
+ new App({ target: el, props })
17
+ },
18
+ })
19
+ })
@@ -6,6 +6,7 @@ module InertiaRails
6
6
  FRONT_END_INSTALLERS = [
7
7
  'react',
8
8
  'vue',
9
+ 'svelte',
9
10
  ]
10
11
 
11
12
  def install
@@ -70,5 +71,14 @@ module InertiaRails
70
71
  template "vue/inertia.js", Rails.root.join("app/javascript/packs/inertia.js").to_s
71
72
  say "done!", :green
72
73
  end
74
+
75
+ def install_svelte!
76
+ say "Creating a Svelte page component...", :blue
77
+ run 'yarn add @inertiajs/inertia-svelte'
78
+ template "svelte/InertiaExample.svelte", Rails.root.join("app/javascript/Pages/InertiaExample.svelte").to_s
79
+ say "Copying inertia.js into webpacker's packs folder...", :blue
80
+ template "svelte/inertia.js", Rails.root.join("app/javascript/packs/inertia.js").to_s
81
+ say "done!", :green
82
+ end
73
83
  end
74
84
  end
@@ -1,4 +1,5 @@
1
1
  require_relative "inertia_rails"
2
+ require_relative "helper"
2
3
 
3
4
  module InertiaRails
4
5
  module Controller
@@ -9,6 +10,7 @@ module InertiaRails
9
10
  # :inertia_errors are deleted from the session by the middleware
10
11
  InertiaRails.share(errors: session[:inertia_errors]) if session[:inertia_errors].present?
11
12
  end
13
+ helper ::InertiaRails::Helper
12
14
  end
13
15
 
14
16
  module ClassMethods
@@ -0,0 +1,7 @@
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
@@ -5,6 +5,7 @@ require 'inertia_rails/lazy'
5
5
  module InertiaRails
6
6
  thread_mattr_accessor :threadsafe_shared_plain_data
7
7
  thread_mattr_accessor :threadsafe_shared_blocks
8
+ thread_mattr_accessor :threadsafe_html_headers
8
9
 
9
10
  def self.configure
10
11
  yield(Configuration)
@@ -23,6 +24,18 @@ module InertiaRails
23
24
  Configuration.layout
24
25
  end
25
26
 
27
+ def self.ssr_enabled?
28
+ Configuration.ssr_enabled
29
+ end
30
+
31
+ def self.ssr_url
32
+ Configuration.ssr_url
33
+ end
34
+
35
+ def self.html_headers
36
+ self.threadsafe_html_headers || []
37
+ end
38
+
26
39
  # "Setters"
27
40
  def self.share(**args)
28
41
  self.shared_plain_data = self.shared_plain_data.merge(args)
@@ -32,9 +45,14 @@ module InertiaRails
32
45
  self.shared_blocks = self.shared_blocks + [block]
33
46
  end
34
47
 
48
+ def self.html_headers=(headers)
49
+ self.threadsafe_html_headers = headers
50
+ end
51
+
35
52
  def self.reset!
36
53
  self.shared_plain_data = {}
37
54
  self.shared_blocks = []
55
+ self.html_headers = []
38
56
  end
39
57
 
40
58
  def self.lazy(value = nil, &block)
@@ -46,6 +64,8 @@ module InertiaRails
46
64
  module Configuration
47
65
  mattr_accessor(:layout) { 'application' }
48
66
  mattr_accessor(:version) { nil }
67
+ mattr_accessor(:ssr_enabled) { false }
68
+ mattr_accessor(:ssr_url) { 'http://localhost:13714' }
49
69
 
50
70
  def self.evaluated_version
51
71
  self.version.respond_to?(:call) ? self.version.call : self.version
@@ -5,79 +5,91 @@ module InertiaRails
5
5
  end
6
6
 
7
7
  def call(env)
8
- @env = env
8
+ InertiaRailsRequest.new(@app, env)
9
+ .response
10
+ end
9
11
 
10
- status, headers, body = @app.call(env)
11
- request = ActionDispatch::Request.new(env)
12
+ class InertiaRailsRequest
13
+ def initialize(app, env)
14
+ @app = app
15
+ @env = env
16
+ end
12
17
 
13
- ::InertiaRails.reset!
18
+ def response
19
+ status, headers, body = @app.call(@env)
20
+ request = ActionDispatch::Request.new(@env)
14
21
 
15
- # Inertia errors are added to the session via redirect_to
16
- request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
22
+ ::InertiaRails.reset!
17
23
 
18
- status = 303 if inertia_non_post_redirect?(status)
24
+ # Inertia errors are added to the session via redirect_to
25
+ request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
19
26
 
20
- return stale_inertia_get? ? force_refresh(request) : [status, headers, body]
21
- end
27
+ status = 303 if inertia_non_post_redirect?(status)
22
28
 
23
- private
29
+ stale_inertia_get? ? force_refresh(request) : [status, headers, body]
30
+ end
24
31
 
25
- def keep_inertia_errors?(status)
26
- redirect_status?(status) || stale_inertia_request?
27
- end
32
+ private
28
33
 
29
- def stale_inertia_request?
30
- inertia_request? && version_stale?
31
- end
34
+ def keep_inertia_errors?(status)
35
+ redirect_status?(status) || stale_inertia_request?
36
+ end
32
37
 
33
- def redirect_status?(status)
34
- [301, 302].include? status
35
- end
38
+ def stale_inertia_request?
39
+ inertia_request? && version_stale?
40
+ end
36
41
 
37
- def non_get_redirectable_method?
38
- ['PUT', 'PATCH', 'DELETE'].include? request_method
39
- end
42
+ def redirect_status?(status)
43
+ [301, 302].include? status
44
+ end
40
45
 
41
- def inertia_non_post_redirect?(status)
42
- inertia_request? && redirect_status?(status) && non_get_redirectable_method?
43
- end
46
+ def non_get_redirectable_method?
47
+ ['PUT', 'PATCH', 'DELETE'].include? request_method
48
+ end
44
49
 
45
- def stale_inertia_get?
46
- get? && stale_inertia_request?
47
- end
50
+ def inertia_non_post_redirect?(status)
51
+ inertia_request? && redirect_status?(status) && non_get_redirectable_method?
52
+ end
48
53
 
49
- def get?
50
- request_method == 'GET'
51
- end
54
+ def stale_inertia_get?
55
+ get? && stale_inertia_request?
56
+ end
52
57
 
53
- def request_method
54
- @env['REQUEST_METHOD']
55
- end
58
+ def get?
59
+ request_method == 'GET'
60
+ end
56
61
 
57
- def inertia_version
58
- @env['HTTP_X_INERTIA_VERSION']
59
- end
62
+ def request_method
63
+ @env['REQUEST_METHOD']
64
+ end
60
65
 
61
- def inertia_request?
62
- @env['HTTP_X_INERTIA'].present?
63
- end
66
+ def inertia_version
67
+ @env['HTTP_X_INERTIA_VERSION']
68
+ end
64
69
 
65
- def version_stale?
66
- sent_version != saved_version
67
- end
70
+ def inertia_request?
71
+ @env['HTTP_X_INERTIA'].present?
72
+ end
68
73
 
69
- def sent_version
70
- return nil if inertia_version.nil?
71
- InertiaRails.version.is_a?(Numeric) ? inertia_version.to_f : inertia_version
72
- end
74
+ def version_stale?
75
+ sent_version != saved_version
76
+ end
73
77
 
74
- def saved_version
75
- InertiaRails.version.is_a?(Numeric) ? InertiaRails.version.to_f : InertiaRails.version
76
- end
78
+ def sent_version
79
+ return nil if inertia_version.nil?
80
+
81
+ InertiaRails.version.is_a?(Numeric) ? inertia_version.to_f : inertia_version
82
+ end
77
83
 
78
- def force_refresh(request)
79
- request.flash.keep
80
- Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url}).finish
84
+ def saved_version
85
+ InertiaRails.version.is_a?(Numeric) ? InertiaRails.version.to_f : InertiaRails.version
86
+ end
87
+
88
+ def force_refresh(request)
89
+ request.flash.keep
90
+ Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url}).finish
91
+ end
81
92
  end
82
93
  end
83
94
  end
95
+
@@ -1,3 +1,5 @@
1
+ require 'net/http'
2
+ require 'json'
1
3
  require_relative "inertia_rails"
2
4
 
3
5
  module InertiaRails
@@ -20,12 +22,21 @@ module InertiaRails
20
22
  @response.set_header('X-Inertia', 'true')
21
23
  @render_method.call json: page, status: @response.status, content_type: Mime[:json]
22
24
  else
25
+ return render_ssr if ::InertiaRails.ssr_enabled? rescue nil
23
26
  @render_method.call template: 'inertia', layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
24
27
  end
25
28
  end
26
29
 
27
30
  private
28
31
 
32
+ def render_ssr
33
+ uri = URI("#{::InertiaRails.ssr_url}/render")
34
+ res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
35
+
36
+ ::InertiaRails.html_headers = res['head']
37
+ @render_method.call html: res['body'].html_safe, layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
38
+ end
39
+
29
40
  def props
30
41
  _props = ::InertiaRails.shared_data(@controller).merge(@props).select do |key, prop|
31
42
  if rendering_partial_component?
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "1.11.0"
2
+ VERSION = "1.12.1"
3
3
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inertia_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.0
4
+ version: 1.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Knoles
8
8
  - Brandon Shar
9
9
  - Eugene Granovsky
10
- autorequire:
10
+ autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2021-03-23 00:00:00.000000000 Z
13
+ date: 2022-05-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -124,7 +124,7 @@ dependencies:
124
124
  - - ">="
125
125
  - !ruby/object:Gem::Version
126
126
  version: '0'
127
- description:
127
+ description:
128
128
  email:
129
129
  - brain@bellawatt.com
130
130
  - brandon@bellawatt.com
@@ -156,12 +156,15 @@ files:
156
156
  - lib/generators/inertia_rails/install/controller.rb
157
157
  - lib/generators/inertia_rails/install/react/InertiaExample.jsx
158
158
  - lib/generators/inertia_rails/install/react/inertia.jsx
159
+ - lib/generators/inertia_rails/install/svelte/InertiaExample.svelte
160
+ - lib/generators/inertia_rails/install/svelte/inertia.js
159
161
  - lib/generators/inertia_rails/install/vue/InertiaExample.vue
160
162
  - lib/generators/inertia_rails/install/vue/inertia.js
161
163
  - lib/generators/inertia_rails/install_generator.rb
162
164
  - lib/inertia_rails.rb
163
165
  - lib/inertia_rails/controller.rb
164
166
  - lib/inertia_rails/engine.rb
167
+ - lib/inertia_rails/helper.rb
165
168
  - lib/inertia_rails/inertia_rails.rb
166
169
  - lib/inertia_rails/lazy.rb
167
170
  - lib/inertia_rails/middleware.rb
@@ -182,7 +185,7 @@ metadata:
182
185
  homepage_uri: https://github.com/inertiajs/inertia-rails
183
186
  source_code_uri: https://github.com/inertiajs/inertia-rails
184
187
  changelog_uri: https://github.com/inertiajs/inertia-rails/CHANGELOG.md
185
- post_install_message:
188
+ post_install_message:
186
189
  rdoc_options: []
187
190
  require_paths:
188
191
  - lib
@@ -197,8 +200,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
200
  - !ruby/object:Gem::Version
198
201
  version: '0'
199
202
  requirements: []
200
- rubygems_version: 3.1.4
201
- signing_key:
203
+ rubygems_version: 3.3.3
204
+ signing_key:
202
205
  specification_version: 4
203
206
  summary: Inertia adapter for Rails
204
207
  test_files: []