turbolinks_render 0.9.10 → 0.9.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/turbolinks_render/middleware.rb +57 -50
- data/lib/turbolinks_render/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60f02af004bd119d0d38ee2678d8128b93f1e6756c641c60972f7707e755593d
|
4
|
+
data.tar.gz: cee50736d7790b5f9e795c4ad2421274cc3020c1b47e5ed56e48781ccb81784e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dd95e4590be8d63d6c0b57ba1ee0199d5d222537a3fb623379774ff433b6bbfd2aeb56a5ad7a1e9d063431a4c3cd2450bc9630ea014b6e9274eb1f50250d771
|
7
|
+
data.tar.gz: f31224ef7f37349d795982627ad50bb9902eebef345c62afa3e0f8210f31b16e2ae8e313fef79a691e67a03dc0814a8a007a3b4ff700d98316ea160af7435d2e
|
data/README.md
CHANGED
@@ -30,7 +30,7 @@ By default, `render` will be handled by Turbolinks if these conditions are met:
|
|
30
30
|
|
31
31
|
When these conditions are met and `render` is used:
|
32
32
|
|
33
|
-
- The
|
33
|
+
- The page content is replaced with the rendered content with Javascript. It uses Turbolinks to do this replacement, so it is pretty smart about keeping the `<head>` when it's identical, executing `<script>` elements, etc.
|
34
34
|
- An event `turbolinks:load` is dispatched
|
35
35
|
|
36
36
|
You can disable turbolinks on a given request with:
|
@@ -1,55 +1,39 @@
|
|
1
1
|
module TurbolinksRender
|
2
2
|
class Middleware
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def call(env)
|
8
|
-
@request = Rack::Request.new(env)
|
9
|
-
@request.set_header('X-Turbolinks-Render-Candidate', turbolinks_response_candidate?)
|
10
|
-
|
11
|
-
@status, @headers, @response = @app.call(env)
|
12
|
-
|
13
|
-
return [@status, @headers, @response] if file? || (!@response.respond_to?(:body) && !@response.respond_to?(:[]))
|
14
|
-
|
15
|
-
body = @response.respond_to?(:body) ? @response.body : @response[0]
|
16
|
-
body = render_body_with_turbolinks(body) if render_with_turbolinks?
|
17
|
-
[@status, @headers, [body]]
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
3
|
+
Request = Struct.new(:request) do
|
4
|
+
def candidate_for_turbolinks?
|
5
|
+
request.xhr? && !request.get?
|
6
|
+
end
|
21
7
|
|
22
|
-
|
23
|
-
|
24
|
-
build_turbolinks_response_to_render(body).tap do |turbolinks_body|
|
25
|
-
@headers["Content-Length"] = turbolinks_body.bytesize
|
8
|
+
def turbolinks_render_option
|
9
|
+
@turbolinks_render_option ||= request.get_header('X-Turbolinks-Render-Option')
|
26
10
|
end
|
27
11
|
end
|
28
12
|
|
29
|
-
|
30
|
-
|
31
|
-
|
13
|
+
Response = Struct.new(:status, :headers, :response) do
|
14
|
+
def candidate_for_turbolinks?
|
15
|
+
html_response?
|
16
|
+
end
|
32
17
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
18
|
+
def turbolinks_body
|
19
|
+
@turbolinks_body ||= js_code_to_render_html(body)
|
20
|
+
end
|
37
21
|
|
38
|
-
|
39
|
-
@request.get_header('X-Turbolinks-Render-Option')
|
40
|
-
end
|
22
|
+
private
|
41
23
|
|
42
|
-
|
43
|
-
|
44
|
-
|
24
|
+
def html_response?
|
25
|
+
headers['Content-Type'] =~ /text\/html/
|
26
|
+
end
|
45
27
|
|
46
|
-
|
47
|
-
|
48
|
-
|
28
|
+
def body
|
29
|
+
body = ''
|
30
|
+
response.each {|part| body << part}
|
31
|
+
body
|
32
|
+
end
|
49
33
|
|
50
|
-
|
51
|
-
|
52
|
-
|
34
|
+
def js_code_to_render_html(html)
|
35
|
+
escaped_html = ActionController::Base.helpers.j(html)
|
36
|
+
<<-JS
|
53
37
|
(function(){
|
54
38
|
function renderWithTurbolinks(htmlContent){
|
55
39
|
var currentSnapshot = Turbolinks.Snapshot.fromHTMLElement(document.documentElement);
|
@@ -58,27 +42,50 @@ module TurbolinksRender
|
|
58
42
|
var nullDelegate = {viewInvalidated: nullCallback, viewWillRender: nullCallback, viewRendered: nullCallback};
|
59
43
|
|
60
44
|
var renderer = new Turbolinks.SnapshotRenderer(currentSnapshot, newSpanshot, false);
|
61
|
-
renderer.
|
62
|
-
if(renderer.shouldRender()){
|
63
|
-
renderer.render(nullCallback);
|
64
|
-
}
|
65
|
-
else{
|
45
|
+
if(!renderer.shouldRender()){
|
66
46
|
renderer = new Turbolinks.ErrorRenderer(htmlContent);
|
67
|
-
renderer.delegate = nullDelegate;
|
68
|
-
renderer.render(nullCallback);
|
69
47
|
}
|
48
|
+
renderer.delegate = nullDelegate;
|
49
|
+
renderer.render(nullCallback);
|
70
50
|
}
|
71
51
|
Turbolinks.clearCache();
|
72
52
|
renderWithTurbolinks("#{escaped_html}");
|
73
53
|
Turbolinks.dispatch('turbolinks:load');
|
74
54
|
window.scroll(0, 0);
|
75
55
|
})();
|
76
|
-
|
56
|
+
JS
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def initialize(app)
|
61
|
+
@app = app
|
62
|
+
end
|
63
|
+
|
64
|
+
def call(env)
|
65
|
+
rack_request = Rack::Request.new(env)
|
66
|
+
request = Request.new(rack_request)
|
67
|
+
rack_request.set_header('X-Turbolinks-Render-Candidate', request.candidate_for_turbolinks?)
|
68
|
+
|
69
|
+
rack_status, rack_headers, rack_response = @app.call(env)
|
70
|
+
response = Response.new(rack_status, rack_headers, rack_response)
|
71
|
+
|
72
|
+
return [rack_status, rack_headers, rack_response] unless render_with_turbolinks?(request, response)
|
73
|
+
|
74
|
+
rack_headers["Content-Type"] = 'text/javascript'
|
75
|
+
rack_headers["Content-Length"] = response.turbolinks_body.bytesize
|
76
|
+
|
77
|
+
[rack_status, rack_headers, [response.turbolinks_body]]
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def render_with_turbolinks?(request, response)
|
83
|
+
request.candidate_for_turbolinks? && response.candidate_for_turbolinks? &&
|
84
|
+
(request.turbolinks_render_option || (render_with_turbolinks_by_default? && request.turbolinks_render_option != false))
|
77
85
|
end
|
78
86
|
|
79
87
|
def render_with_turbolinks_by_default?
|
80
88
|
Rails.application.config.turbolinks_render.render_with_turbolinks_by_default
|
81
89
|
end
|
82
|
-
|
83
90
|
end
|
84
91
|
end
|