diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-04 18:32:31 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-04 18:32:31 +0200 |
commit | 16ca48b2c05dad0af4cec600cee69adcafcf255f (patch) | |
tree | 63c1e117c0cf4da989652bd8236375fc9ba3a094 /packages/gitbook-core/src/actions/navigation.js | |
parent | 87ae2481e4e6f4c6c0756ae379c8c806d95cbb68 (diff) | |
download | gitbook-16ca48b2c05dad0af4cec600cee69adcafcf255f.zip gitbook-16ca48b2c05dad0af4cec600cee69adcafcf255f.tar.gz gitbook-16ca48b2c05dad0af4cec600cee69adcafcf255f.tar.bz2 |
Correctly update page when url changed
Diffstat (limited to 'packages/gitbook-core/src/actions/navigation.js')
-rw-r--r-- | packages/gitbook-core/src/actions/navigation.js | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/packages/gitbook-core/src/actions/navigation.js b/packages/gitbook-core/src/actions/navigation.js index bcb4619..26bfa59 100644 --- a/packages/gitbook-core/src/actions/navigation.js +++ b/packages/gitbook-core/src/actions/navigation.js @@ -14,9 +14,27 @@ const SUPPORTED = ( */ function activate() { return (dispatch, getState) => { + dispatch({ type: ACTION_TYPES.NAVIGATION_ACTIVATE, - listener: () => dispatch(emit()) + listener: (location) => { + location = Location.fromNative(location); + const prevLocation = getState().navigation.location; + + // Fetch page if required + if (!prevLocation || location.pathname !== prevLocation.pathname) { + dispatch(fetchPage(location.pathname)); + } + + // Signal location to listener + dispatch(emit()); + + // Update the location + dispatch({ + type: ACTION_TYPES.NAVIGATION_UPDATE, + location + }); + } }); // Trigger for existing listeners @@ -116,24 +134,14 @@ function listen(listener) { /** * Fetch a new page and update the store accordingly - * @param {String} uri - * @param {Boolean} options.replace + * @param {String} pathname * @return {Action} action */ -function fetchPage(uri, options = {}) { - const shouldReplace = options.replace; - +function fetchPage(pathname) { return (dispatch, getState) => { - const prevURI = location.href; dispatch({ type: ACTION_TYPES.PAGE_FETCH_START }); - if (shouldReplace) { - dispatch(replace(uri)); - } else { - dispatch(push(uri)); - } - - window.fetch(uri, { + window.fetch(pathname, { credentials: 'include' }) .then( @@ -154,9 +162,8 @@ function fetchPage(uri, options = {}) { ) .catch( error => { - dispatch(replace(prevURI)); - dispatch(redirect(uri)); - + // dispatch(redirect(pathname)); + console.error(error); dispatch({ type: ACTION_TYPES.PAGE_FETCH_ERROR, error }); } ); |