inertia_rails 1.10.0 → 1.12.0

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: c76eba9ada41f6b781b030f81b43bb87169034594127e106ec4dbee472926e8e
4
- data.tar.gz: e9ed9115c9d6a678bdefdeeeafbeb1790c9a28f37521d71c699d5ca40989cd86
3
+ metadata.gz: 468c8c88d85da84a010be47c474e9d8d1ed4735d5d83cd8058038401e322334c
4
+ data.tar.gz: ce4ccdf41063f88495e8829dbefc4eb92936850d9f4da8a4f51e907eff9f90f4
5
5
  SHA512:
6
- metadata.gz: 5d0285ccf5284c9fb4d267d25dfc899d58be5f57b2166fc42251246398483aaf639c8dbec94181a1a5978c020ea2fb796872248dd3de7c14db165b8ab20755cb
7
- data.tar.gz: c06d57bec2e2c4fa55b15428055684999af1d65ee226f42263cb945d17ce4f06029bc8365b2a9f52b59db924b251f34944dd96998a7933b64fe18c6e80ae3117
6
+ metadata.gz: 6a097f6f871a2b7b294e5e497d051c8d677230f8a600e4a602a73b742e76bce34686b7b76d3287bb2d5af897a3254a6d368b03a672c0830779b7753f9284a0f0
7
+ data.tar.gz: caa62abc7dab0b7abe669b3735a4381e7ee1af3b705983b386059c4a0938a9df6a26f274f85169dbdf55a76bb4a06383e6930e22464035ef29fb6492af36bd2d
data/CHANGELOG.md CHANGED
@@ -4,6 +4,19 @@ 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.0] - 2022-05-04
8
+
9
+ * SSR!
10
+
11
+ ## [1.11.1] - 2021-06-27
12
+
13
+ * Fixed thread safety in the middleware. Thanks @caifara!
14
+
15
+ ## [1.11.0] - 2021-03-23
16
+
17
+ * Fixed the install generator. `installable?` was always returning false, preventing it from actually running.
18
+ * Added an install generator for Vue.
19
+
7
20
  ## [1.10.0] - 2021-03-22
8
21
 
9
22
  * Added install generator to quickly add Inertia to existing rails apps via `rails inertia_rails:install:react`
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
+ })
@@ -0,0 +1,11 @@
1
+ <template>
2
+ <h1 :style="{ 'text-align': 'center' }">Hello {{name}}!</h1>
3
+ </template>
4
+
5
+ <script>
6
+ export default {
7
+ props: {
8
+ name: { type: String, required: true },
9
+ },
10
+ }
11
+ </script>
@@ -0,0 +1,24 @@
1
+ import axios from 'axios'
2
+ import Vue from 'vue'
3
+
4
+ import { app, plugin } from '@inertiajs/inertia-vue'
5
+ import { InertiaProgress } from '@inertiajs/progress'
6
+
7
+ document.addEventListener('DOMContentLoaded', () => {
8
+ const csrfToken = document.querySelector('meta[name=csrf-token]').content
9
+ axios.defaults.headers.common['X-CSRF-Token'] = csrfToken
10
+
11
+ InertiaProgress.init();
12
+ const el = document.getElementById('app')
13
+
14
+ Vue.use(plugin)
15
+
16
+ new Vue({
17
+ render: h => h(app, {
18
+ props: {
19
+ initialPage: JSON.parse(el.dataset.page),
20
+ resolveComponent: name => require(`../Pages/${name}`).default,
21
+ },
22
+ }),
23
+ }).$mount(el)
24
+ })
@@ -5,6 +5,8 @@ module InertiaRails
5
5
 
6
6
  FRONT_END_INSTALLERS = [
7
7
  'react',
8
+ 'vue',
9
+ 'svelte',
8
10
  ]
9
11
 
10
12
  def install
@@ -32,6 +34,8 @@ module InertiaRails
32
34
 
33
35
  return false
34
36
  end
37
+
38
+ true
35
39
  end
36
40
 
37
41
  def install_base!
@@ -53,10 +57,28 @@ module InertiaRails
53
57
  def install_react!
54
58
  say "Creating a React page component...", :blue
55
59
  run 'yarn add @inertiajs/inertia-react'
56
- template "react.jsx", Rails.root.join("app/javascript/Pages/InertiaExample.js").to_s
60
+ template "react/InertiaExample.jsx", Rails.root.join("app/javascript/Pages/InertiaExample.js").to_s
57
61
  say "Copying inertia.jsx into webpacker's packs folder...", :blue
58
- template "inertia.jsx", Rails.root.join("app/javascript/packs/inertia.jsx").to_s
62
+ template "react/inertia.jsx", Rails.root.join("app/javascript/packs/inertia.jsx").to_s
63
+ say "done!", :green
64
+ end
65
+
66
+ def install_vue!
67
+ say "Creating a Vue page component...", :blue
68
+ run 'yarn add @inertiajs/inertia-vue'
69
+ template "vue/InertiaExample.vue", Rails.root.join("app/javascript/Pages/InertiaExample.vue").to_s
70
+ say "Copying inertia.js into webpacker's packs folder...", :blue
71
+ template "vue/inertia.js", Rails.root.join("app/javascript/packs/inertia.js").to_s
72
+ say "done!", :green
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
59
81
  say "done!", :green
60
82
  end
61
83
  end
62
- end
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
@@ -15,6 +17,8 @@ module InertiaRails
15
17
  end
16
18
 
17
19
  def render
20
+ return render_ssr if ::InertiaRails.ssr_enabled?
21
+
18
22
  if @request.headers['X-Inertia']
19
23
  @response.set_header('Vary', 'Accept')
20
24
  @response.set_header('X-Inertia', 'true')
@@ -26,6 +30,14 @@ module InertiaRails
26
30
 
27
31
  private
28
32
 
33
+ def render_ssr
34
+ uri = URI("#{::InertiaRails.ssr_url}/render")
35
+ res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
36
+
37
+ ::InertiaRails.html_headers = res['head']
38
+ @render_method.call html: res['body'].html_safe, layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
39
+ end
40
+
29
41
  def props
30
42
  _props = ::InertiaRails.shared_data(@controller).merge(@props).select do |key, prop|
31
43
  if rendering_partial_component?
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "1.10.0"
2
+ VERSION = "1.12.0"
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.10.0
4
+ version: 1.12.0
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-22 00:00:00.000000000 Z
13
+ date: 2022-05-04 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
@@ -154,12 +154,17 @@ files:
154
154
  - gemfiles/rails_6.1.gemfile
155
155
  - inertia_rails.gemspec
156
156
  - lib/generators/inertia_rails/install/controller.rb
157
- - lib/generators/inertia_rails/install/inertia.jsx
158
- - lib/generators/inertia_rails/install/react.jsx
157
+ - lib/generators/inertia_rails/install/react/InertiaExample.jsx
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
161
+ - lib/generators/inertia_rails/install/vue/InertiaExample.vue
162
+ - lib/generators/inertia_rails/install/vue/inertia.js
159
163
  - lib/generators/inertia_rails/install_generator.rb
160
164
  - lib/inertia_rails.rb
161
165
  - lib/inertia_rails/controller.rb
162
166
  - lib/inertia_rails/engine.rb
167
+ - lib/inertia_rails/helper.rb
163
168
  - lib/inertia_rails/inertia_rails.rb
164
169
  - lib/inertia_rails/lazy.rb
165
170
  - lib/inertia_rails/middleware.rb
@@ -180,7 +185,7 @@ metadata:
180
185
  homepage_uri: https://github.com/inertiajs/inertia-rails
181
186
  source_code_uri: https://github.com/inertiajs/inertia-rails
182
187
  changelog_uri: https://github.com/inertiajs/inertia-rails/CHANGELOG.md
183
- post_install_message:
188
+ post_install_message:
184
189
  rdoc_options: []
185
190
  require_paths:
186
191
  - lib
@@ -195,8 +200,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
200
  - !ruby/object:Gem::Version
196
201
  version: '0'
197
202
  requirements: []
198
- rubygems_version: 3.1.4
199
- signing_key:
203
+ rubygems_version: 3.3.3
204
+ signing_key:
200
205
  specification_version: 4
201
206
  summary: Inertia adapter for Rails
202
207
  test_files: []