redirects to / implemented when path not matching or is invalid

This commit is contained in:
NikolaBorislavovHristov 2018-07-02 12:47:50 +03:00
parent 114e8c3fdf
commit ffec89d3cc

View file

@ -31,10 +31,17 @@ class Router extends Component {
const hashIndex = window.location.href.indexOf('#');
const hashPath = hashIndex === -1 ? '' : window.location.href.substring(hashIndex + 1);
const path = joinPaths('/', hashPath);
if (hashPath !== path) {
window.location.replace(`#${path}`);
return;
}
let pathMatched = false;
for (let viewConfigIndex = 0; viewConfigIndex < this.props.config.views.length; viewConfigIndex++) {
const viewConfig = this.props.config.views[viewConfigIndex];
for (const routeConfig of viewConfig.routes) {
if (matchPath(path, routeConfig)) {
pathMatched = true;
this.setState(({ views }) => ({
views: views.map((view, viewIndex) => {
if (viewIndex > viewConfigIndex) {
@ -56,6 +63,10 @@ class Router extends Component {
}
}
}
if (!pathMatched) {
window.location.replace('#/');
}
}
render() {