fix(spa): resolve relative links and push the URL the page was actually served from (after redirects)

This commit is contained in:
saberzero1 committed 2026-11-07 22:50:48 +01:00
1 parent 5507de3365
commit 3e4a50a5ed
2 files changed
+15 -2

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
SPA navigation follows redirects correctly: when a link is redirected (for example `folder` → `folder/` on static hosts, or an alias page's canonical link), relative links on the new page resolve against where it really is, and the address bar shows that URL. Links on folder pages and parent tag pages reached through slash-less links pointed one level too high.
+10 -2
View File
@@ -68,8 +68,16 @@ async function _navigate(url: URL, isBack: boolean = false) {
isNavigating = true
startLoading()
p = p || new DOMParser()
// Where the page really came from: a redirect (e.g. `folder` → `folder/`, or an alias page's
// canonical link) changes the base its relative links resolve against, and the address bar
// should show it too. fetch() drops the #hash, so keep the requested one.
let finalUrl = url
const contents = await fetchCanonical(url)
.then((res) => {
if (res.url) {
finalUrl = new URL(res.url)
finalUrl.hash = url.hash
}
const contentType = res.headers.get("content-type")
if (contentType?.startsWith("text/html")) {
return res.text()
@@ -92,7 +100,7 @@ async function _navigate(url: URL, isBack: boolean = false) {
cleanupFns.clear()
const html = p.parseFromString(contents, "text/html")
normalizeRelativeURLs(html, url)
normalizeRelativeURLs(html, finalUrl)
let title = html.querySelector("title")?.textContent
if (title) {
@@ -129,7 +137,7 @@ async function _navigate(url: URL, isBack: boolean = false) {
// delay setting the url until now
// at this point everything is loaded so changing the url should resolve to the correct addresses
if (!isBack) {
history.pushState({}, "", url)
history.pushState({}, "", finalUrl)
}
notifyNav(getFullSlug(window))