@ray-js/router
Version:
Ray Core
50 lines • 1.25 kB
JavaScript
import { createBrowserHistory } from 'history';
import React from 'react';
export let history;
let changeKey = '';
export function createHistory(options) {
const {
scheduler
} = options;
history = createBrowserHistory(options);
history.listen(async historyLocation => {
const {
key,
pathname
} = historyLocation;
if (key && key === changeKey) {
return;
}
changeKey = key;
let matchedPage = scheduler.getMatchedPage(pathname);
if (!matchedPage) {
// 兜底通配页面
matchedPage = scheduler.getMatchedPage('*');
}
if (matchedPage) {
const page = await matchedPage.component();
options.onChange(page, {
page: matchedPage
});
} else {
console.warn('Not match any route!');
// 兜底 404 页面
const NotFound = function () {
return /*#__PURE__*/React.createElement('div', null, 'Page Not Found');
};
options.onChange(NotFound, {
page: {
pathname: pathname,
route: '*',
path: '*',
config: {},
params: {},
component: () => {
return Promise.resolve(NotFound);
}
}
});
}
});
return history;
}