Skip to content

Commit

Permalink
Fix home link
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSpaar committed Jan 28, 2024
1 parent 7481eeb commit 934102d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions static/markdown_spa.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
if (typeof preFetch !== 'function') { function preFetch() {} }
if (typeof postFetch !== 'function') { function postFetch() {} }

function overrideLinks() {
document.querySelectorAll('a').forEach(a => {
if (a.href.startsWith(window.location.origin) && !a.hasAttribute('onclick')) {
a.onclick = e => {
e.preventDefault();
updatePage(a.href);
};
}
});
}

async function updatePage(path) {
preFetch();

Expand All @@ -11,11 +22,15 @@ async function updatePage(path) {
document.documentElement.innerHTML = text;
}

window.history.pushState({}, '', path);
overrideLinks();
postFetch();

window.history.pushState({}, '', path);
}

postFetch();
overrideLinks();

let curPath = window.location.pathname;

window.addEventListener('popstate', async _ => {
Expand All @@ -26,12 +41,3 @@ window.addEventListener('popstate', async _ => {
curPath = window.location.pathname;
await updatePage(window.location.href);
});

window.addEventListener('click', e => {
if (e.target.tagName == 'A' && e.target.href.startsWith(window.location.origin)) {
e.preventDefault();

if (e.target.href != window.location.href)
updatePage(e.target.href);
}
});

0 comments on commit 934102d

Please sign in to comment.