history-extra
Advanced tools
Comparing version
### Changelog | ||
#### [v4.0.2](https://github.com/w33ble/history-extra/compare/v4.0.1...v4.0.2) (16 November 2018) | ||
- Fix: Allow push/replace on same path [`#1`](https://github.com/w33ble/history-extra/pull/1) | ||
- fix: support push and replace to same path [`658d4c7`](https://github.com/w33ble/history-extra/commit/658d4c7dad938b5aba32a95361e60aa5c3f8cdaa) | ||
- docs: add script and env arg details [`2075435`](https://github.com/w33ble/history-extra/commit/2075435915a19a9f25092563bf5f3ae3e22df477) | ||
- fix: better esm module in build [`19efdf2`](https://github.com/w33ble/history-extra/commit/19efdf28daeccde941f95005aa84a76823335b6a) | ||
#### [v4.0.1](https://github.com/w33ble/history-extra/compare/v4.0.0...v4.0.1) (14 November 2018) | ||
@@ -4,0 +10,0 @@ - fix: use default exports in src [`8bca3b2`](https://github.com/w33ble/history-extra/commit/8bca3b22bde7366ca1fac65bc2c1c6eaf750faf6) |
/* | ||
* history-extra version 4.0.1 | ||
* history-extra version 4.0.2 | ||
* | ||
@@ -31,7 +31,7 @@ * The MIT License (MIT) | ||
(global.history = factory(global.LocationUtils,global.PathUtils,global.createTransitionManager,global.DOMUtils)); | ||
}(this, (function (locationUtils,PathUtils,createTransitionManager,DOMUtils) { 'use strict'; | ||
}(this, (function (locationUtils,PathUtils,_createTransitionManager,DOMUtils) { 'use strict'; | ||
locationUtils = locationUtils && locationUtils.hasOwnProperty('default') ? locationUtils['default'] : locationUtils; | ||
PathUtils = PathUtils && PathUtils.hasOwnProperty('default') ? PathUtils['default'] : PathUtils; | ||
createTransitionManager = createTransitionManager && createTransitionManager.hasOwnProperty('default') ? createTransitionManager['default'] : createTransitionManager; | ||
_createTransitionManager = _createTransitionManager && _createTransitionManager.hasOwnProperty('default') ? _createTransitionManager['default'] : _createTransitionManager; | ||
DOMUtils = DOMUtils && DOMUtils.hasOwnProperty('default') ? DOMUtils['default'] : DOMUtils; | ||
@@ -53,4 +53,6 @@ | ||
supportsGoWithoutReloadUsingHash = DOMUtils.supportsGoWithoutReloadUsingHash, | ||
supportsHistory = DOMUtils.supportsHistory; | ||
supportsHistory = DOMUtils.supportsHistory; // goofy hack to handle cjs and esm differences in build | ||
var createTransitionManager = Object.hasOwnProperty.call(_createTransitionManager, 'default') ? _createTransitionManager.default : _createTransitionManager; | ||
function warning(condition, message) { | ||
@@ -67,2 +69,3 @@ if (condition) return; // eslint-disable-next-line no-console | ||
var PopStateEvent = 'popstate'; | ||
var HashChangeEvent = 'hashchange'; | ||
@@ -235,36 +238,38 @@ | ||
var encodedPath = encodePath(basename + path); | ||
var hashChanged = getHashPath() !== encodedPath; | ||
if (hashChanged) { | ||
// We cannot tell if a hashchange was caused by a PUSH, so we'd | ||
// rather setState here and ignore the hashchange. The caveat here | ||
// is that other hash histories in the page will consider it a POP. | ||
if (canUseHistory) { | ||
// eslint-disable-next-line no-shadow | ||
var key = location.key, | ||
_state = location.state; | ||
var href = createHref(location); | ||
globalHistory.pushState({ | ||
key: key, | ||
state: _state | ||
}, null, href); | ||
} else { | ||
// legacy fallback | ||
ignorePath = path; | ||
var hashChanged = getHashPath() !== encodedPath; | ||
if (canUseHistory) { | ||
// eslint-disable-next-line no-shadow | ||
var key = location.key, | ||
_state = location.state; | ||
var href = createHref(location); | ||
globalHistory.pushState({ | ||
key: key, | ||
state: _state | ||
}, null, href); | ||
if (hashChanged) { | ||
// We cannot tell if a hashchange was caused by a PUSH, so we'd | ||
// rather setState here and ignore the hashchange. The caveat here | ||
// is that other hash histories in the page will consider it a POP. | ||
warning(false, 'Hash history cannot PUSH the same path; a new entry will not be added to the history stack'); | ||
setState(); | ||
} else { | ||
warning(state === undefined, 'Browser history cannot push state in browsers that do not support HTML5 history, state is ignored'); | ||
pushHashPath(encodedPath); | ||
} | ||
var prevIndex = allPaths.lastIndexOf(createPath(history.location)); | ||
var nextPaths = allPaths.slice(0, prevIndex === -1 ? 0 : prevIndex + 1); | ||
nextPaths.push(path); | ||
allPaths = nextPaths; | ||
setState({ | ||
action: action, | ||
location: location | ||
}); | ||
} else { | ||
warning(false, 'Hash history cannot PUSH the same path; a new entry will not be added to the history stack'); | ||
setState(); | ||
pushHashPath(encodedPath); | ||
} | ||
var prevIndex = allPaths.lastIndexOf(createPath(history.location)); | ||
var nextPaths = allPaths.slice(0, prevIndex === -1 ? 0 : prevIndex + 1); | ||
nextPaths.push(path); | ||
allPaths = nextPaths; | ||
setState({ | ||
action: action, | ||
location: location | ||
}); | ||
}); | ||
@@ -283,21 +288,22 @@ }; // eslint-disable-next-line no-shadow | ||
var encodedPath = encodePath(basename + path); | ||
var hashChanged = getHashPath() !== encodedPath; | ||
if (hashChanged) { | ||
// We cannot tell if a hashchange was caused by a REPLACE, so we'd | ||
// rather setState here and ignore the hashchange. The caveat here | ||
// is that other hash histories in the page will consider it a POP. | ||
ignorePath = path; | ||
if (canUseHistory) { | ||
// eslint-disable-next-line no-shadow | ||
var key = location.key, | ||
_state2 = location.state; | ||
var href = createHref(location); | ||
globalHistory.replaceState({ | ||
key: key, | ||
state: _state2 | ||
}, null, href); | ||
} else { | ||
// legacy fallback | ||
warning(state === undefined, 'Browser history cannot push state in browsers that do not support HTML5 history, state is ignored'); | ||
var hashChanged = getHashPath() !== encodedPath; | ||
if (canUseHistory) { | ||
// eslint-disable-next-line no-shadow | ||
var key = location.key, | ||
_state2 = location.state; | ||
var href = createHref(location); | ||
globalHistory.replaceState({ | ||
key: key, | ||
state: _state2 | ||
}, null, href); | ||
} else { | ||
warning(state === undefined, 'Browser history cannot push state in browsers that do not support HTML5 history, state is ignored'); | ||
if (hashChanged) { | ||
// We cannot tell if a hashchange was caused by a REPLACE, so we'd | ||
// rather setState here and ignore the hashchange. The caveat here | ||
// is that other hash histories in the page will consider it a POP. | ||
ignorePath = path; | ||
replaceHashPath(encodedPath); | ||
@@ -333,7 +339,8 @@ } | ||
listenerCount += delta; | ||
var eventType = canUseHistory ? PopStateEvent : HashChangeEvent; | ||
if (listenerCount === 1) { | ||
addEventListener(window, HashChangeEvent, handleHashChange); | ||
addEventListener(window, eventType, handleHashChange); | ||
} else if (listenerCount === 0) { | ||
removeEventListener(window, HashChangeEvent, handleHashChange); | ||
removeEventListener(window, eventType, handleHashChange); | ||
} | ||
@@ -340,0 +347,0 @@ }; |
{ | ||
"name": "history-extra", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"description": "Extra functionality for the history module", | ||
@@ -12,4 +12,4 @@ "main": "dist/createHashStateHistory.js", | ||
"build": "rollup -c rollup.build.js", | ||
"start": "node .", | ||
"test": "karma start karma.conf.js" | ||
"test": "karma start karma.conf.js", | ||
"coverage-report": "codecov" | ||
}, | ||
@@ -59,2 +59,3 @@ "repository": { | ||
"@w33ble/npm-auto-tools": "*", | ||
"codecov": "^3.1.0", | ||
"dotenv": "^6.1.0", | ||
@@ -73,2 +74,3 @@ "eslint": "^4.9.0", | ||
"karma-coverage-istanbul-reporter": "^2.0.4", | ||
"karma-firefox-launcher": "^1.1.0", | ||
"karma-rollup-preprocessor": "^6.1.0", | ||
@@ -75,0 +77,0 @@ "karma-sauce-launcher": "^1.2.0", |
@@ -7,2 +7,3 @@ # history-extra | ||
[](https://raw.githubusercontent.com/w33ble/history-extra/master/LICENSE) | ||
[](https://codecov.io/gh/w33ble/history-extra) | ||
[](https://www.npmjs.com/package/history-extra) | ||
@@ -23,3 +24,3 @@ [](https://nodejs.org/api/documentation.html#documentation_stability_index) | ||
So far, it's only been tested with `^4.7.0` and newer. | ||
So far, it's only been tested with `4.7.0` and newer. | ||
@@ -36,2 +37,24 @@ ## Methods | ||
## Development | ||
### Scripts | ||
There are several scripts available to check and test the code. The CI will run them too, but they're also helpful for running locally. All of these are launched with `npm run <script>` or `yarn run <script>`. | ||
script | description | ||
------ | ----------- | ||
lint | Runs linter on the code to catch syntax and other issues. | ||
build | Runs the build, producing the output in `dist`. | ||
test | Runs the tests in a local browser (Chrome and Firefox). | ||
### Environment Variables | ||
There are some ENV args that make things nice for development. | ||
arg | description | ||
--- | ----------- | ||
DEV | Boolean, useful for tests. Puts karma in watch mode and will re-run tests every time you save a change. | ||
TRAVIS | Boolean, used by the CI. Also useful for running your tests on Sauce Labs instead of just locally. | ||
KARMA_LAUNCHERS | Useful for controlling which launchers to use, as a comma-separated list. Local options are `Chrome` and `Firefox`. If `TRAVIS` is truthy, options are `SL_Explorer`, `SL_Chrome`, `SL_Firefox`, and `SL_Safari`. | ||
#### Thanks | ||
@@ -38,0 +61,0 @@ |
/* eslint no-use-before-define: 0 */ | ||
import locationUtils from 'history/LocationUtils'; | ||
import PathUtils from 'history/PathUtils'; | ||
import createTransitionManager from 'history/createTransitionManager'; | ||
import _createTransitionManager from 'history/createTransitionManager'; | ||
import DOMUtils from 'history/DOMUtils'; | ||
@@ -25,2 +25,7 @@ | ||
// goofy hack to handle cjs and esm differences in build | ||
const createTransitionManager = Object.hasOwnProperty.call(_createTransitionManager, 'default') | ||
? _createTransitionManager.default | ||
: _createTransitionManager; | ||
function warning(condition, message) { | ||
@@ -37,2 +42,3 @@ if (condition) return; | ||
const PopStateEvent = 'popstate'; | ||
const HashChangeEvent = 'hashchange'; | ||
@@ -217,15 +223,24 @@ | ||
const encodedPath = encodePath(basename + path); | ||
const hashChanged = getHashPath() !== encodedPath; | ||
if (hashChanged) { | ||
// We cannot tell if a hashchange was caused by a PUSH, so we'd | ||
// rather setState here and ignore the hashchange. The caveat here | ||
// is that other hash histories in the page will consider it a POP. | ||
if (canUseHistory) { | ||
// eslint-disable-next-line no-shadow | ||
const { key, state } = location; | ||
const href = createHref(location); | ||
globalHistory.pushState({ key, state }, null, href); | ||
} else { | ||
// legacy fallback | ||
ignorePath = path; | ||
if (canUseHistory) { | ||
// eslint-disable-next-line no-shadow | ||
const { key, state } = location; | ||
const href = createHref(location); | ||
globalHistory.pushState({ key, state }, null, href); | ||
const hashChanged = getHashPath() !== encodedPath; | ||
if (hashChanged) { | ||
// We cannot tell if a hashchange was caused by a PUSH, so we'd | ||
// rather setState here and ignore the hashchange. The caveat here | ||
// is that other hash histories in the page will consider it a POP. | ||
warning( | ||
false, | ||
'Hash history cannot PUSH the same path; a new entry will not be added to the history stack' | ||
); | ||
setState(); | ||
} else { | ||
@@ -236,20 +251,14 @@ warning( | ||
); | ||
pushHashPath(encodedPath); | ||
} | ||
const prevIndex = allPaths.lastIndexOf(createPath(history.location)); | ||
const nextPaths = allPaths.slice(0, prevIndex === -1 ? 0 : prevIndex + 1); | ||
pushHashPath(encodedPath); | ||
} | ||
nextPaths.push(path); | ||
allPaths = nextPaths; | ||
const prevIndex = allPaths.lastIndexOf(createPath(history.location)); | ||
const nextPaths = allPaths.slice(0, prevIndex === -1 ? 0 : prevIndex + 1); | ||
setState({ action, location }); | ||
} else { | ||
warning( | ||
false, | ||
'Hash history cannot PUSH the same path; a new entry will not be added to the history stack' | ||
); | ||
nextPaths.push(path); | ||
allPaths = nextPaths; | ||
setState(); | ||
} | ||
setState({ action, location }); | ||
}); | ||
@@ -270,20 +279,23 @@ }; | ||
const encodedPath = encodePath(basename + path); | ||
const hashChanged = getHashPath() !== encodedPath; | ||
if (hashChanged) { | ||
// We cannot tell if a hashchange was caused by a REPLACE, so we'd | ||
// rather setState here and ignore the hashchange. The caveat here | ||
// is that other hash histories in the page will consider it a POP. | ||
ignorePath = path; | ||
if (canUseHistory) { | ||
// eslint-disable-next-line no-shadow | ||
const { key, state } = location; | ||
const href = createHref(location); | ||
globalHistory.replaceState({ key, state }, null, href); | ||
} else { | ||
// legacy fallback | ||
if (canUseHistory) { | ||
// eslint-disable-next-line no-shadow | ||
const { key, state } = location; | ||
const href = createHref(location); | ||
globalHistory.replaceState({ key, state }, null, href); | ||
} else { | ||
warning( | ||
state === undefined, | ||
'Browser history cannot push state in browsers that do not support HTML5 history, state is ignored' | ||
); | ||
warning( | ||
state === undefined, | ||
'Browser history cannot push state in browsers that do not support HTML5 history, state is ignored' | ||
); | ||
const hashChanged = getHashPath() !== encodedPath; | ||
if (hashChanged) { | ||
// We cannot tell if a hashchange was caused by a REPLACE, so we'd | ||
// rather setState here and ignore the hashchange. The caveat here | ||
// is that other hash histories in the page will consider it a POP. | ||
ignorePath = path; | ||
replaceHashPath(encodedPath); | ||
@@ -315,7 +327,8 @@ } | ||
listenerCount += delta; | ||
const eventType = canUseHistory ? PopStateEvent : HashChangeEvent; | ||
if (listenerCount === 1) { | ||
addEventListener(window, HashChangeEvent, handleHashChange); | ||
addEventListener(window, eventType, handleHashChange); | ||
} else if (listenerCount === 0) { | ||
removeEventListener(window, HashChangeEvent, handleHashChange); | ||
removeEventListener(window, eventType, handleHashChange); | ||
} | ||
@@ -322,0 +335,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
89682
4.96%949
1.93%66
53.49%31
6.9%