add client-side preloading logic, move router into runtime module

This commit is contained in:
Rich Harris
2017-12-13 13:29:38 -05:00
parent c7e3fc4493
commit 941867f0a4
2 changed files with 43 additions and 27 deletions

25
runtime/router.js Normal file
View File

@@ -0,0 +1,25 @@
const router = {
init(callback) {
console.log('initing');
window.addEventListener('click', event => {
let a = event.target;
while (a && a.nodeName !== 'A') a = a.parentNode;
if (!a) return;
if (callback(new URL(a.href))) {
event.preventDefault();
history.pushState({}, '', a.href);
}
});
window.addEventListener('popstate', event => {
callback(window.location);
});
callback(window.location);
}
};
window.router = router;
export default router;