mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-14 20:14:39 +00:00
add client-side preloading logic, move router into runtime module
This commit is contained in:
25
runtime/router.js
Normal file
25
runtime/router.js
Normal 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;
|
||||
Reference in New Issue
Block a user