From 0998bc355e6425e6b2bdf3d2da7124aa7b63b2a2 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Thu, 17 Aug 2023 08:58:11 +0000
Subject: [PATCH] fix rebuild debouncing

---
 quartz/components/scripts/spa.inline.ts |   24 +++++++++++++++++-------
 1 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/quartz/components/scripts/spa.inline.ts b/quartz/components/scripts/spa.inline.ts
index 4d31b5e..e0a8373 100644
--- a/quartz/components/scripts/spa.inline.ts
+++ b/quartz/components/scripts/spa.inline.ts
@@ -1,5 +1,5 @@
 import micromorph from "micromorph"
-import { CanonicalSlug, RelativeURL, getCanonicalSlug } from "../../path"
+import { CanonicalSlug, RelativeURL, getCanonicalSlug } from "../../util/path"
 
 // adapted from `micromorph`
 // https://github.com/natemoo-re/micromorph
@@ -46,10 +46,7 @@
     })
 
   if (!contents) return
-  if (!isBack) {
-    history.pushState({}, "", url)
-    window.scrollTo({ top: 0 })
-  }
+
   const html = p.parseFromString(contents, "text/html")
   let title = html.querySelector("title")?.textContent
   if (title) {
@@ -64,8 +61,20 @@
   announcer.dataset.persist = ""
   html.body.appendChild(announcer)
 
+  // morph body
   micromorph(document.body, html.body)
 
+  // scroll into place and add history
+  if (!isBack) {
+    history.pushState({}, "", url)
+    if (url.hash) {
+      const el = document.getElementById(url.hash.substring(1))
+      el?.scrollIntoView()
+    } else {
+      window.scrollTo({ top: 0 })
+    }
+  }
+
   // now, patch head
   const elementsToRemove = document.head.querySelectorAll(":not([spa-preserve])")
   elementsToRemove.forEach((el) => el.remove())
@@ -91,8 +100,9 @@
       }
     })
 
-    window.addEventListener("popstate", () => {
-      if (window.location.hash) return
+    window.addEventListener("popstate", (event) => {
+      const { url } = getOpts(event) ?? {}
+      if (window.location.hash && window.location.pathname === url?.pathname) return
       try {
         navigate(new URL(window.location.toString()), true)
       } catch (e) {

--
Gitblit v1.10.0