Ask form submit handler: post the question
handler · lines 419–444View on GitHubIntercepts the Ask form and posts the question.
Calls Module scope The demo page
$("ask").addEventListener("submit", async (event) => { event.preventDefault(); const form = event.target; const question = form.elements.question.value.trim(); const status = $("ask-status"); if (question === "") return;Intercept the form, read the question In the browser
WhatA submit listener on #ask calls preventDefault so the browser does not navigate, then reads the input named question and trims it. When nothing is left, it returns without sending a request.
WhyThe page stays in place and makes the request itself. The input's maxlength="500" (line 80) and this trim mirror the Worker's ASK 1 check, so a question typed into the page never draws ASK 1's 400.
input maxlength = 500
const button = form.querySelector("button"); button.disabled = true; status.textContent = STAGES.retrieving; const answers = $("answers");Disable Ask, name the first stage In the browser
WhatThe submit button is disabled, and the status line (#ask-status, an aria-live region) reads "Searching your documents…" before any byte is sent. answers is the container the answer boxes go into.
WhyDisabling the button stops a double click from posting the question twice, which would reserve two of the demo account's ten questions. The finally block at the end of the handler re-enables it on every path. The handler sets the status text synchronously, so it shows at the next paint instead of a network round trip later, when the Worker's first stage event (retrieving) arrives.
STAGES.retrieving = "Searching your documents…"
try { // `Accept: text/event-stream` asks for the answer as a stream of events (one checked // sentence at a time) rather than one JSON body at the end. `fetch` resolves as soon as the // headers arrive; the body is then read below while the Worker is still writing it. const response = await fetch("/api/demo/ask", { method: "POST", headers: { "Content-Type": "application/json", Accept: "text/event-stream" }, body: JSON.stringify({ question }), });POST with Accept: text/event-stream In the browser
WhatInside a try, fetch posts {"question": …} as JSON to /api/demo/ask with Accept: text/event-stream. The URL is the site's Pages Function on the same origin, so the demo cookie goes with the request. The await resolves when the status and headers arrive, while the body is still open.
WhyThe Accept header selects the Worker's server-sent event rendering of the ask over one JSON body at the end, and the site's Pages Function forwards the header unchanged. EventSource cannot be used because it only issues GET requests with no body, and the question is a POST body. fetch with a streamed response body delivers the same events without that restriction.
method: POSTContent-Type: application/jsonAccept: text/event-stream
Lines 429–437 on GitHubHTML Standard: server-sent events (EventSource)Pages Function: the body passed through unread
// Refusals before any answer (the limit, a bad question, signed out) come as JSON. if (!(response.headers.get("Content-Type") ?? "").includes("text/event-stream")) { const body = await response.json().catch(() => ({})); if (response.status === 429) { status.textContent = ""; showOffer(); return; } status.textContent = body.error || "Something went wrong. Please try again in a minute."; return; }A refusal comes as JSON In the browser
WhatIf the response's Content-Type does not contain text/event-stream, the body is parsed as JSON, and an unparseable body reads as {}. A 429 clears the status line and calls showOffer, which sets left to 0, writes "0 questions left.", hides the form (#ask) and shows the offer (#offer). Any other status shows body.error, or a generic retry line.
WhyThe Worker answers in JSON whenever it refuses before streaming. ASK 1 returns 400 for an empty question or one over 500 characters. ASK 2 returns 429, with the body {error, left: 0}, when no question is left. The router returns 401 when the access key matches no demo account, before ASK 1 runs, so a signed-out visitor gets 401 whatever the question. The site's Pages Function returns its own 503 when the DEMO service binding is missing. The media type is the reliable signal, because every streamed reply is a 200 text/event-stream and every refusal is JSON. A 401's error is the bare text "signed out", shown as it is.
429 → showOffer()400: "Please ask a question of up to 500 characters."