Show the language picker on every page, targeting the nearest page that answers GET
CI / full-gate (push) Successful in 2m39s
CI / full-gate (push) Successful in 2m39s
This commit is contained in:
@@ -1503,3 +1503,33 @@ test("an error page renders without composing the menu — it exists for when th
|
||||
assert.match(await res.text(), /Page not found/);
|
||||
assert.equal(built, 0);
|
||||
});
|
||||
|
||||
test("a POST-rendered page still offers the language picker, pointed at a page that answers GET", async (t) => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "pp-post-lang-"));
|
||||
mkdirSync(join(dir, "demo", "views"), { recursive: true });
|
||||
t.after(() => rmSync(dir, { force: true, recursive: true }));
|
||||
// The view renders the picker exactly as the shell does.
|
||||
writeFileSync(join(dir, "demo", "views", "page.ejs"), `<%- include("partials/locale-switch") %>`);
|
||||
const demo: Plugin = {
|
||||
apiVersion: "1.0.0",
|
||||
id: "demo",
|
||||
routes: [
|
||||
{ handler: () => ({ view: "page" }), method: "GET", path: "/thing" },
|
||||
{ handler: () => ({ view: "page" }), method: "POST", path: "/thing" },
|
||||
{ handler: () => ({ view: "page" }), method: "POST", path: "/thing/act" }, // POST-only: no GET sibling
|
||||
],
|
||||
};
|
||||
const app = createApp({ i18n: createI18n(await loadI18n()), plugins: [demo], pluginsDir: dir });
|
||||
await new Promise<void>((r) => app.listen(0, r));
|
||||
t.after(() => app.close());
|
||||
const url = `http://localhost:${(app.address() as AddressInfo).port}`;
|
||||
const post = (path: string, headers: Record<string, string> = {}) => fetch(url + path, { headers, method: "POST" });
|
||||
|
||||
// A POST whose path also answers GET → the picker points at that page.
|
||||
assert.match(await (await post("/demo/thing?locale=sv-SE")).text(), /href="\/demo\/thing\?locale=en-US"/);
|
||||
// A POST-only path → the page the form was submitted from, so the link can't dead-end on a 405.
|
||||
const fromForm = await post("/demo/thing/act?locale=sv-SE", { referer: `${url}/demo/thing?locale=sv-SE` });
|
||||
assert.match(await fromForm.text(), /href="\/demo\/thing\?locale=en-US"/);
|
||||
// …and with no referer to fall back on, the front page.
|
||||
assert.match(await (await post("/demo/thing/act?locale=sv-SE")).text(), /href="\/\?locale=en-US"/);
|
||||
});
|
||||
|
||||
+28
-1
@@ -30,6 +30,7 @@ import type { SystemCapabilities } from "../plugin-host/system.ts";
|
||||
import { allowedMethods, isAuthorized, matchRoute } from "../plugin-host/router.ts";
|
||||
import { buildAuthRoutes } from "../auth/routes.ts";
|
||||
import { securityHeaders } from "./security-headers.ts";
|
||||
import { localPath } from "./safe-url.ts";
|
||||
import { routePublic, serveStatic } from "./static.ts";
|
||||
import { renderPluginView } from "../plugin-host/view-resolver.ts";
|
||||
|
||||
@@ -120,6 +121,18 @@ export function createApp(options: AppOptions = {}): Server {
|
||||
// it. A plugin's context carries that plugin's translator, so its own catalog wins in its own views.
|
||||
// They are merged LAST: these names are reserved (README → Building plugins), and a handler that
|
||||
// happens to use one loses that key rather than breaking the shell that renders around it.
|
||||
// Where the language picker on this page should point. Normally the page itself; after a POST
|
||||
// that URL may answer no GET (POST /admin/users/:id/delete has no GET sibling), so fall back to
|
||||
// the page the form was submitted from, then to the front page — the picker is on every page, so
|
||||
// every one of its links has to land somewhere real.
|
||||
const switchBase = (req: IncomingMessage, url: URL): string => {
|
||||
const method = (req.method ?? "GET").toUpperCase();
|
||||
if (method === "GET" || method === "HEAD") return `${url.pathname}${url.search}`;
|
||||
const answersGet = matchRoute(plugins, "GET", url.pathname) !== null
|
||||
|| matchBuiltinRoute(builtinRoutes, "GET", url.pathname) !== undefined;
|
||||
return answersGet ? url.pathname : (sameOriginPath(req) ?? "/");
|
||||
};
|
||||
|
||||
// Named field by field on purpose: spreading the context would trigger its lazy `chrome` getter,
|
||||
// composing the menu for every render — including the standalone error pages, which exist to
|
||||
// render when the shell's own data is what failed.
|
||||
@@ -127,7 +140,7 @@ export function createApp(options: AppOptions = {}): Server {
|
||||
locale: ctx.locale,
|
||||
localeHref: ctx.localeHref,
|
||||
locales: ctx.locales,
|
||||
method: ctx.req.method ?? "GET",
|
||||
switchBase: switchBase(ctx.req, ctx.url),
|
||||
t: ctx.t,
|
||||
url: ctx.url,
|
||||
});
|
||||
@@ -403,6 +416,20 @@ export function createApp(options: AppOptions = {}): Server {
|
||||
});
|
||||
}
|
||||
|
||||
// The Referer as a host-relative path, when it is one of ours — the page a form was submitted
|
||||
// from. Anything off-origin or malformed is discarded rather than trusted into a link.
|
||||
function sameOriginPath(req: IncomingMessage): string | null {
|
||||
const referer = req.headers.referer;
|
||||
if (typeof referer !== "string") return null;
|
||||
try {
|
||||
const url = new URL(referer);
|
||||
if (req.headers.host !== undefined && url.host !== req.headers.host) return null;
|
||||
return localPath(`${url.pathname}${url.search}`);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
type ViewRenderer = (view: string, data: Record<string, unknown>) => Promise<string>;
|
||||
|
||||
// Turn a handler's RouteResult into the HTTP response. `null` = the handler took over `ctx.res`
|
||||
|
||||
@@ -8,7 +8,7 @@ const request = (overrides: Partial<I18nRequest> = {}): I18nRequest => ({
|
||||
locale: "sv-SE",
|
||||
localeHref: (href) => href,
|
||||
locales: ["en-US", "sv-SE"],
|
||||
method: "GET",
|
||||
switchBase: "/admin/users?q=ada",
|
||||
t: ENGLISH,
|
||||
url: new URL("http://localhost/admin/users?q=ada"),
|
||||
...overrides,
|
||||
@@ -32,8 +32,9 @@ test("dir follows the locale's script", () => {
|
||||
assert.equal(i18nLocals(request({ locale: "ar-EG" })).dir, "rtl");
|
||||
});
|
||||
|
||||
test("a page rendered from a POST offers no language links — that URL may have no GET at all", () => {
|
||||
// Following one would dead-end on a 405 (a POST-only route), or silently discard a re-rendered
|
||||
// form's input. The picker renders nothing below two choices, so an empty list hides it.
|
||||
assert.deepEqual(i18nLocals(request({ method: "POST" })).localeSwitch, []);
|
||||
test("the picker points wherever the host says — after a POST that is the nearest page answering GET", () => {
|
||||
// The picker is on every page; on a POST-rendered one its own URL may answer no GET, so the host
|
||||
// resolves the target (app.ts → switchBase) and this just renders it.
|
||||
const locals = i18nLocals(request({ switchBase: "/admin/users/u1" }));
|
||||
assert.deepEqual(locals.localeSwitch.map((c) => c.href), ["/admin/users/u1?locale=en-US", "/admin/users/u1?locale=sv-SE"]);
|
||||
});
|
||||
|
||||
@@ -32,7 +32,9 @@ export interface I18nRequest {
|
||||
locale: string;
|
||||
localeHref: (href: string) => string;
|
||||
locales: string[];
|
||||
method: string; // a page rendered in response to a POST has no linkable URL — see localeSwitch
|
||||
// Where the language picker points — "this page", except after a POST, whose URL may answer no
|
||||
// GET at all; the host then resolves the nearest page that does (app.ts → switchBase).
|
||||
switchBase: string;
|
||||
t: Translate;
|
||||
url: URL;
|
||||
}
|
||||
@@ -50,11 +52,6 @@ export const ENGLISH_LOCALS: I18nLocals = {
|
||||
};
|
||||
|
||||
export function i18nLocals(ctx: I18nRequest): I18nLocals {
|
||||
const here = `${ctx.url.pathname}${ctx.url.search}`;
|
||||
// The picker links to this same page in another language. After a POST that page's URL often has
|
||||
// no GET at all (the admin's recovery-code screen, say), so linking there would dead-end on a 405
|
||||
// — and on a re-rendered form it would silently discard what the user typed. Offer nothing.
|
||||
const linkable = ctx.method === "GET" || ctx.method === "HEAD";
|
||||
// ctx.localeHref is a no-op unless the URL asked for a locale, so it is also the honest answer to
|
||||
// "did it?" — asking the function that decides keeps the two from drifting apart.
|
||||
const carried = ctx.localeHref("/") === "/" ? null : ctx.locale;
|
||||
@@ -63,7 +60,7 @@ export function i18nLocals(ctx: I18nRequest): I18nLocals {
|
||||
locale: ctx.locale,
|
||||
localeHref: (href) => ctx.localeHref(href),
|
||||
localeParam: carried,
|
||||
localeSwitch: linkable ? ctx.locales.map((tag) => ({ current: tag === ctx.locale, href: localeHref(here, tag), label: localeLabel(tag), tag })) : [],
|
||||
localeSwitch: ctx.locales.map((tag) => ({ current: tag === ctx.locale, href: localeHref(ctx.switchBase, tag), label: localeLabel(tag), tag })),
|
||||
locales: ctx.locales,
|
||||
t: ctx.t,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user