summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-10-04 18:32:31 +0200
committerSamy Pesse <samypesse@gmail.com>2016-10-04 18:32:31 +0200
commit16ca48b2c05dad0af4cec600cee69adcafcf255f (patch)
tree63c1e117c0cf4da989652bd8236375fc9ba3a094 /packages/gitbook-core
parent87ae2481e4e6f4c6c0756ae379c8c806d95cbb68 (diff)
downloadgitbook-16ca48b2c05dad0af4cec600cee69adcafcf255f.zip
gitbook-16ca48b2c05dad0af4cec600cee69adcafcf255f.tar.gz
gitbook-16ca48b2c05dad0af4cec600cee69adcafcf255f.tar.bz2
Correctly update page when url changed
Diffstat (limited to 'packages/gitbook-core')
-rw-r--r--packages/gitbook-core/src/actions/TYPES.js1
-rw-r--r--packages/gitbook-core/src/actions/navigation.js41
-rw-r--r--packages/gitbook-core/src/components/PJAXWrapper.js16
-rw-r--r--packages/gitbook-core/src/models/Location.js5
-rw-r--r--packages/gitbook-core/src/models/Page.js22
-rw-r--r--packages/gitbook-core/src/reducers/navigation.js15
-rw-r--r--packages/gitbook-core/src/reducers/page.js22
7 files changed, 68 insertions, 54 deletions
diff --git a/packages/gitbook-core/src/actions/TYPES.js b/packages/gitbook-core/src/actions/TYPES.js
index 6dcab4f..f49b4bb 100644
--- a/packages/gitbook-core/src/actions/TYPES.js
+++ b/packages/gitbook-core/src/actions/TYPES.js
@@ -7,6 +7,7 @@ module.exports = {
NAVIGATION_ACTIVATE: 'navigation/activate',
NAVIGATION_DEACTIVATE: 'navigation/deactivate',
NAVIGATION_LISTEN: 'navigation/listen',
+ NAVIGATION_UPDATE: 'navigation/update',
PAGE_FETCH_START: 'navigation/fetch:start',
PAGE_FETCH_END: 'navigation/fetch:end',
PAGE_FETCH_ERROR: 'navigation/fetch:error',
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 });
}
);
diff --git a/packages/gitbook-core/src/components/PJAXWrapper.js b/packages/gitbook-core/src/components/PJAXWrapper.js
index f35a554..d2eb456 100644
--- a/packages/gitbook-core/src/components/PJAXWrapper.js
+++ b/packages/gitbook-core/src/components/PJAXWrapper.js
@@ -1,6 +1,6 @@
const React = require('react');
const ReactRedux = require('react-redux');
-const navigation = require('../actions/navigation');
+const Navigation = require('../actions/navigation');
/**
* Check if an element is inside a link
@@ -61,7 +61,7 @@ function getHrefForEvent(event) {
if (link.getAttribute('data-nopjax'))
return;
- return link.href;
+ return link.pathname;
}
/*
@@ -83,25 +83,15 @@ const PJAXWrapper = React.createClass({
}
event.preventDefault();
- dispatch(navigation.fetchPage(href));
-
- },
-
- onPopState(event) {
- const { dispatch } = this.props;
- event.preventDefault();
-
- dispatch(navigation.fetchPage(location.href, { replace: true }));
+ dispatch(Navigation.push(href));
},
componentDidMount() {
document.addEventListener('click', this.onClick, false);
- window.addEventListener('popstate', this.onPopState, false);
},
componentWillUnmount() {
document.removeEventListener('click', this.onClick, false);
- window.removeEventListener('popstate', this.onPopState, false);
},
render() {
diff --git a/packages/gitbook-core/src/models/Location.js b/packages/gitbook-core/src/models/Location.js
index 3b62bef..cdfea2d 100644
--- a/packages/gitbook-core/src/models/Location.js
+++ b/packages/gitbook-core/src/models/Location.js
@@ -10,6 +10,11 @@ const DEFAULTS = {
};
class Location extends Record(DEFAULTS) {
+
+ /**
+ * Return search query as a string
+ * @return {String}
+ */
get search() {
const { query } = this;
return query.size === 0 ?
diff --git a/packages/gitbook-core/src/models/Page.js b/packages/gitbook-core/src/models/Page.js
new file mode 100644
index 0000000..838191e
--- /dev/null
+++ b/packages/gitbook-core/src/models/Page.js
@@ -0,0 +1,22 @@
+const { Record } = require('immutable');
+
+const DEFAULTS = {
+ title: '',
+ content: '',
+ dir: 'ltr',
+ depth: 1,
+ level: '',
+ previous: null,
+ next: null
+};
+
+class Page extends Record(DEFAULTS) {
+ static create(state) {
+ return state instanceof Page ?
+ state : new Page({
+ ...state
+ });
+ }
+}
+
+module.exports = Page;
diff --git a/packages/gitbook-core/src/reducers/navigation.js b/packages/gitbook-core/src/reducers/navigation.js
index a6d71f8..11774de 100644
--- a/packages/gitbook-core/src/reducers/navigation.js
+++ b/packages/gitbook-core/src/reducers/navigation.js
@@ -5,16 +5,18 @@ const ACTION_TYPES = require('../actions/TYPES');
const isServerSide = (typeof window === 'undefined');
const NavigationState = Record({
+ // Current location
+ location: null,
// Are we loading a new page
- loading: Boolean(false),
+ loading: Boolean(false),
// Did we fail loading a page?
- error: null,
+ error: null,
// Listener for history changes
listeners: List(),
// Function to call to stop listening
- unlisten: null,
+ unlisten: null,
// History instance
- history: null
+ history: null
});
function reduceNavigation(state, action) {
@@ -58,6 +60,11 @@ function reduceNavigation(state, action) {
unlisten: null
});
+ case ACTION_TYPES.NAVIGATION_UPDATE:
+ return state.merge({
+ location: action.location
+ });
+
case ACTION_TYPES.NAVIGATION_LISTEN:
return state.merge({
listeners: state.listeners.push(action.listener)
diff --git a/packages/gitbook-core/src/reducers/page.js b/packages/gitbook-core/src/reducers/page.js
index 2d8c943..9b94d1e 100644
--- a/packages/gitbook-core/src/reducers/page.js
+++ b/packages/gitbook-core/src/reducers/page.js
@@ -1,26 +1,8 @@
-const { Record } = require('immutable');
const ACTION_TYPES = require('../actions/TYPES');
-
-const DEFAULTS = {
- title: '',
- content: '',
- dir: 'ltr',
- depth: 1,
- level: '',
- previous: null
-};
-
-class PageState extends Record(DEFAULTS) {
- static create(state) {
- return state instanceof PageState ?
- state : new PageState({
- ...state
- });
- }
-}
+const Page = require('../models/Page');
module.exports = (state, action) => {
- state = PageState.create(state);
+ state = Page.create(state);
switch (action.type) {