| | |
| | | }, 100) |
| | | } |
| | | |
| | | let isNavigating = false |
| | | let p: DOMParser |
| | | async function navigate(url: URL, isBack: boolean = false) { |
| | | async function _navigate(url: URL, isBack: boolean = false) { |
| | | isNavigating = true |
| | | startLoading() |
| | | p = p || new DOMParser() |
| | | const contents = await fetchCanonical(url) |
| | |
| | | delete announcer.dataset.persist |
| | | } |
| | | |
| | | async function navigate(url: URL, isBack: boolean = false) { |
| | | if (isNavigating) return |
| | | isNavigating = true |
| | | try { |
| | | await _navigate(url, isBack) |
| | | } catch (e) { |
| | | console.error(e) |
| | | window.location.assign(url) |
| | | } finally { |
| | | isNavigating = false |
| | | } |
| | | } |
| | | |
| | | window.spaNavigate = navigate |
| | | |
| | | function createRouter() { |
| | |
| | | return |
| | | } |
| | | |
| | | try { |
| | | navigate(url, false) |
| | | } catch (e) { |
| | | window.location.assign(url) |
| | | } |
| | | }) |
| | | |
| | | 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) { |
| | | window.location.reload() |
| | | } |
| | | return |
| | | }) |
| | | } |