Skip to content

Commit

Permalink
Fixed several issues: Don't cache back btn, debounce requests, don't …
Browse files Browse the repository at this point in the history
…add duplicate links to history.
  • Loading branch information
robgraeber committed Mar 19, 2016
1 parent bc6f9f2 commit b0c0443
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
build
.DS_Store
.idea/
23 changes: 23 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# [InstantClick](http://instantclick.io/)

Modified version of InstantClick with latest bug fixes + tweaks. Changelog:
- Option to not cache the browser back button.
- Basic preload caching, to save on unnecessary fetches.
- Fix history bug when clicking on the same link multiple times.

####Install

```
npm install instantclick2
```

####How to use

```
var InstantClick = require('instantclick2');
InstantClick.init({
preloadingMode: 50,
cacheBrowserBackBtn: true
});
```

---
All the informations you need to use InstantClick are on the link above. This ReadMe’s purpose is about how to use and contribute to a development version of InstantClick.

## Tests
Expand Down
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "instantclick2",
"version": "1.0.1",
"description": "Modified version of InstantClick with latest bug fixes + tweaks",
"main": "src/instantclick.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/robgraeber/instantclick2.git"
},
"keywords": [
"instant",
"click",
"pjax",
"spa"
],
"author": "Rob Graeber",
"license": "MIT",
"bugs": {
"url": "https://github.com/robgraeber/instantclick2/issues"
},
"homepage": "https://github.com/robgraeber/instantclick2#readme"
}
49 changes: 44 additions & 5 deletions src/instantclick.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var instantClick
, $urlToPreload
, $preloadTimer
, $lastTouchTimestamp
, $preloadCacheTimeLimit = 30000 //caches preloaded page for 30 seconds
, $lastPreloadTimeDict = {}

// Preloading-related variables
, $history = {}
Expand All @@ -23,6 +25,7 @@ var instantClick

// Variables defined by public functions
, $preloadOnMousedown
, $cacheBrowserBackBtn = true
, $delayBeforePreload
, $eventsCallbacks = {
fetch: [],
Expand Down Expand Up @@ -111,10 +114,10 @@ var instantClick
/* We cannot just use `document.body = doc.body`, it causes Safari (tested
5.1, 6.0 and Mobile 7.0) to execute script tags directly.
*/

if (newUrl) {
history.pushState(null, null, newUrl)

if (window.location.href !== newUrl){
history.pushState(null, null, newUrl)
}
var hashIndex = newUrl.indexOf('#')
, hashElem = hashIndex > -1
&& document.getElementById(newUrl.substr(hashIndex + 1))
Expand Down Expand Up @@ -149,6 +152,7 @@ var instantClick
document.title = title
}

$lastPreloadTimeDict = {};
instantanize()
if (pop) {
triggerPageEvent('restore')
Expand Down Expand Up @@ -252,8 +256,12 @@ var instantClick
if (!$isPreloading || $isWaitingForCompletion) {
return
}
if ($xhr.readyState > 1 && $xhr.readyState < 4) {
$lastPreloadTimeDict = {};
}
$xhr.abort()
setPreloadingAsHalted()

}

function readystatechangeListener() {
Expand Down Expand Up @@ -321,6 +329,10 @@ var instantClick
}

function popstateListener() {
if (!$cacheBrowserBackBtn) {
location.href = location.href;
return;
}
var loc = removeHash(location.href)
if (loc == $currentLocationWithoutHash) {
return
Expand Down Expand Up @@ -403,6 +415,7 @@ var instantClick

return
}

if ($preloadTimer) {
clearTimeout($preloadTimer)
$preloadTimer = false
Expand All @@ -411,14 +424,24 @@ var instantClick
if (!url) {
url = $urlToPreload
}

if ($isPreloading && (url == $url || $isWaitingForCompletion)) {
return
}
$isPreloading = true
$isWaitingForCompletion = false

$url = url

//prevent preloading the same page twice
if ($lastPreloadTimeDict[url] && $lastPreloadTimeDict[url] + $preloadCacheTimeLimit > new Date().getTime()) {
return;
} else {
//clears the dict, since we only support preloading 1 page at a time
$lastPreloadTimeDict = {};
$lastPreloadTimeDict[url] = new Date().getTime();
}

$body = false
$mustRedirect = false
$timing = {
Expand Down Expand Up @@ -525,7 +548,19 @@ var instantClick
Because of this mess, the only whitelisted browser on Android is Chrome.
*/

function init(preloadingMode) {
function init(options) {
var preloadingMode;

if (typeof options !== 'object') {
//legacy parameters
preloadingMode = options;
} else {
preloadingMode = options.preloadingMode;
if (options.cacheBrowserBackBtn !== undefined) {
$cacheBrowserBackBtn = options.cacheBrowserBackBtn;
}
}

if ($currentLocationWithoutHash) {
/* Already initialized */
return
Expand Down Expand Up @@ -588,3 +623,7 @@ var instantClick
}

}(document, location, navigator.userAgent);

if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = InstantClick;
}

0 comments on commit b0c0443

Please sign in to comment.