fix: active links outside partials not updated (#1852)
This commit is contained in:
@@ -3,6 +3,8 @@ export const PARTIAL_ATTR = "f-partial";
|
||||
export const LOADING_ATTR = "f-loading";
|
||||
export const CLIENT_NAV_ATTR = "f-client-nav";
|
||||
export const DATA_KEY_ATTR = "data-fresh-key";
|
||||
export const DATA_CURRENT = "data-current";
|
||||
export const DATA_ANCESTOR = "data-ancestor";
|
||||
|
||||
export const enum PartialMode {
|
||||
REPLACE,
|
||||
|
||||
+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));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -14,7 +14,12 @@ import { Partial, PartialProps } from "../../runtime/Partial.tsx";
|
||||
import { renderToString } from "preact-render-to-string";
|
||||
import { RenderState } from "./state.ts";
|
||||
import { Island } from "../types.ts";
|
||||
import { DATA_KEY_ATTR, LOADING_ATTR, PartialMode } from "../../constants.ts";
|
||||
import {
|
||||
CLIENT_NAV_ATTR,
|
||||
DATA_KEY_ATTR,
|
||||
LOADING_ATTR,
|
||||
PartialMode,
|
||||
} from "../../constants.ts";
|
||||
import { setActiveUrl } from "../../runtime/active_url.ts";
|
||||
|
||||
// See: https://github.com/preactjs/preact/blob/7748dcb83cedd02e37b3713634e35b97b26028fd/src/internal.d.ts#L3C1-L16
|
||||
@@ -192,10 +197,16 @@ options.vnode = (vnode) => {
|
||||
vnode.type !== "style" && vnode.type !== "script" && vnode.type !== "link"
|
||||
) {
|
||||
props[DATA_KEY_ATTR] = vnode.key;
|
||||
} else if (props[LOADING_ATTR]) {
|
||||
}
|
||||
|
||||
if (props[LOADING_ATTR]) {
|
||||
// Avoid automatic signals unwrapping
|
||||
props[LOADING_ATTR] = { value: props[LOADING_ATTR] };
|
||||
}
|
||||
|
||||
if (typeof props[CLIENT_NAV_ATTR] === "boolean") {
|
||||
props[CLIENT_NAV_ATTR] = props[CLIENT_NAV_ATTR] ? "true" : null;
|
||||
}
|
||||
} else if (
|
||||
current && typeof vnode.type === "function" && vnode.type !== Fragment &&
|
||||
ownerStack.length > 0
|
||||
|
||||
Reference in New Issue
Block a user