Joel Stevick

Production RAG, line by line · Markup, styles and how the site serves it · stage 1 of 6

The demo page

GET /demo serves the static demo.html through the Pages Function functions/demo.ts, which sets the HttpOnly demo cookie to the access key in the address's ?key= (the visitor's sign-in link); the cookie signs the browser in to the visitor's demo account (their row in the demos table, which owns their files and questions). The page holds two boxes, #signed-out and #signed-in. Both stay hidden until the module script calls start() and the session request decides which one to show.

Browser: the visitor's page: demo.html and its module scriptPages Function: the site's server code on Cloudflare Pages, functions/New to Cloudflare? How these fit together
demo.html134 / 134 lines on this page explainedlines 1-129,501-505 of 505; the rest are on other stage pages
functions/demo.ts27 / 27 lines on this page explainedevery line of the file
In the browser
runs in the visitor's browser, on the demo page
Ingestion
runs when a PDF's text is uploaded
Query time
runs when the Worker answers a question or a search_documents call from the MCP server, and in the request handling every route shares, in the Worker and in the site's functions, which uploads and removals pass through too
MCP request
runs on a request to the MCP server or its OAuth endpoints
Site request
runs on a site request that is not a question, an upload, an MCP call or the cron: a page load, a session read, the demo page's /api/grants calls, an admin read, a file's removal
Scheduled
runs in the Worker's daily cron job
Eval run
runs when the eval harness runs on a developer's machine
MCP host
runs in an MCP host outside the Worker: the search loop example, on a developer's machine (npm run search) or in its test
Module load
runs once, when the module is loaded
Type only
erased by the TypeScript compiler; never runs
Never runs
a comment, a licence or markup with no behaviour of its own

Pinned to joelstevick-demo-rag-pipeline at 6a9c6c7.

demo.html The demo page: lines 1–129 (the head, the markup and the module script's top-level constants) and lines 501–505 (the start() call and the closing tags).

<head>

markup · lines 1–33View on GitHub

The document head: the licence, metadata for search and link previews, the site's stylesheet, the page's own styles and the site's two scripts.

<!doctype html><!--  Copyright (c) 2026 Joel Stevick  SPDX-License-Identifier: MIT  Licensed under the MIT License: https://opensource.org/licenses/MIT-->

Doctype and licence Never runs

WhatThe HTML5 doctype, then an HTML comment with the copyright, the SPDX identifier and the MIT licence.

WhyThe doctype puts the browser in standards mode.

<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Test the answer quality on your own documents — Joel Stevick</title><meta name="description" content="A free test of answer quality and source checks: upload your own PDFs and ask up to ten questions, each answer citing the file and page it rests on."><link rel="canonical" href="https://joelstevick.com/demo"><meta property="og:title" content="Try it on your own PDFs: the free demo"><meta property="og:description" content="A free test of answer quality and source checks: upload your own PDFs and ask up to ten questions, each answer citing the file and page it rests on."><meta property="og:image" content="https://joelstevick.com/img/og.jpg"><meta property="og:url" content="https://joelstevick.com/demo"><meta property="og:type" content="website"><meta property="og:site_name" content="Joel Stevick"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:image" content="https://joelstevick.com/img/og.jpg"><link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'><rect width='64' height='64' rx='12' fill='%23254d42'/><text x='32' y='43' font-family='Helvetica,Arial' font-size='30' font-weight='700' fill='white' text-anchor='middle'>JS</text></svg>">

Metadata Never runs

WhatThe document language, character set and responsive viewport; the title and description; the canonical URL; Open Graph and Twitter card tags; and a favicon drawn as an inline SVG data URL.

WhyThe canonical URL is https://joelstevick.com/demo with no query, so search engines index a shared /demo?key=… link as the plain page. The SVG favicon costs no extra request. The # of its fill colour is written %23 because a bare # starts the URL's fragment and would truncate the SVG data.

canonical: https://joelstevick.com/demo

<link rel="stylesheet" href="/styles.css"><style>  /* The "Use it from Claude Code" box; styles.css is never edited. */  .connect { margin-top: 3rem; padding-top: 1.5rem; border-top: 1px solid currentColor; }  .connect-line { overflow-x: auto; padding: .8rem 1rem; border: 1px solid currentColor; border-radius: .4rem; font-size: .9rem; }  .apps { list-style: none; padding: 0; }  .apps li { display: flex; gap: 1rem; align-items: baseline; justify-content: space-between; padding: .5rem 0; border-bottom: 1px solid rgba(128, 128, 128, .35); }</style>

The site's stylesheet and the page's own styles Never runs

WhatA link to /styles.css, then four rules for the "Use it from Claude Code" box: its top border and spacing, the bordered, scrollable box around the command, and the connected-apps list, laid out as rows with the Disconnect button on the right.

Whystyles.css belongs to the site and is not in this repository (README, Run it). The comment says it is never edited, so the styles only this page needs are written inline. overflow-x: auto lets the long claude mcp add command scroll sideways on a phone instead of widening the layout.

<script src="/account.js" defer></script><script src="/track.js" defer></script></head>

The site's scripts In the browser

What/account.js and /track.js, both deferred, then the end of the head.

WhyNeither file is in this repository. On the site, account.js shows or hides the header's account links (the elements with data-signed-in and data-signed-out), and track.js records visits with consent. The demo page works without either. defer runs both after the document is parsed, so neither blocks rendering.

Header

markup · lines 34–50View on GitHub

The skip link and the site's top bar.

<body><a class="skip" href="#main">Skip to content</a><header class="top">  <div class="wrap">    <a class="brand" href="/"><img src="/img/joel-stevick-avatar.jpg" alt="" width="36" height="36">Joel Stevick</a>    <nav>      <a href="/">Home</a>      <a href="/learn">Learn</a>      <a href="/blog">Blog</a>      <a href="/learn/source">Code, line by line</a>      <span class="account"><a href="/sign-in" data-signed-out>Sign in</a><a href="/admin" rel="nofollow" data-admin hidden>Admin</a><a href="/projects" data-signed-in hidden>Projects</a><a href="/" data-signed-in data-sign-out hidden>Sign out</a></span>      <a class="button try" href="/demo">See the answer quality</a>      <a class="button" href="https://github.com/AiFirstDevelopment/joelstevick-demo-rag-pipeline" target="_blank" rel="noopener">Read the code</a>    </nav>  </div></header>

Skip link and navigation Never runs

WhatA skip link to #main, the brand link (avatar and name, to /), and the navigation: Home; Learn (the free video series); Blog; Code, line by line (this walkthrough); an account span whose links carry hidden, data-signed-out, data-signed-in, data-admin and data-sign-out; the See the answer quality button to /demo; and the Read the code button to this repository on GitHub.

WhyThe skip link lets a keyboard or screen-reader user jump past the navigation. The links point at what the site offers now, the free series and this code, and at nothing for sale. Every account link except Sign in starts hidden, and the site's account.js toggles them. The Admin link is rel="nofollow" so crawlers do not follow it. The GitHub button opens a new tab with rel="noopener", so the opened page gets no reference to this one (window.opener).

#signed-out

markup · lines 51–67View on GitHub

The heading, and the box a visitor without a live demo account sees.

<main id="main" class="case demo">  <div class="wrap">    <p class="eyebrow">See the answer quality</p>    <h1>Ask your own documents.</h1>    <div id="signed-out" hidden>      <p class="expired" id="expired" hidden>This demo link has expired or is no longer valid. A demo's search index is deleted after 10 days without use.</p>      <p class="lede">Upload a set of your own PDFs and ask a chatbot built from them on the spot. It shows the answer quality and the source checks on files you can hand it in a minute: each answer is two to four sentences, or a list of up to twelve items when it names several things, with the file and page it rests on. The answer appears a sentence at a time, each checked against its sources first, and a sentence whose sources don't check out is never shown. The free video series builds a pipeline like this one a step at a time: <a href="/learn">start with episode 1</a>.</p>      <ul class="limits">        <li>Up to 10 questions.</li>        <li>Up to 5 PDFs, 300 pages and 500 KB of text in all. Scanned pages without text are skipped.</li>        <li class="retention">Your PDFs are read in this browser and never uploaded. Only the text of each page and the file name are sent. The demo's search index is deleted after 10 days without use. The text, your questions and the answers are kept for up to 25 months to analyse and improve the demo and to build test sets for chatbots (with your consent in the EEA, UK and Switzerland). Don't upload anything confidential, or anything a government controls (classified, controlled unclassified or export-controlled). Remove a PDF and its text goes with it. <a href="/privacy">Privacy policy</a> · <a href="/terms">Terms</a></li>        <li>Try it free — just verify your email.</li>      </ul>      <div class="actions"><a class="button" id="try-it" href="/register?for=demo">Try it free</a></div>    </div>

Heading and the sign-up box Never runs

Whatmain#main with the eyebrow and the h1, then #signed-out, hidden at first: the expired line (also hidden), a description of the demo app with a link to the free video series, the limits list with the retention notice, and the Try it free link to the sign-up page that creates a demo account.

WhyBoth boxes start hidden, so a visitor never sees the wrong one flash before start() has the session. The limits list repeats the Worker's constants: LIMITS (10 questions, 5 PDFs, 300 pages and 500,000 characters, written "500 KB of text"), IDLE_DAYS (10) and KEEP_MONTHS (25). The script holds no copy of these values, so a changed limit must be edited in this markup by hand.

LIMITS = { asks: 10, files: 5, pages: 300, chars: 500_000 }IDLE_DAYS = 10KEEP_MONTHS = 25

#signed-in

markup · lines 68–110View on GitHub

The signed-in view: the upload button, the file list, the question form, the answers, the offer shown when the questions run out, and the MCP box.

    <div id="signed-in" hidden>      <p class="notice retention">Your PDFs are read in this browser and never uploaded. Only the text of each page and the file name are sent. The demo's search index is deleted after 10 days without use. The text, your questions and the answers are kept for up to 25 months to analyse and improve the demo and to build test sets for chatbots (with your consent in the EEA, UK and Switzerland). Don't upload anything confidential, or anything a government controls (classified, controlled unclassified or export-controlled). Remove a PDF and its text goes with it. <a href="/privacy">Privacy policy</a> · <a href="/terms">Terms</a>. Bookmark this page's address to come back on another device.</p>      <p class="notice" id="cleared" hidden>Your documents' search index was deleted after 10 days without use. Upload them again to keep asking.</p>      <div class="upload">        <!-- The native input is cleared after each upload, so its own "No file chosen" would contradict the list below; the label is the button. -->        <label class="button pick">Add PDFs <input id="pdfs" type="file" accept="application/pdf,.pdf" multiple></label>      </div>      <ul class="files" id="files"></ul>      <p class="status" id="upload-status" role="status" aria-live="polite"></p>      <form class="ask" id="ask">        <input name="question" maxlength="500" placeholder="Ask a question about your documents" aria-label="Your question" autocomplete="off">        <button class="button" type="submit">Ask</button>      </form>      <p class="left" id="left"></p>      <p class="status" id="ask-status" role="status" aria-live="polite"></p>      <div id="answers" aria-live="polite"></div>

Upload, files and the question form Never runs

WhatThe retention notice, the cleared notice (hidden), a label styled as a button that wraps the file input (PDFs only, several at once), the #files list, a status line, the #ask form with a question input of maxlength 500, the questions-left line, a second status line and #answers.

WhyThe status lines are role="status" with aria-live="polite", and #answers is aria-live, so screen readers announce progress and answers without moving focus. maxlength 500 mirrors MAX_QUESTION in the Worker, which still checks the trimmed length itself. The notice asks the visitor to bookmark the address. After a ?key= link the address still holds the key, so the bookmark signs another browser in until the demo account is given a new key (by a new sign-up or openDemo). After that, the old key gets a 401 with expired: true in its body.

maxlength = 500 (MAX_QUESTION = 500)accept: application/pdf,.pdf

      <div class="offer" id="offer" hidden>        <h2>That was your ten questions.</h2>        <p>Want to build this yourself? The free video series builds this pipeline a step at a time, from your first call to a model to answers that cite their sources, and then the eval harness that proves the answers are good.</p>        <div class="actions">          <a class="button" href="/learn">Start with episode 1</a>          <a class="button ghost" href="https://github.com/AiFirstDevelopment/joelstevick-demo-rag-pipeline" target="_blank" rel="noopener">Read this demo's code</a>        </div>      </div>

After the tenth question Never runs

What#offer (hidden): a heading, a paragraph on the free video series that builds this pipeline, a link to the series and a link to this repository on GitHub.

WhyshowOffer reveals #offer and hides the form once no question is left. It is called from the session on load, from an ask's done or fallback event, and on a 429. The GitHub link opens a new tab with rel="noopener", so the opened page gets no reference to this one.

      <!-- The demo's MCP server (README.md, "The MCP server"): shown once a PDF is uploaded. Nothing secret is on the page. -->      <section class="connect" id="connect" hidden aria-labelledby="connect-title">        <h2 id="connect-title">Use it from Claude Code</h2>        <p>Your documents are also an MCP server: Claude Code can search them, ask them and read them. Paste this into a terminal:</p>        <pre class="connect-line"><code id="connect-line">claude mcp add --transport http my-docs https://joelstevick.com/mcp/demo</code></pre>        <p><button type="button" class="button ghost" id="connect-copy">Copy</button></p>        <p>The first time Claude Code uses it, it opens this browser to ask your permission. A question it asks counts against the ten on this page.</p>        <h3>Connected apps</h3>        <p id="apps-none">No app is connected.</p>        <ul class="apps" id="apps"></ul>        <p class="status" id="apps-status" role="status" aria-live="polite"></p>      </section>    </div>  </div></main>

Use it from Claude Code Never runs

Whatsection#connect (hidden): the claude mcp add command, a Copy button, a note that the first use opens a consent page and that its questions count against the same ten, then the Connected apps list, its empty line and a status line. #signed-in, its wrapper and the main element close.

WhyshowConnect shows the section only while the demo account has a file and the Worker reports the MCP server as set up, and it rewrites the command with location.origin. The command holds no secret. Claude Code gets its access through the MCP server's OAuth flow.

Footer

markup · lines 111–118View on GitHub

The site's footer.

<footer>  <div class="wrap">    <span>© 2026 Joel Stevick</span>    <span class="legal"><a href="/learn/source">How it works, line by line</a><a href="/terms">Terms</a><a href="/privacy">Privacy</a><a href="/privacy#choices" data-privacy-choices>Privacy choices</a><a href="https://github.com/AiFirstDevelopment" target="_blank" rel="noopener">GitHub</a></span>  </div></footer>

Footer links Never runs

WhatThe copyright, then links to How it works, line by line (/learn/source), Terms, Privacy, Privacy choices and the GitHub organisation.

WhyThe Privacy choices link carries data-privacy-choices, the attribute the site's consent script looks for. Without that script, the link still goes to /privacy#choices.

Module scope

module scope · lines 119–129View on GitHub

The page script's top-level constants and state, and two small helpers.

Called by #pdfs change handler Upload handler answerBox() File list and stream reader Ask form submit handler: post the question Ask form submit Ask form submit handler: read the stream, render it File list and stream reader Copy button handler Listing and disconnecting apps drawApps() Listing and disconnecting apps listFile() File list and stream reader removeFile() Removing a file showConnect() Listing and disconnecting apps start() Demo accounts and sessions

<script type="module">  const PDFJS = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.10.38";  const $ = (id) => document.getElementById(id);

A module script, pdf.js's address and $ In the browser

WhatThe script is type="module". PDFJS is the cdnjs base URL of pdf.js 4.10.38, and $ is a shorthand for document.getElementById.

WhyA module script is deferred, so it runs after the document is parsed and start() finds every element. Its top-level names also stay out of the global scope. pdf.js is pinned to one version and imported only when pagesOf reads the first PDF, so a visitor who only reads answers never downloads it.

pdf.js 4.10.38

  // Each PDF this tab added (accepted by the Worker), kept as an object URL for the life of  // the tab, so a source's name can open it at the cited page. Never sent anywhere.  const opened = new Map();  let left = 0;

Object URLs of opened PDFs, and the questions left In the browser

Whatopened maps a file name to an object URL for a PDF added in this tab. A file is kept only after the Worker accepts it (line 287). left holds the number of questions left.

WhyAn object URL points at the File in the tab's memory, so a cited source can open the PDF at its page (#page=N) although the file was never uploaded. The URLs are never revoked and are lost on reload, so only files added in this tab become links.

  const showLeft = () => { $("left").textContent = left === 1 ? "1 question left." : `${left} questions left.`; };  const showOffer = () => { left = 0; showLeft(); $("offer").hidden = false; $("ask").hidden = true; };

showLeft and showOffer In the browser

WhatshowLeft writes "1 question left." or "N questions left.". showOffer sets left to 0, redraws that line, shows #offer and hides the form.

WhyThe Worker's 429 arrives as a JSON body instead of a stream event, and the page does not read that body's left field, so showOffer sets left to 0 itself. On its other two calls (the session on load, and a done or fallback event) left is already 0 or below.

Page end

module scope · lines 501–505View on GitHub

The one call that starts the page, and the closing tags.

Calls start() Demo accounts and sessions

  start();</script></body></html>

start(), then close In the browser

WhatCalls start() once, then closes the script, body and html elements.

Whystart() is the last statement, so every function and listener above is defined before the session request goes out. Its promise is not awaited. start() shows the sign-up box when the request fails or returns a non-2xx status, but a 2xx body that is not JSON makes body.files.forEach throw, and nothing catches that rejection.

functions/demo.ts The whole Pages Function for /demo. It serves demo.html and sets the cookie, from the ?key= in the address, that signs the browser in to its demo account.

Module scope

module scope · lines 1–9View on GitHub

The licence, a comment on what the function does, and its imports.

// Copyright (c) 2026 Joel Stevick// SPDX-License-Identifier: MIT// Licensed under the MIT License: https://opensource.org/licenses/MIT

Licence Never runs

WhatThe copyright and the MIT licence, with an SPDX identifier.

WhyEvery source file in the repository except types/html.d.ts carries the same header, so a licence scanner can read the licence from each file.

// /demo?key=<key> signs this browser in to that demo: the link a prospect// bookmarks after saving a card. The page itself is demo.html.

What the function does Never runs

WhatThe comment says /demo?key=<key> signs the browser in to that demo account, and that the page itself is demo.html.

WhyCloudflare Pages routes by file name, so functions/demo.ts runs for /demo in front of the static demo.html. The comment's "after saving a card" is out of date. A demo account is now tied to a verified email address (demo.html line 64, app.ts:129–130), and the link is any address that carries the key.

import { demoCookie, isDemoKey } from "./_lib/demo";import { serverEvent, type TrackEnv } from "./_lib/track";

Imports Module load

WhatdemoCookie and isDemoKey from functions/_lib/demo.ts; serverEvent and the TrackEnv type from functions/_lib/track.ts.

Why_lib/demo.ts defines the key's format and the cookie's attributes once. The /api/demo/* Pages Function imports the same module to read the cookie back.

onRequestGet

handler · lines 10–27View on GitHub

Serves demo.html and sets the demo cookie to the access key in ?key=.

Calls demoCookie() Pages Function /api/demo/* isDemoKey Pages Function /api/demo/* serverEvent() Pages Function /api/demo/*

export const onRequestGet: PagesFunction<TrackEnv> = async ({ request, env, next }) => {

GET only Site request

WhatRuns on every GET /demo. Exporting the handler as onRequestGet binds it to GET only.

WhyA request with any other method skips this handler and falls through to Pages' static asset handling. Opening a bookmark sends a GET, so setting the cookie on GET is what lets a bookmarked address sign the browser in.

  // Read the key from the link and fetch the page as usual.  const key = new URL(request.url).searchParams.get("key");  const page = await next();

Read the key, fetch the page Site request

Whatkey is the ?key= query parameter, or null. next() passes the request on to Pages, which serves the static demo.html.

WhyThe function fetches the page before it examines the key, so every branch after it returns the same HTML. The key decides only the headers.

  // Without a well-formed key, the page is served as it is.  if (!isDemoKey(key)) return page;

No well-formed key, page unchanged Site request

WhatisDemoKey tests /^[A-Za-z0-9_-]{43}$/, 43 base64url characters, which is the length randomKey(32) produces. Any other value returns the page as next() returned it.

WhyThe function checks only the shape and makes no call to the Worker. A well-formed key that matches no live demo account still sets the cookie. The session request then answers 401 with expired, and the page shows the expired line.

/^[A-Za-z0-9_-]{43}$/32 random bytes → 43 characters

  // Note that someone opened their demo from the link.  await serverEvent(env, request, "demo_open", {});

Record the open Site request

WhatRecords a demo_open event, and waits for it, before the response is built.

WhyIn this repository serverEvent is a stub that records nothing (functions/_lib/track.ts). On the site, it writes the event to the site's own analytics.

  // Hand back the same page with the demo cookie set, and keep it out of any cache.  const response = new Response(page.body, page);  response.headers.append("Set-Cookie", demoCookie(key));  response.headers.set("Cache-Control", "no-store");  return response;};

Same page, with the cookie, never cached Site request

Whatnew Response(page.body, page) copies the response so its headers can be changed. demoCookie(key) is appended as a Set-Cookie header, and Cache-Control is set to no-store.

Whynext() returns a Response with immutable headers, so the function builds a new one around the same body stream. The cookie is HttpOnly (no script can read it), Secure and SameSite=Lax, and lasts 14 days. no-store stops a cache from keeping a response that carries one visitor's Set-Cookie and serving it to another.

cookie: demo, Path=/Max-Age = 14 × 86,400 = 1,209,600 sHttpOnly; Secure; SameSite=LaxCache-Control: no-store