turbolinks 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -50,7 +50,11 @@ Also, Turbolinks is installed as the last click handler for links. So if you ins
50
50
  Asset change detection
51
51
  ----------------------
52
52
 
53
- Turbolinks will remember what assets were linked or referenced in the head of the initial page. If those assets change, either more or added or existing ones have a new URL, the page will do a full reload instead of going through Turbolinks. This ensures that all Turbolinks sessions will always be running off your latest JavaScript and CSS.
53
+ You can track certain assets, like application.js and application.css, that you want to ensure are always of the latest version inside a Turbolinks session. This is done by marking those asset links with data-turbolinks-track, like so:
54
+
55
+ <link href="/assets/application.css?9bd64a86adb3cd9ab3b16e9dca67a33a.css" media="screen" rel="stylesheet" type="text/css" data-turbolinks-track>
56
+
57
+ If those assets change URLs (embed an md5 stamp to ensure this), the page will do a full reload instead of going through Turbolinks. This ensures that all Turbolinks sessions will always be running off your latest JavaScript and CSS.
54
58
 
55
59
  When this happens, you'll technically be requesting the same page twice. Once through Turbolinks to detect that the assets changed, and then again when we do a full redirect to that page.
56
60
 
@@ -73,6 +77,14 @@ Full speed for pushState browsers, graceful fallback for everything else
73
77
  Like pjax, this naturally only works with browsers capable of pushState. But of course we fall back gracefully to full page reloads for browsers that do not support it.
74
78
 
75
79
 
80
+ Compatibility
81
+ -------------
82
+
83
+ Turbolinks is designed to work with any browser that fully supports pushState and all the related APIs. This includes Safari 6.0+ (but not Safari 5.1.x!), IE10, and latest Chromes and Firefoxes.
84
+
85
+ Do note that existing JavaScript libraries may not all be compatible with Turbolinks out of the box due to the change in instantiation cycle. You might very well have to modify them to work with Turbolinks' new set of events.
86
+
87
+
76
88
  Installation
77
89
  ------------
78
90
 
@@ -1,7 +1,7 @@
1
1
  initialized = false
2
2
  currentState = null
3
3
  referer = document.location.href
4
- assets = []
4
+ trackingAssets = []
5
5
  pageCache = {}
6
6
  createDocument = null
7
7
 
@@ -72,7 +72,16 @@ changePage = (title, body, runScripts) ->
72
72
  triggerEvent 'page:change'
73
73
 
74
74
  executeScriptTags = ->
75
- eval(script.innerHTML) for script in document.body.getElementsByTagName 'script' when script.type in ['', 'text/javascript']
75
+ for script in document.body.getElementsByTagName 'script' when script.type in ['', 'text/javascript']
76
+ if script.src? and script.src isnt '' and not script.getAttribute('data-turbolinks-evaluated')?
77
+ copy = document.createElement 'script'
78
+ copy.setAttribute attr.name, attr.value for attr in script.attributes
79
+ copy.setAttribute 'data-turbolinks-evaluated', ''
80
+ parent = script.parentNode
81
+ parent.removeChild script
82
+ parent.insertBefore copy, parent.childNodes[0]
83
+ else
84
+ window.eval(script.innerHTML)
76
85
 
77
86
 
78
87
  reflectNewUrl = (url) ->
@@ -81,8 +90,8 @@ reflectNewUrl = (url) ->
81
90
  window.history.pushState { turbolinks: true, position: currentState.position + 1 }, '', url
82
91
 
83
92
  reflectRedirectedUrl = (xhr) ->
84
- if (location = xhr.getResponseHeader('X-XHR-Current-Location'))
85
- window.history.replaceState currentState, '', location
93
+ unless (location = xhr.getResponseHeader 'X-XHR-Current-Location') is document.location.pathname + document.location.search
94
+ window.history.replaceState currentState, '', location + document.location.hash
86
95
 
87
96
  rememberCurrentUrl = ->
88
97
  window.history.replaceState { turbolinks: true, position: Date.now() }, '', document.location.href
@@ -90,8 +99,8 @@ rememberCurrentUrl = ->
90
99
  rememberCurrentState = ->
91
100
  currentState = window.history.state
92
101
 
93
- rememberCurrentAssets = ->
94
- assets = extractAssets document
102
+ rememberCurrentTrackingAssets = ->
103
+ trackingAssets = extractTrackAssets document
95
104
 
96
105
  rememberInitialPage = ->
97
106
  unless initialized
@@ -112,13 +121,13 @@ triggerEvent = (name) ->
112
121
  event.initEvent name, true, true
113
122
  document.dispatchEvent event
114
123
 
124
+ extractTrackAssets = (doc) ->
125
+ (node.src || node.href) for node in doc.head.childNodes when node.getAttribute?('data-turbolinks-track')?
115
126
 
116
- extractAssets = (doc) ->
117
- (node.src || node.href) for node in doc.head.childNodes when node.src or node.href
118
127
 
119
128
  assetsChanged = (doc)->
120
- extractedAssets = extractAssets doc
121
- extractedAssets.length isnt assets.length or intersection(extractedAssets, assets).length != assets.length
129
+ extractedTrackAssets = extractTrackAssets doc
130
+ extractedTrackAssets.length isnt trackingAssets.length or intersection(extractedTrackAssets, trackingAssets).length != trackingAssets.length
122
131
 
123
132
  intersection = (a, b) ->
124
133
  [a, b] = [b, a] if a.length > b.length
@@ -163,7 +172,7 @@ handleClick = (event) ->
163
172
 
164
173
  extractLink = (event) ->
165
174
  link = event.target
166
- link = link.parentNode until link is document or !link or link.nodeName is 'A'
175
+ link = link.parentNode until !link.parentNode or link.nodeName is 'A'
167
176
  link
168
177
 
169
178
  crossOriginLink = (link) ->
@@ -196,7 +205,7 @@ browserSupportsPushState =
196
205
  window.history and window.history.pushState and window.history.replaceState and window.history.state != undefined
197
206
 
198
207
  if browserSupportsPushState
199
- rememberCurrentAssets()
208
+ rememberCurrentTrackingAssets()
200
209
  document.addEventListener 'click', installClickHandlerLast, true
201
210
 
202
211
  window.addEventListener 'popstate', (event) ->
data/test/index.html CHANGED
@@ -16,10 +16,10 @@
16
16
  <li><a href="/other.html"><span>Wrapped link</span></a></li>
17
17
  <li><a href="http://www.google.com/">Cross origin</a></li>
18
18
  <li><a href="/other.html" onclick="if(!confirm('follow link?')) { return false}">Confirm Fire Order</a></li>
19
- <li><a href="/reload.html"><span>Asset Change</span></a></li>
19
+ <li><a href="/reload.html"><span>New assets track </span></a></li>
20
20
  <li><a href="/dummy.gif?12345">Query Param Image Link</a></li>
21
21
  <li><a href="#">Hash link</a></li>
22
- <li><a href="/reload.html#foo">Asset Change with hash link</a></li>
22
+ <li><a href="/reload.html#foo">New assets track with hash link</a></li>
23
23
  </ul>
24
24
 
25
25
  <div style="background:#ccc;height:5000px;width:200px;">
data/test/reload.html CHANGED
@@ -3,7 +3,7 @@
3
3
  <head>
4
4
  <meta charset="utf-8">
5
5
  <title>Home</title>
6
- <script type="text/javascript" src="/js/turbolinks.js?1"></script>
6
+ <script type="text/javascript" src="/js/turbolinks.js?1" data-turbolinks-track='true'></script>
7
7
  <script type="text/javascript">
8
8
  document.addEventListener("page:change", function() {
9
9
  console.log("page changed");
metadata CHANGED
@@ -1,32 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbolinks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
5
4
  prerelease:
5
+ version: 0.6.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - David Heinemeier Hansson
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-26 00:00:00.000000000 Z
12
+ date: 2012-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: coffee-rails
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
15
+ version_requirements: !ruby/object:Gem::Requirement
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
20
+ none: false
21
+ name: coffee-rails
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
24
+ requirement: !ruby/object:Gem::Requirement
26
25
  requirements:
27
26
  - - ! '>='
28
27
  - !ruby/object:Gem::Version
29
28
  version: '0'
29
+ none: false
30
30
  description:
31
31
  email: david@loudthinking.com
32
32
  executables: []
@@ -49,17 +49,17 @@ rdoc_options: []
49
49
  require_paths:
50
50
  - lib
51
51
  required_ruby_version: !ruby/object:Gem::Requirement
52
- none: false
53
52
  requirements:
54
53
  - - ! '>='
55
54
  - !ruby/object:Gem::Version
56
55
  version: '0'
57
- required_rubygems_version: !ruby/object:Gem::Requirement
58
56
  none: false
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
58
  requirements:
60
59
  - - ! '>='
61
60
  - !ruby/object:Gem::Version
62
61
  version: '0'
62
+ none: false
63
63
  requirements: []
64
64
  rubyforge_project:
65
65
  rubygems_version: 1.8.23