fix(runtime): stabilize navigation and custom errors
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user