Files
reverse-engineering-anything/website/worker.mjs
T
188e2bd684 fix(website): honor validators and restore the implicit design system (#14768)
* fix(website): honor validators and restore the implicit design system

- Route every worker answer that skips Markdown negotiation through the HTML
  fallback, so validators and ranges removed for the probe still apply.
- Replace undefined classes with existing components and remove 27 CSS
  classes no page uses.
- Express article text color as a token so component colors win in both
  themes, and use logical properties for text-flow layout in RTL pages.
- Give an untranslated English fallback notice English direction.
- Start the page navigation closed without JavaScript, dismiss it with Escape
  or an outside click, and keep later copy clicks, failed key imports and
  first animation frames from corrupting demo state.
- Sync the theme bootstrap into the Notes fixture through a declared region.

* fix(website): skip Markdown negotiation for requests with a body

Markdown probing takes ownership of the caller request body. Methods other
than GET and HEAD have no Markdown representation, so they reach the asset
service once with the original body and validators.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(website): describe the homepage menu's closed start

The disclosure has no open attribute, so without JavaScript it starts
closed and the summary reveals the fragment links. The guide now matches
that, including overlay dismissal.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* style(website): wrap the worker precondition assertion

oxfmt's 80-column width is what Static checks enforce. The If-Match
comparison in the non-retrieval test exceeded it.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-26 17:04:00 +00:00

124 lines
4.3 KiB
JavaScript

// Negotiate prebuilt Markdown while keeping the static asset service's routing.
function prefersMarkdown(accept) {
const ranges = (accept ?? "")
.toLowerCase()
.split(",")
.map((range) => {
const [type, ...parameters] = range.trim().split(";");
const quality = parameters
.map((value) => value.trim())
.find((value) => value.startsWith("q="));
const q = quality === undefined ? 1 : Number(quality.slice(2));
return {
type: type.trim(),
q: Number.isFinite(q) && q >= 0 && q <= 1 ? q : 0,
};
});
const quality = (type) => {
for (const match of [type, "text/*", "*/*"]) {
const matches = ranges.filter((range) => range.type === match);
if (matches.length) return Math.max(...matches.map((range) => range.q));
}
return 0;
};
// Wildcards alone never opt a browser into Markdown; HTML wins ties.
return (
ranges.some((range) => range.type === "text/markdown") &&
quality("text/markdown") > 0 &&
quality("text/markdown") > quality("text/html")
);
}
function advertiseMarkdown(response, path) {
const vary = new Set(
(response.headers.get("Vary") ?? "")
.split(",")
.map((s) => s.trim())
.filter(Boolean),
);
if (![...vary].some((s) => s.toLowerCase() === "accept" || s === "*"))
vary.add("Accept");
response.headers.set("Vary", [...vary].join(", "));
response.headers.append(
"Link",
`<${path}>; rel="alternate"; type="text/markdown"`,
);
return response;
}
/**
* Answer without Markdown: the route is absent or the response is not
* negotiable HTML.
*
* Reevaluate HTML only when probing removed conditional or range headers.
* Otherwise the existing response already answers the caller's request.
*/
const htmlFallback = async (original, request, probeChanged, env) => {
if (!probeChanged) return original;
await original.body?.cancel();
return env.ASSETS.fetch(request);
};
export default {
async fetch(request, env) {
// Markdown exists only as a GET/HEAD representation. A probe request takes
// ownership of a body, so any other method must reach the asset service
// on the caller's own request, validators included.
if (!["GET", "HEAD"].includes(request.method))
return env.ASSETS.fetch(request);
const useMarkdown = prefersMarkdown(request.headers.get("Accept"));
// HTML validators describe a different representation. Probe the route first,
// then let the asset service evaluate validators/ranges against the Markdown.
const probe = new Request(request);
let probeChanged = false;
if (useMarkdown) {
for (const name of [
"If-None-Match",
"If-Modified-Since",
"If-Match",
"If-Unmodified-Since",
"If-Range",
"Range",
]) {
probeChanged ||= probe.headers.has(name);
probe.headers.delete(name);
}
}
const original = await env.ASSETS.fetch(probe);
// Every answer that does not negotiate Markdown must honor the caller's
// own validators and ranges, which the probe may have removed.
const url = new URL(request.url);
if (
![200, 206, 304].includes(original.status) ||
!original.headers.get("Content-Type")?.startsWith("text/html") ||
(!url.pathname.endsWith("/") && !url.pathname.endsWith(".html"))
)
return htmlFallback(original, request, probeChanged, env);
const markdown = new URL(url);
markdown.pathname = url.pathname.endsWith("/")
? `${url.pathname}index.md`
: url.pathname.slice(0, -5) + ".md";
const candidate = await env.ASSETS.fetch(
useMarkdown
? new Request(markdown, request)
: new Request(markdown, { method: "HEAD" }),
);
if (!candidate.ok && ![304, 412, 416].includes(candidate.status)) {
await candidate.body?.cancel();
return htmlFallback(original, request, probeChanged, env);
}
const response = new Response(
useMarkdown ? candidate.body : original.body,
useMarkdown ? candidate : original,
);
if (useMarkdown) {
await original.body?.cancel();
if (candidate.status < 400)
response.headers.set("Content-Type", "text/markdown; charset=utf-8");
} else {
await candidate.body?.cancel();
}
return advertiseMarkdown(response, markdown.pathname);
},
};