turbolinks 2.1.0 → 2.2.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
  SHA1:
3
- metadata.gz: 44d63b0594e6fb0a716f3e4bf5b20fc846d6fcfa
4
- data.tar.gz: ef06a46f52118a4f02ff22d5b021c836ad8383e8
3
+ metadata.gz: d3d08c4d35026032ba9df71e06412e39e6e2808b
4
+ data.tar.gz: bee4e26fe03db79826e124bb1f756f65a067e315
5
5
  SHA512:
6
- metadata.gz: 7590f57d144d29dc0ded39043a95d11d01d32de69172bb82d0899558e587820d1d3ec5571ea17f7ad2bd913ad9f4fc4d348ee74c0fe0e1e3bf22d2933d4fdaf9
7
- data.tar.gz: 8a583b3da14790bafbdd16e86bc59dc8c150c5609f280ba2e14c971f60bce9102d5fc8332b0bc98f9595a95d9fe6e3314de3ee3ea8f3890ad9afb8f5cb948a29
6
+ metadata.gz: 9af7fec4638b55c127964a05d8ad8c9d79db475578656e4b41f5d79c5556023ef635b6c6fc201c3c4dd8a0154715d1ccc434d64764458f68c43fb70701134af1
7
+ data.tar.gz: 4671935850cac59c3f809661847a46dd8d98a5acc6b5a782f32f351e55eb8daaf804ed3ee6b71eaffda503f0b1319addee9086b8d1d05c8b91a5866aa96a9936
data/README.md CHANGED
@@ -64,6 +64,22 @@ To implement a client-side spinner, you could listen for `page:fetch` to start i
64
64
 
65
65
  DOM transformations that are idempotent are best. If you have transformations that are not, hook them to happen only on `page:load` instead of `page:change` (as that would run them again on the cached pages).
66
66
 
67
+ Transition Cache: A Speed Boost
68
+ -------------------------------
69
+
70
+ Transition Cache, added in v2.2.0, makes loading cached pages instantaneous. Once a user has visited a page, returning later to the page results in an instant load.
71
+
72
+ For example, if Page A is already cached by Turbolinks and you are on Page B, clicking a link to Page A will *immediately* display the cached copy of Page A. Turbolinks will then fetch Page A from the server and replace the cached page once the new copy is returned.
73
+
74
+ To enable Transition Cache, include the following in your javascript:
75
+ ```javascript
76
+ Turbolinks.enableTransitionCache();
77
+ ```
78
+
79
+ The one drawback is that dramatic differences in appearence between a cached copy and new copy may lead to a jarring affect for the end-user. This will be especially true for pages that have many moving parts (expandable sections, sortable tables, infinite scrolling, etc.).
80
+
81
+ If you find that a page is causing problems, you can have Turbolinks skip displaying the cached copy by adding `data-no-transition-cache` to any DOM element on the offending page.
82
+
67
83
  Initialization
68
84
  --------------
69
85
 
@@ -170,7 +186,7 @@ Installation
170
186
 
171
187
  1. Add `gem 'turbolinks'` to your Gemfile.
172
188
  1. Run `bundle install`.
173
- 1. Add `//= require turbolinks` to your Javascript manifest file (usually found at `app/assets/javascripts/application.js`).
189
+ 1. Add `//= require turbolinks` to your Javascript manifest file (usually found at `app/assets/javascripts/application.js`). If your manifest requires both turbolinks and jQuery, make sure turbolinks is listed *after* jQuery.
174
190
  1. Restart your server and you're now using turbolinks!
175
191
 
176
192
  Language Ports
@@ -180,6 +196,7 @@ Language Ports
180
196
 
181
197
  * [Flask Turbolinks](https://github.com/lepture/flask-turbolinks) (Python Flask)
182
198
  * [ASP.NET MVC Turbolinks](https://github.com/kazimanzurrashid/aspnetmvcturbolinks)
199
+ * [PHP Turbolinks Component](https://github.com/helthe/Turbolinks) (Symfony Component)
183
200
 
184
201
  Credits
185
202
  -------
@@ -1,18 +1,36 @@
1
- pageCache = {}
2
- cacheSize = 10
3
- currentState = null
4
- loadedAssets = null
5
- htmlExtensions = ['html']
1
+ pageCache = {}
2
+ cacheSize = 10
3
+ transitionCacheEnabled = false
6
4
 
7
- referer = null
5
+ currentState = null
6
+ loadedAssets = null
7
+ htmlExtensions = ['html']
8
8
 
9
- createDocument = null
10
- xhr = null
9
+ referer = null
11
10
 
11
+ createDocument = null
12
+ xhr = null
12
13
 
13
- fetchReplacement = (url) ->
14
+
15
+ fetch = (url) ->
14
16
  rememberReferer()
15
17
  cacheCurrentPage()
18
+ reflectNewUrl url
19
+
20
+ if transitionCacheEnabled and cachedPage = transitionCacheFor(url)
21
+ fetchHistory cachedPage
22
+ fetchReplacement url
23
+ else
24
+ fetchReplacement url, resetScrollPosition
25
+
26
+ transitionCacheFor = (url) ->
27
+ cachedPage = pageCache[url]
28
+ cachedPage if cachedPage and !cachedPage.transitionCacheDisabled
29
+
30
+ enableTransitionCache = (enable = true) ->
31
+ transitionCacheEnabled = enable
32
+
33
+ fetchReplacement = (url, onLoadFunction = =>) ->
16
34
  triggerEvent 'page:fetch', url: url
17
35
 
18
36
  xhr?.abort()
@@ -25,22 +43,19 @@ fetchReplacement = (url) ->
25
43
  triggerEvent 'page:receive'
26
44
 
27
45
  if doc = processResponse()
28
- reflectNewUrl url
29
46
  changePage extractTitleAndBody(doc)...
30
47
  reflectRedirectedUrl()
31
- resetScrollPosition()
48
+ onLoadFunction()
32
49
  triggerEvent 'page:load'
33
50
  else
34
51
  document.location.href = url
35
52
 
36
53
  xhr.onloadend = -> xhr = null
37
- xhr.onabort = -> rememberCurrentUrl()
38
54
  xhr.onerror = -> document.location.href = url
39
55
 
40
56
  xhr.send()
41
57
 
42
58
  fetchHistory = (cachedPage) ->
43
- cacheCurrentPage()
44
59
  xhr?.abort()
45
60
  changePage cachedPage.title, cachedPage.body
46
61
  recallScrollPosition cachedPage
@@ -48,12 +63,14 @@ fetchHistory = (cachedPage) ->
48
63
 
49
64
 
50
65
  cacheCurrentPage = ->
51
- pageCache[currentState.position] =
52
- url: document.location.href,
53
- body: document.body,
54
- title: document.title,
55
- positionY: window.pageYOffset,
56
- positionX: window.pageXOffset
66
+ pageCache[currentState.url] =
67
+ url: document.location.href,
68
+ body: document.body,
69
+ title: document.title,
70
+ positionY: window.pageYOffset,
71
+ positionX: window.pageXOffset,
72
+ cachedAt: new Date().getTime(),
73
+ transitionCacheDisabled: document.querySelector('[data-no-transition-cache]')?
57
74
 
58
75
  constrainPageCacheTo cacheSize
59
76
 
@@ -61,10 +78,15 @@ pagesCached = (size = cacheSize) ->
61
78
  cacheSize = parseInt(size) if /^[\d]+$/.test size
62
79
 
63
80
  constrainPageCacheTo = (limit) ->
64
- for own key, value of pageCache when key <= currentState.position - limit
81
+ pageCacheKeys = Object.keys pageCache
82
+
83
+ cacheTimesRecentFirst = pageCacheKeys.map (url) ->
84
+ pageCache[url].cachedAt
85
+ .sort (a, b) -> b - a
86
+
87
+ for key in pageCacheKeys when pageCache[key].cachedAt <= cacheTimesRecentFirst[limit]
65
88
  triggerEvent 'page:expire', pageCache[key]
66
- pageCache[key] = null
67
- return
89
+ delete pageCache[key]
68
90
 
69
91
  changePage = (title, body, csrfToken, runScripts) ->
70
92
  document.title = title
@@ -92,7 +114,7 @@ removeNoscriptTags = (node) ->
92
114
 
93
115
  reflectNewUrl = (url) ->
94
116
  if url isnt referer
95
- window.history.pushState { turbolinks: true, position: currentState.position + 1 }, '', url
117
+ window.history.pushState { turbolinks: true, url: url }, '', url
96
118
 
97
119
  reflectRedirectedUrl = ->
98
120
  if location = xhr.getResponseHeader 'X-XHR-Redirected-To'
@@ -103,7 +125,7 @@ rememberReferer = ->
103
125
  referer = document.location.href
104
126
 
105
127
  rememberCurrentUrl = ->
106
- window.history.replaceState { turbolinks: true, position: Date.now() }, '', document.location.href
128
+ window.history.replaceState { turbolinks: true, url: document.location.href }, '', document.location.href
107
129
 
108
130
  rememberCurrentState = ->
109
131
  currentState = window.history.state
@@ -282,7 +304,8 @@ installJqueryAjaxSuccessPageUpdateTrigger = ->
282
304
 
283
305
  installHistoryChangeHandler = (event) ->
284
306
  if event.state?.turbolinks
285
- if cachedPage = pageCache[event.state.position]
307
+ if cachedPage = pageCache[event.state.url]
308
+ cacheCurrentPage()
286
309
  fetchHistory cachedPage
287
310
  else
288
311
  visit event.target.location.href
@@ -318,7 +341,7 @@ if browserSupportsCustomEvents
318
341
  installJqueryAjaxSuccessPageUpdateTrigger()
319
342
 
320
343
  if browserSupportsTurbolinks
321
- visit = fetchReplacement
344
+ visit = fetch
322
345
  initializeTurbolinks()
323
346
  else
324
347
  visit = (url) -> document.location.href = url
@@ -327,6 +350,7 @@ else
327
350
  # Turbolinks.visit(url)
328
351
  # Turbolinks.pagesCached()
329
352
  # Turbolinks.pagesCached(20)
353
+ # Turbolinks.enableTransitionCache()
330
354
  # Turbolinks.allowLinkExtensions('md')
331
355
  # Turbolinks.supported
332
- @Turbolinks = { visit, pagesCached, allowLinkExtensions, supported: browserSupportsTurbolinks }
356
+ @Turbolinks = { visit, pagesCached, enableTransitionCache, allowLinkExtensions, supported: browserSupportsTurbolinks }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbolinks
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-18 00:00:00.000000000 Z
11
+ date: 2014-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coffee-rails
@@ -61,9 +61,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  version: '0'
62
62
  requirements: []
63
63
  rubyforge_project:
64
- rubygems_version: 2.0.5
64
+ rubygems_version: 2.0.3
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: Turbolinks makes following links in your web application faster (use with
68
68
  Rails Asset Pipeline)
69
69
  test_files: []
70
+ has_rdoc: