Skip to content

Commit

Permalink
ignore hash change (#7)
Browse files Browse the repository at this point in the history
* WIP

* update test, don't change history for hash change
  • Loading branch information
ryan-mahoney committed Oct 21, 2018
1 parent c9894e8 commit 5f504f1
Show file tree
Hide file tree
Showing 14 changed files with 1,555 additions and 2,094 deletions.
34 changes: 24 additions & 10 deletions dist/handleHistoryChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

// initilize a place-holder for the last request cancellation token
var requestCancellation = false;
var lastLocation = null;

exports.default = function (changePage) {
// handle server rendered case
Expand All @@ -49,11 +50,24 @@
// listen for changes to the current location
_history2.default.listen(function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(location, action) {
var uuid, path, CancelToken, response, data, previousScroll;
var check, uuid, path, CancelToken, response, data, previousScroll;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
// determine if location actually change, ignoring hash changes
check = "" + (location.state ? location.state + ":" : "") + location.pathname + (location.search ? "?" + location.search : "");

if (!(check === lastLocation && location.hash !== "")) {
_context.next = 3;
break;
}

return _context.abrupt("return");

case 3:
lastLocation = check;

// clear and start
_nprogress2.default.done();
_nprogress2.default.start();
Expand All @@ -70,7 +84,7 @@
requestCancellation.cancel("Override a previous request");
}
requestCancellation = CancelToken.source();
_context.next = 9;
_context.next = 13;
return _axios2.default.get(path, {
cancelToken: requestCancellation.token,
headers: {
Expand All @@ -80,7 +94,7 @@
return error.response || null;
});

case 9:
case 13:
response = _context.sent;


Expand All @@ -90,31 +104,31 @@
// if there was not response, do nothing

if (!(response === null)) {
_context.next = 13;
_context.next = 17;
break;
}

return _context.abrupt("return");

case 13:
case 17:
if (!(response.status[0] == 5)) {
_context.next = 16;
_context.next = 20;
break;
}

changePage((0, _assign2.default)({}, response.data, { location: "/500" }));
return _context.abrupt("return");

case 16:
case 20:
if (!(response.status == 404)) {
_context.next = 19;
_context.next = 23;
break;
}

changePage((0, _assign2.default)({}, response.data, { location: "/404" }));
return _context.abrupt("return");

case 19:
case 23:
data = (0, _assign2.default)({}, response.data.page, {
location: location.pathname
});
Expand Down Expand Up @@ -143,7 +157,7 @@
}
}

case 24:
case 28:
case "end":
return _context.stop();
}
Expand Down
2 changes: 1 addition & 1 deletion dist/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
handleSyncRegistered = true;
}

var _helper$match = _helper2.default.match(routes, props.page.location.split("?", 1)[0]),
var _helper$match = _helper2.default.match(routes, props.page && props.page.location ? props.page.location.split("?", 1)[0] : "/"),
Component = _helper$match.Component;

return _react2.default.createElement(Component, props);
Expand Down
16 changes: 7 additions & 9 deletions dist/scroll.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports", "babel-runtime/core-js/object/assign", "babel-runtime/core-js/json/stringify"], factory);
define(["exports", "babel-runtime/helpers/defineProperty", "babel-runtime/core-js/object/assign", "babel-runtime/core-js/json/stringify"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("babel-runtime/core-js/object/assign"), require("babel-runtime/core-js/json/stringify"));
factory(exports, require("babel-runtime/helpers/defineProperty"), require("babel-runtime/core-js/object/assign"), require("babel-runtime/core-js/json/stringify"));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.assign, global.stringify);
factory(mod.exports, global.defineProperty, global.assign, global.stringify);
global.scroll = mod.exports;
}
})(this, function (exports, _assign, _stringify) {
})(this, function (exports, _defineProperty2, _assign, _stringify) {
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getScrollFromSessionStorage = exports.setScrollToSessionStorage = exports.getScrollPosition = undefined;

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _assign2 = _interopRequireDefault(_assign);

var _stringify2 = _interopRequireDefault(_stringify);
Expand All @@ -36,11 +38,7 @@
};

var setScrollToSessionStorage = exports.setScrollToSessionStorage = function setScrollToSessionStorage() {
var path = window.location.pathname;
var data = (0, _stringify2.default)((0, _assign2.default)({}, getScrollFromSessionStorage("*") || {}, {
path: getScrollPosition()
}));
sessionStorage.setItem("scroll", data);
return sessionStorage.setItem("scroll", (0, _stringify2.default)((0, _assign2.default)({}, getScrollFromSessionStorage("*") || {}, (0, _defineProperty3.default)({}, window.location.pathname, getScrollPosition()))));
};

var getScrollFromSessionStorage = exports.getScrollFromSessionStorage = function getScrollFromSessionStorage(url) {
Expand Down
40 changes: 20 additions & 20 deletions example/components/Home.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import React from 'react';
import { Link } from './../../src/router.js';
import React from "react";
import { Link } from "./../../src/router.js";

const Home = (props) => {
return (
const Home = props => (
<div>
<h1>Home</h1>
<div>
<h1>Home</h1>
<div>Render Mode: {props.page.mode}</div>
<div>
<Link to="/about">About</Link>
</div>
<div>
<Link to="/x">Unknown</Link>
</div>
<div>
<Link to="/bad">Error</Link>
</div>
<div>
<Link to="/needauth">Requires Authorization</Link>
</div>
Render Mode: {props.page && props.page.mode ? props.page.mode : "?"}
</div>
)
};
<div>
<Link to="/about">About</Link>
</div>
<div>
<Link to="/x">Unknown</Link>
</div>
<div>
<Link to="/bad">Error</Link>
</div>
<div>
<Link to="/needauth">Requires Authorization</Link>
</div>
</div>
);

export default Home;
1 change: 1 addition & 0 deletions example/constants/action-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CHANGE_PAGE = "CHANGE_PAGE";
69 changes: 34 additions & 35 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
{
"name": "example",
"version": "1.0.0",
"description": "example",
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-loader": "^7.1.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.24.1",
"css-loader": "^0.28.4",
"json-loader": "^0.5.4",
"extract-text-webpack-plugin": "3.0.0",
"postcss-loader": "^2.0.6",
"postcss-nested": "^2.0.3",
"style-loader": "^0.18.2",
"webpack": "^3.2.0",
"body-parser": "^1.17.1",
"cookie-parser": "^1.4.3",
"express": "^4.15.2"
},
"dependencies": {
"path-to-regexp": "^1.7.0",
"uuid": "^3.1.0",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"redux": "^3.6.0",
"react-redux": "^5.0.2",
"redux-thunk": "^2.2.0",
"axios": "^0.16.2",
"nprogress": "^0.2.0",
"history": "^4.5.1"
}
"name": "example",
"version": "1.0.0",
"description": "example",
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-loader": "^7.1.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.24.1",
"css-loader": "^0.28.4",
"json-loader": "^0.5.4",
"extract-text-webpack-plugin": "3.0.0",
"postcss-loader": "^2.0.6",
"postcss-nested": "^2.0.3",
"style-loader": "^0.18.2",
"webpack": "^3.2.0",
"body-parser": "^1.17.1",
"cookie-parser": "^1.4.3",
"express": "^4.15.2"
},
"dependencies": {
"path-to-regexp": "^1.7.0",
"uuid": "^3.1.0",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"redux": "^3.6.0",
"react-redux": "^5.0.2",
"axios": "^0.16.2",
"nprogress": "^0.2.0",
"history": "^4.5.1"
}
}
20 changes: 20 additions & 0 deletions example/reducers/page-reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CHANGE_PAGE } from "./../constants/action-types";

export const reducer = (
currentState = {
location: "/",
error: null,
sortState: {},
modalKey: {}
},
action
) => {
let update = {};
switch (action.type) {
case CHANGE_PAGE:
return Object.assign({}, action.data);

default:
return currentState;
}
};
11 changes: 4 additions & 7 deletions example/store/configureStore.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { createStore, combineReducers } from 'redux';
import { reducer } from './../../src/reducer.js';
import { createStore, combineReducers } from "redux";
import { reducer } from "./../reducers/page-reducer";

const rootReducer = combineReducers({
page: reducer
});

const configureStore = (preloadedState) => {
const store = createStore(
rootReducer,
preloadedState
);
const configureStore = preloadedState => {
const store = createStore(rootReducer, preloadedState);

return store;
};
Expand Down
Loading

0 comments on commit 5f504f1

Please sign in to comment.