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 +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +5 -9
- data/lib/generators/inertia_rails/install/{react.jsx → react/InertiaExample.jsx} +0 -0
- data/lib/generators/inertia_rails/install/{inertia.jsx → react/inertia.jsx} +0 -0
- data/lib/generators/inertia_rails/install/svelte/InertiaExample.svelte +11 -0
- data/lib/generators/inertia_rails/install/svelte/inertia.js +19 -0
- data/lib/generators/inertia_rails/install/vue/InertiaExample.vue +11 -0
- data/lib/generators/inertia_rails/install/vue/inertia.js +24 -0
- data/lib/generators/inertia_rails/install_generator.rb +25 -3
- data/lib/inertia_rails/controller.rb +2 -0
- data/lib/inertia_rails/helper.rb +7 -0
- data/lib/inertia_rails/inertia_rails.rb +20 -0
- data/lib/inertia_rails/middleware.rb +65 -53
- data/lib/inertia_rails/renderer.rb +12 -0
- data/lib/inertia_rails/version.rb +1 -1
- metadata +14 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 468c8c88d85da84a010be47c474e9d8d1ed4735d5d83cd8058038401e322334c
|
4
|
+
data.tar.gz: ce4ccdf41063f88495e8829dbefc4eb92936850d9f4da8a4f51e907eff9f90f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
2
|
-
|
3
|
-
Visit [inertiajs.com](https://inertiajs.com/) to learn more.
|
1
|
+

|
4
2
|
|
5
|
-
# Note to pre Rubygems release users
|
6
3
|
|
7
|
-
|
4
|
+
# Inertia.js Rails Adapter
|
8
5
|
|
9
|
-
|
6
|
+
Visit [inertiajs.com](https://inertiajs.com/) for installation instructions, documentation, and to learn more.
|
10
7
|
|
11
|
-
|
8
|
+
*Maintained and sponsored by the team at [bellaWatt](https://bellawatt.com/)*
|
12
9
|
|
13
|
-
|
14
|
-
2. Change any `Inertia.configure` calls to `InertiaRails.configure`
|
10
|
+
[](https://bellawatt.com/)
|
File without changes
|
File without changes
|
@@ -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,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
|
@@ -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
|
-
@
|
8
|
+
InertiaRailsRequest.new(@app, env)
|
9
|
+
.response
|
10
|
+
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
+
class InertiaRailsRequest
|
13
|
+
def initialize(app, env)
|
14
|
+
@app = app
|
15
|
+
@env = env
|
16
|
+
end
|
12
17
|
|
13
|
-
|
18
|
+
def response
|
19
|
+
status, headers, body = @app.call(@env)
|
20
|
+
request = ActionDispatch::Request.new(@env)
|
14
21
|
|
15
|
-
|
16
|
-
request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
|
22
|
+
::InertiaRails.reset!
|
17
23
|
|
18
|
-
|
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
|
-
|
21
|
-
end
|
27
|
+
status = 303 if inertia_non_post_redirect?(status)
|
22
28
|
|
23
|
-
|
29
|
+
stale_inertia_get? ? force_refresh(request) : [status, headers, body]
|
30
|
+
end
|
24
31
|
|
25
|
-
|
26
|
-
redirect_status?(status) || stale_inertia_request?
|
27
|
-
end
|
32
|
+
private
|
28
33
|
|
29
|
-
|
30
|
-
|
31
|
-
|
34
|
+
def keep_inertia_errors?(status)
|
35
|
+
redirect_status?(status) || stale_inertia_request?
|
36
|
+
end
|
32
37
|
|
33
|
-
|
34
|
-
|
35
|
-
|
38
|
+
def stale_inertia_request?
|
39
|
+
inertia_request? && version_stale?
|
40
|
+
end
|
36
41
|
|
37
|
-
|
38
|
-
|
39
|
-
|
42
|
+
def redirect_status?(status)
|
43
|
+
[301, 302].include? status
|
44
|
+
end
|
40
45
|
|
41
|
-
|
42
|
-
|
43
|
-
|
46
|
+
def non_get_redirectable_method?
|
47
|
+
['PUT', 'PATCH', 'DELETE'].include? request_method
|
48
|
+
end
|
44
49
|
|
45
|
-
|
46
|
-
|
47
|
-
|
50
|
+
def inertia_non_post_redirect?(status)
|
51
|
+
inertia_request? && redirect_status?(status) && non_get_redirectable_method?
|
52
|
+
end
|
48
53
|
|
49
|
-
|
50
|
-
|
51
|
-
|
54
|
+
def stale_inertia_get?
|
55
|
+
get? && stale_inertia_request?
|
56
|
+
end
|
52
57
|
|
53
|
-
|
54
|
-
|
55
|
-
|
58
|
+
def get?
|
59
|
+
request_method == 'GET'
|
60
|
+
end
|
56
61
|
|
57
|
-
|
58
|
-
|
59
|
-
|
62
|
+
def request_method
|
63
|
+
@env['REQUEST_METHOD']
|
64
|
+
end
|
60
65
|
|
61
|
-
|
62
|
-
|
63
|
-
|
66
|
+
def inertia_version
|
67
|
+
@env['HTTP_X_INERTIA_VERSION']
|
68
|
+
end
|
64
69
|
|
65
|
-
|
66
|
-
|
67
|
-
|
70
|
+
def inertia_request?
|
71
|
+
@env['HTTP_X_INERTIA'].present?
|
72
|
+
end
|
68
73
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
end
|
74
|
+
def version_stale?
|
75
|
+
sent_version != saved_version
|
76
|
+
end
|
73
77
|
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
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?
|
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.
|
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:
|
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/
|
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.
|
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: []
|