mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-12 03:05:12 +00:00
46 lines
790 B
JavaScript
46 lines
790 B
JavaScript
window.addEventListener('click', event => {
|
|
let a = event.target;
|
|
while (a && a.nodeName !== 'A') a = a.parentNode;
|
|
if (!a) return;
|
|
|
|
if (navigate(new URL(a.href))) {
|
|
event.preventDefault();
|
|
history.pushState({}, '', a.href);
|
|
}
|
|
});
|
|
|
|
window.addEventListener('popstate', event => {
|
|
navigate(window.location);
|
|
});
|
|
|
|
const target = document.querySelector('__selector__');
|
|
let component;
|
|
|
|
function navigate(url) {
|
|
if (url.origin !== window.location.origin) return;
|
|
|
|
let match;
|
|
let params = {};
|
|
const query = {};
|
|
|
|
function render(mod) {
|
|
if (component) {
|
|
component.destroy();
|
|
} else {
|
|
target.innerHTML = '';
|
|
}
|
|
|
|
component = new mod.default({
|
|
target,
|
|
data: { query, params },
|
|
hydrate: !!component
|
|
});
|
|
}
|
|
|
|
// ROUTES
|
|
|
|
return true;
|
|
}
|
|
|
|
navigate(window.location);
|