Workbench / The TiffinBox Build
S13 · V2·13
Ordering With No JavaScript
Volume 2, chapter 13. The customer's whole ordering flow (menu, search, order form, review, place order, receipt) is built as server-rendered HTML forms, and checked in Chromium with JavaScript switched off. Post/Redirect/Get and the stage 5 idempotency key, carried in a hidden field, make reloads, the back button and double clicks safe; every dynamic value is escaped; form bodies are parsed strictly; mistakes come back on the form with the customer's input kept. A first version that reviewed orders by POST sent the back button to a "Confirm Form Resubmission" error, and the fix was to use the method HTML meant for it.
Stage 13: ordering with no JavaScript
The brief
What you'll build. Server-rendered pages, in a new Pages class:
GET /menu?q=: the menu (or a search over it) inside a page with a header, navigation, asearchlandmark and a skip link;GET /order: the order form: customer number, kitchen, a labelled quantity field per dish, and a hidden idempotency key generated when the form is rendered;GET /order/review: the order checked and priced by the rules, with every mistake listed in an alert at the top of the form and the customer's input kept;POST /checkout: places the order through the same code as the JSON API (stage 11), then 303 See Other to the receipt;GET /receipt/{id}: the placed order and its stored price.
Plus Html (escaping), Form (strict application/x-www-form-urlencoded parsing), PagesTest (4 tests, 17 server tests in total), web-tests/stage13.py (the flow in Chromium with JavaScript off) and web-tests/validate.sh (the Nu HTML Checker over every page).
Why a real company needs this. A checkout that works without JavaScript works on a slow 3G connection when the script bundle times out, on a phone with a broken browser extension, for a screen-reader user, and for the tests that check it. It also forces the hard parts (validation, idempotency, error messages, what each URL means) to live on the server, where they must be anyway. Stage 15 adds JavaScript on top; nothing in this stage depends on it.
Why now. Chapter 13 is HTML: semantics, landmarks, forms, labels, input types, validation and what forms actually send. Stage 12 set the security baseline (no inline script or style) that these pages must respect.
What you'll have at the end.
- the full ordering flow with no script, valid HTML on every page;
- 4 new server tests and two browser-level checks;
- ADR-0015: HTML first, and what each URL in the flow means.
Theory to own
- GET or POST. For each of "search the menu", "review my order" and "place my order", which method is right, and what does the choice give the user? (Chapter: "Form anatomy and submission"; Companion, Interview prep Q1.)
- Post/Redirect/Get. Why answer a successful
POST /checkoutwith a 303 instead of the receipt itself? (Chapter Exercises P8.) - Double submission. A customer double-clicks "Place order". What reaches the server, and what makes it one order? (Volume 1, stages 5 and 9.)
- Escaping. A dish named
<script>alert(1)</script>is added to the menu. What must every page do with it, and where should that happen? (Chapter 12: XSS; Companion, theoretical gap 4.) - Quantity fields. Why not
type="number"for quantities, given the server's rule of a whole number from 0 to 20? (Companion, Gaps 3.)
Build it
Step 1: escaping, and a page layout
Write Html.text(s) (escaping & < > " ') and Pages.page(status, title, main): a complete document with <html lang="en">, <meta charset="utf-8"> first, the viewport, a <title>, the stylesheet, a skip link, a <header> with the brand and a <nav aria-label="Main">, <main id="main">, and a <footer>. Pages are Cache-Control: no-store (they show order data). No inline script or style anywhere: stage 12's CSP forbids both.
Checkpoint. With a dish named <script>alert(1)</script> & "vada" on the menu, /menu shows it as text, and neither /menu nor a search for <script> contains a <script element.
Step 2: the menu and the order form
Write /menu (a GET search form inside <search>, a sentence saying how many dishes matched, a list of dishes with prices) and /order:
- a hidden
keyfield with a fresh UUID on every rendering; - a customer number field with a
<label>and a hint linked byaria-describedby(sign-in arrives in Volume 3); - a kitchen
<select>; - a
<fieldset>with a<legend>, and for each dish a<label>("Masala dosa, ₹89.50") and an input withinputmode="numeric" pattern="[0-9]{0,2}" maxlength="2"; - a submit button,
type="submit", labelled "Review order".
Checkpoint. In Chromium with JavaScript off, searching "dosa" shows "1 dish matches "dosa"." and the accessibility tree shows a textbox named "Customer number" and a combobox named "Kitchen".
Step 3: review, with errors on the form
Write GET /order/review. Validate into either a draft (customer, key, kitchen, lines) or a list of errors written for people ("Masala dosa: the quantity must be a whole number from 0 to 20."). On errors, re-render the order form with status 400, an alert listing every problem (role="alert"), and every value the customer typed. On success, price the draft with the rules (stage 10) and show a table (<caption>, scope on headers, a <tfoot> with subtotal, discount and amount to pay), then a POST form to /checkout carrying the draft in hidden fields and a "Place order" button.
Checkpoint. ?customer=&kitchen=k7&q-d1=1e1&q-d2=3 returns 400 with both problems listed and value="3" kept in the second dish's field; a valid draft shows ₹179.00 for two masala dosas.
Step 4: checkout with Post/Redirect/Get, and strict form parsing
Write Form.parse(body): split on &, decode + as a space and %XX escapes, reject malformed escapes, raw non-ASCII bytes, invalid UTF-8, repeated field names and more than 100 fields. Then POST /checkout: parse, validate with draft, place with the shared placement method (refactored out of stage 9's JSON handler so both use one code path), and answer 303 See Other with Location: /receipt/{id}.
Checkpoint. The same checkout body posted twice redirects to /receipt/o1 both times; bodies with %E0%B0, %zz or a repeated field are rejected with a message.
Step 5: the flow in a browser, with JavaScript off
Write web-tests/stage13.py: with JavaScript disabled in Chromium, fill the order form, review, place the order, then reload the receipt, go back to the review page, place the order again from there, and double-click "Place order" on a fresh order. Count orders on the server after each step. First build review as a POST (the form's method="post"), and run the script.
Checkpoint. Every path leaves exactly one order per attempt, and the back button works.
Step 6: validate the HTML
Write web-tests/validate.sh: start the server, fetch every kind of page (/, /menu, /order, a review, a review with errors, a receipt), and run the Nu HTML Checker (npm install vnu-jar) over them.
Checkpoint. No errors or warnings on any page.
Senior and architect lens
1. Build the baseline the platform guarantees, then enhance. Every later stage (JavaScript, React, offline support) can fail without the customer losing the ability to order. That's a strong property to have for the flow that makes money, and it costs little when done first.
2. ADR-0015: HTML first, and the meaning of each URL. Write it with these points:
- Decision: the customer ordering flow is server-rendered HTML forms that work without JavaScript; scripts enhance it.
- URLs:
/menu?q=(GET, shareable),/order(GET, new attempt and key),/order/review(GET, a priced preview, no side effects),/checkout(POST, idempotent by the form's key, 303 to the receipt),/receipt/{id}(GET). - Rules: every dynamic value escaped by
Html.text; form bodies parsed byForm.parse; the same validation and placement code for HTML and JSON clients. - Revisit when: sign-in arrives (the customer field and its URL exposure go away), or a flow genuinely needs script (live tracking, stage 17).
3. Use the method that matches the meaning. The back-button error came from using POST for something that changed nothing. HTTP methods aren't style: they decide caching, history, retries, prefetching and what browsers will repeat without asking. Choosing them by meaning removes a whole class of browser behaviour bugs.
4. Idempotency belongs to the attempt, not the request. The key is generated when the customer starts an attempt (the form is rendered) and travels with every submission of it. That one decision makes double clicks, back-and-resubmit and network retries all safe, without any client-side tricks such as disabling the button (which fails without JavaScript, and fails anyway on a network retry).
5. Shared paths beat parallel paths. Refactoring placement into one method used by both the JSON API and the HTML checkout means idempotency, pricing, locking and durability are tested once and can't drift between clients. Volume 3 keeps the same rule: controllers are thin adapters over one service.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| it has to look good and work on a phone | app.css is one line |
stage 14 |
| searching as you type | a full page load per search | stage 15 (an enhancement over this form) |
| the customer number in URLs and history | there's no sign-in | Volume 3 (sessions and authentication) |
| anyone with an order number can see a receipt | receipts aren't tied to a customer | Volume 3 |
| the menu has hundreds of dishes | one quantity field per dish on one page | stage 21 (UI architecture: a cart) |
Evidence
- The server build prints
tests: 17 passed;web-tests/validate.shprints "valid: 6 pages, no errors or warnings". web-tests/stage13.pyoutput, including the first version'sERR_CACHE_MISS, is in the README.- A screenshot of the order form with errors, and its accessibility snapshot, are in the README.
- A test proves a hostile dish name is escaped on every page that shows dish names.
- ADR-0015 is committed.
Carry forward
- The HTML flow is the baseline every enhancement must keep working: stage 15 adds search-as-you-type on top of the
/menuform, and stage 24's server-rendered menu falls back to these pages. - The form's idempotency key is the model for every client: stage 19's JavaScript retries and stage 29's background sync carry a key per attempt.
Html.textandForm.parseare the only ways values enter and leave pages; stage 28 tests them against a hostile corpus.web-tests/validate.shruns on every stage from now on.