fix(runtime): stabilize navigation and custom errors
Quality / quality (ubuntu-latest) (push) Failing after 23s
Quality / quality (windows-latest) (push) Canceled after 0s

This commit is contained in:
2026-08-15 10:44:06 +05:30
parent f0447fddb0
commit f88dd47408
14 changed files with 246 additions and 36 deletions
+57 -2
View File
@@ -672,6 +672,25 @@ export const HMR_CLIENT_JS = `
pendingSync = false;
var doc = new DOMParser().parseFromString(html, "text/html");
var i18nScript = Array.prototype.find.call(
doc.querySelectorAll("script:not([src])"),
function (node) { return /^window\.__wrnI18n=/.test(String(node.textContent || "").trim()); },
);
if (i18nScript) {
var i18nMatch = /^window\.__wrnI18n=([\s\S]*);\s*$/.exec(String(i18nScript.textContent || "").trim());
if (i18nMatch) {
try {
var incomingI18n = JSON.parse(i18nMatch[1]);
var existingI18n = window.__wrnI18n || {};
incomingI18n.t = existingI18n.t;
incomingI18n.set = existingI18n.set;
window.__wrnI18n = incomingI18n;
} catch (error) {
console.error("[wrnexus] failed to synchronize i18n HMR data", error);
}
}
}
if (doc.title) {
document.title = doc.title;
}
@@ -704,6 +723,10 @@ export const HMR_CLIENT_JS = `
window.__wrnexusHydrateCsrFetches(document);
}
if (window.__wrnLang && typeof window.__wrnLang.bind === "function") {
window.__wrnLang.bind(document);
}
if (
window.wrnTheme &&
typeof window.wrnTheme.bind === "function"
@@ -1449,7 +1472,23 @@ export function createHandlers(deps: RuntimeDeps): Handlers {
async function handleApi(ctx: Context): Promise<Response> {
const matched = router.matchApi(ctx.url.pathname);
if (!matched) return Response.json({ error: "Not Found" }, { status: 404 });
if (!matched) {
const fallback = router.matchApi("/api/404");
if (fallback && ctx.url.pathname !== "/api/404") {
const originalPath = ctx.url.pathname;
ctx.url.pathname = "/api/404";
try {
const response = await handleApi(ctx);
return new Response(response.body, {
status: 404,
headers: response.headers,
});
} finally {
ctx.url.pathname = originalPath;
}
}
return Response.json({ error: "Not Found" }, { status: 404 });
}
// Expose the canonical matched route to package dispatchers. A package may
// contribute several URL paths from one module, and request URLs can be
@@ -1606,7 +1645,23 @@ export function createHandlers(deps: RuntimeDeps): Handlers {
}),
);
}
const response = renderNotFound();
let response: Response;
const fallback = router.matchPage("/404");
if (fallback && ctx.url.pathname !== "/404") {
const originalPath = ctx.url.pathname;
ctx.url.pathname = "/404";
try {
const rendered = await handlePage(ctx);
response = new Response(rendered.body, {
status: 404,
headers: rendered.headers,
});
} finally {
ctx.url.pathname = originalPath;
}
} else {
response = renderNotFound();
}
if (!isMobileRequest) return response;
const headers = new Headers(response.headers);
headers.set("x-wrnexus-original-status", "404");