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 +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +5 -9
- 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_generator.rb +10 -0
- 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 +11 -0
- data/lib/inertia_rails/version.rb +1 -1
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 168a95bf6f07afa800c4886e447a035e765a5d7247b0ba2341c9c9b700a46ef9
|
4
|
+
data.tar.gz: ff006c26b44dff8e5f6f2d0e0477a2af6322958d1ea55dd65da7bb19e79be14a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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/)
|
@@ -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
|
@@ -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
|
@@ -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?
|
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.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:
|
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.
|
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: []
|