fix: active links outside partials not updated (#1852)
This commit is contained in:
+32
-14
@@ -1,26 +1,44 @@
|
||||
import { VNode } from "preact";
|
||||
import { DATA_ANCESTOR, DATA_CURRENT } from "../constants.ts";
|
||||
|
||||
export const enum UrlMatchKind {
|
||||
None,
|
||||
Ancestor,
|
||||
Current,
|
||||
}
|
||||
|
||||
export function matchesUrl(current: string, needle: string): UrlMatchKind {
|
||||
let href = new URL(needle, "http://localhost").pathname;
|
||||
if (href !== "/" && href.endsWith("/")) {
|
||||
href = href.slice(0, -1);
|
||||
}
|
||||
|
||||
if (current !== "/" && current.endsWith("/")) {
|
||||
current = current.slice(0, -1);
|
||||
}
|
||||
|
||||
if (current === href) {
|
||||
return UrlMatchKind.Current;
|
||||
} else if (current.startsWith(href + "/") || href === "/") {
|
||||
return UrlMatchKind.Ancestor;
|
||||
}
|
||||
|
||||
return UrlMatchKind.None;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark active or ancestor link
|
||||
* Note: This function is used both on the server and the client
|
||||
*/
|
||||
export function setActiveUrl(vnode: VNode, pathname: string) {
|
||||
export function setActiveUrl(vnode: VNode, pathname: string): void {
|
||||
const props = vnode.props as Record<string, unknown>;
|
||||
const hrefProp = props.href;
|
||||
if (typeof hrefProp === "string" && hrefProp.startsWith("/")) {
|
||||
let href = new URL(hrefProp, "http://localhost").pathname;
|
||||
if (href !== "/" && href.endsWith("/")) {
|
||||
href = href.slice(0, -1);
|
||||
}
|
||||
|
||||
if (pathname !== "/" && pathname.endsWith("/")) {
|
||||
pathname = pathname.slice(0, -1);
|
||||
}
|
||||
|
||||
if (pathname === href) {
|
||||
props["data-current"] = "true";
|
||||
} else if (pathname.startsWith(href + "/") || href === "/") {
|
||||
props["data-ancestor"] = "true";
|
||||
const match = matchesUrl(pathname, hrefProp);
|
||||
if (match === UrlMatchKind.Current) {
|
||||
props[DATA_CURRENT] = "true";
|
||||
} else if (match === UrlMatchKind.Ancestor) {
|
||||
props[DATA_ANCESTOR] = "true";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,13 +14,15 @@ import { type SerializedState } from "../../server/rendering/fresh_tags.tsx";
|
||||
import type { Signal } from "@preact/signals";
|
||||
import {
|
||||
CLIENT_NAV_ATTR,
|
||||
DATA_ANCESTOR,
|
||||
DATA_CURRENT,
|
||||
DATA_KEY_ATTR,
|
||||
LOADING_ATTR,
|
||||
PARTIAL_ATTR,
|
||||
PARTIAL_SEARCH_PARAM,
|
||||
PartialMode,
|
||||
} from "../../constants.ts";
|
||||
import { setActiveUrl } from "../active_url.ts";
|
||||
import { matchesUrl, setActiveUrl, UrlMatchKind } from "../active_url.ts";
|
||||
|
||||
function createRootFragment(
|
||||
parent: Element,
|
||||
@@ -553,10 +555,26 @@ function _walkInner(
|
||||
|
||||
const partialErrorMessage = `Unable to process partial response.`;
|
||||
|
||||
async function fetchPartials(url: URL, init?: RequestInit) {
|
||||
async function fetchPartials(url: URL, realUrl: URL, init?: RequestInit) {
|
||||
url.searchParams.set(PARTIAL_SEARCH_PARAM, "true");
|
||||
const res = await fetch(url, init);
|
||||
await applyPartials(res);
|
||||
|
||||
// Update links
|
||||
document.querySelectorAll("a").forEach((link) => {
|
||||
const match = matchesUrl(realUrl.pathname, link.href);
|
||||
|
||||
if (match === UrlMatchKind.Current) {
|
||||
link.setAttribute(DATA_CURRENT, "true");
|
||||
link.removeAttribute(DATA_ANCESTOR);
|
||||
} else if (match === UrlMatchKind.Ancestor) {
|
||||
link.setAttribute(DATA_ANCESTOR, "true");
|
||||
link.removeAttribute(DATA_CURRENT);
|
||||
} else {
|
||||
link.removeAttribute(DATA_CURRENT);
|
||||
link.removeAttribute(DATA_ANCESTOR);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -767,20 +785,21 @@ document.addEventListener("click", async (e) => {
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
const nextUrl = new URL(el.href);
|
||||
try {
|
||||
// Only add history entry when URL is new. Still apply
|
||||
// the partials because sometimes users click a link to
|
||||
// "refresh" the current page.
|
||||
if (el.href !== window.location.href) {
|
||||
index++;
|
||||
history.pushState({ index }, "", el.href);
|
||||
history.pushState({ index }, "", nextUrl.href);
|
||||
}
|
||||
|
||||
const partialUrl = new URL(
|
||||
partial ? partial : el.href,
|
||||
partial ? partial : nextUrl.href,
|
||||
location.origin,
|
||||
);
|
||||
await fetchPartials(partialUrl);
|
||||
await fetchPartials(partialUrl, nextUrl);
|
||||
} finally {
|
||||
if (indicator !== undefined) {
|
||||
indicator.value = false;
|
||||
@@ -804,7 +823,7 @@ addEventListener("popstate", async (e) => {
|
||||
// page navigation.
|
||||
if (partials.has("body")) {
|
||||
const url = new URL(location.href, location.origin);
|
||||
await fetchPartials(url);
|
||||
await fetchPartials(url, url);
|
||||
} else {
|
||||
location.reload();
|
||||
}
|
||||
@@ -819,7 +838,7 @@ document.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const url = new URL(partial, location.origin);
|
||||
await fetchPartials(url);
|
||||
await fetchPartials(url, new URL(location.href));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user