fix(spa): keep the query string when rebasing relative URLs

This commit is contained in:
saberzero1 committed 2026-11-27 16:10:11 +01:00
1 parent 45ed91c90c
commit 6729a1e7bb
3 files changed
+16 -15

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
Relative links and embeds keep their query string after SPA navigation and in popovers (`./viewer.html?file=a.pdf` no longer loses `?file=a.pdf`).
+10 -14
View File
@@ -486,7 +486,8 @@ describe("normalizeRelativeURLs (SPA navigation and popovers)", () => {
[
{ href: "/notes/deep/sibling" },
{ href: "/notes/up/page#part" },
{ href: "/notes/deep/page" },
// an empty href is the page itself, its query string included (as in the browser)
{ href: "/notes/deep/page?q=1" },
{ src: "/notes/deep/img.png" },
{ src: "/notes/media/v.mp4" },
{ href: "https://example.com/x" },
@@ -497,17 +498,12 @@ describe("normalizeRelativeURLs (SPA navigation and popovers)", () => {
)
})
// _rebaseHtmlElement keeps `pathname + hash` of the resolved URL and drops its `search`, so a
// relative link or embed with a query string loses it after SPA navigation or in a popover
// (`./static/viewer.html?file=a.pdf` → `/notes/static/viewer.html`), while the page loaded
// directly keeps it.
test(
"keeps the query string of a relative URL",
{ todo: "bug: normalizeRelativeURLs drops ?query (quartz/util/path.ts:60)" },
() => {
const { page, els } = fakePage([{ src: "./viewer.html?file=a.pdf#p2" }])
path.normalizeRelativeURLs(page, "https://garden.example/notes/page")
assert.deepStrictEqual(els[0].attrs, { src: "/notes/viewer.html?file=a.pdf#p2" })
},
)
// _rebaseHtmlElement used to keep only `pathname + hash` of the resolved URL, so a relative
// link or embed with a query string lost it after SPA navigation or in a popover
// (`./static/viewer.html?file=a.pdf` → `/notes/static/viewer.html`).
test("keeps the query string of a relative URL", () => {
const { page, els } = fakePage([{ src: "./viewer.html?file=a.pdf#p2" }])
path.normalizeRelativeURLs(page, "https://garden.example/notes/page")
assert.deepStrictEqual(els[0].attrs, { src: "/notes/viewer.html?file=a.pdf#p2" })
})
})
+1 -1
View File
@@ -57,7 +57,7 @@ export const QUARTZ = "quartz"
// https://github.com/natemoo-re/micromorph/blob/main/src/utils.ts#L5
const _rebaseHtmlElement = (el: Element, attr: string, newBase: string | URL) => {
const rebased = new URL(el.getAttribute(attr)!, newBase)
el.setAttribute(attr, rebased.pathname + rebased.hash)
el.setAttribute(attr, rebased.pathname + rebased.search + rebased.hash)
}
export function normalizeRelativeURLs(el: Element | Document, destination: string | URL) {
el.querySelectorAll('[href=""], [href^="./"], [href^="../"]').forEach((item) => {