Developers
Working on the timer
All developer pages · From docs/timer-brief.md in the release that is running.
You are working on the Pub Runners timing app, served at https://pub-runners.com/timer/. It lives in the club's website repo, tomsaunders/pub-runners (a Laravel site), in the folder public/timer. It is plain HTML, CSS and JavaScript with no build step: the files are served exactly as they are in the repo.
This brief lives in the repo as docs/timer-brief.md and is published at https://pub-runners.com/developers/timer-brief, so the latest version is always there. Give it to your coding agent as it is.
Read these first (each guide is also a page at https://pub-runners.com/developers):
CONTRIBUTING.md(Contributing): the workflow and the rules for/timer.docs/timer.md(The timer app): what the app does and which API call each screen makes.public/timer/index.html,app.js,app.cssandmap.js: the app itself.docs/partner-api.md(Partner API guide) and the API reference: the API.
Do the change the developer asks for, and nothing beyond it.
1. Workflow
- Branch: make a branch from
mainintomsaunders/pub-runnersitself, not a fork (forks get no preview site). - Scope: change only files in
public/timer. If the app needs anything from the server (a new endpoint, a new field, a change to the security policy), stop and say so in the pull request. The club makes server changes. - Never push to
main. A push tomaindeploys straight to the live site. - Pull request: open one per change. In the description, say what changed and how you tested it.
- CI: it checks that every
public/timer/*.jsparses, thatindex.htmlonly loads scripts the security policy allows, and runs the site's tests. Fix any failure before asking for review. - Preview site: once CI passes, the pull request gets its own site at
https://pr<number>.demo.pub-runners.com/timer/, with made-up runners and runs. Every push updates it, and closing the pull request removes it. Sign in ashost(oradmin) with the demo password the club sends you; staging and every preview have the same sign-ins. - Merge: the club reviews and merges. Merging deploys to staging, then to pub-runners.com.
2. How it is served
- The page is served at
/timer/. Always link to it with the trailing slash. Keep<base href="/timer/">inindex.htmland use relative URLs for the app's own files. - The site serves
index.htmlwith this Content Security Policy, so everything must work under it:
What the policy means for the code:default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https://tile.openstreetmap.org; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'- Scripts: only from
.jsfiles inpublic/timer. No inline<script>blocks, and noonclick="..."or other event-handler attributes, including in HTML built withinnerHTML. Attach handlers from JavaScript, as the app does (el.onclick = ...oraddEventListener). - Nothing from other sites: no CDNs, external fonts, analytics or third-party APIs.
- Styles: inline style attributes and
<style>blocks are fine. - Images: OpenStreetMap tiles are allowed, so the route map works.
- Requests:
fetchmay only go to the same origin. Turn every API URL the server gives you into a path, for examplenew URL(run.route_gpx_url, location.href).pathname.
- Scripts: only from
- The app runs on the club's own site, so it acts with the rights of whoever is signed in, admins included. Never store or send anything except what is described here.
- Never put an API key, password or token in any file in the repo.
3. Who is who, and how they authenticate
The API base is /api/v1. Send Accept: application/json on every request, and JSON bodies with Content-Type: application/json. Errors are JSON with a message. A 422 also carries errors keyed by field.
Timers: hosts signed in on the site (cookie session)
- Start-up: call
GET /sanctum/csrf-cookiewithcredentials: 'same-origin'. This sets anXSRF-TOKENcookie. - Every request that is not a GET: send the header
X-XSRF-TOKENset to the URL-decoded value of that cookie, withcredentials: 'same-origin'. If you get 419, fetch/sanctum/csrf-cookieagain and retry once. - Am I a host? Call
GET /api/v1/timing/runs.- 200: the user can time.
- 401: not signed in. Offer a link to
/timer/sign-in, which goes through the site's login page and comes back to/timer/. - 403: signed in, but not allowed to time.
- Any signed-in host can time, and several hosts can tap at once. Hosts can also build the results ("finalise").
Participants: their own phones, no account
- The code: one code for the club,
1901unless a host has changed it. It carries over from week to week. The timer shows it, and a host can change it (section 4). - Join: a phone joins with
POST /api/v1/participant/join {"code": "1901"}.- It only ever joins tonight's run: the run dated today, once a host has opened it and until it is finalised.
- The code is case-insensitive, and spaces around it are ignored.
- A wrong code gets 404 with the message "That is not tonight's code. Check it with the timer." If tonight's run is not open, it is 404 with "Tonight's run is not open yet. Ask the timer to open it."
- The limit is 10 joins a minute per IP.
- The token: joining returns
{"token": "pt_...", "run": {...}}. Store the token inlocalStorageand send it asAuthorization: Bearer pt_...on every participant request.- A 401 means the token is gone. Forget it and show the join screen.
- The limit is 60 requests a minute per participant.
- Join links:
/timer/?join=CODEjoins with that code, then removes it from the address bar. - What a token can do: reach only its own check-in. Participants never receive the club's name list, and see other people only as counts.
A phone can be both a participant and a host. Whatever screen is showing, timer taps must keep sending (section 5).
4. Timer endpoints (/api/v1/timing)
GET /runs: runs open for timing (yesterday to tomorrow, not yet finalised).- Response:
{"runs": [{"id", "date", "status", "season", "location", "route_image_url", "route_gpx_url"}]}. - Pick the run whose
dateis today (local time). Otherwise offer "Open tonight's run".
- Response:
POST /runs/tonight: opens today's run in the current season, or returns it if it is already open.- 201 if new, 200 if it already existed. Same run shape as above.
- 409 with a
messageif there is no season or tonight is already finalised.
GET /runs/{id}/roster: everyone the timer can check in, people seen recently first.- Response:
{"runners": [{"id", "name", "public_id", "predicted", "last_seen", "recent"}], "attendance_types": [{"slug", "name", "is_volunteer"}]}. predictedis the average of their last four timed runs, in seconds. Use it for the pace label and for sorting. Search by the start of each word ("adam ca" finds Adam Cason).
- Response:
GET /runs/{id}/state: the whole run, built from every event on the server. Poll it every 3 to 4 seconds.- Send
If-None-Matchwith the lastETagyou got. A 304 means nothing changed. If there is noETag, poll without the header. - Response:
{ "run": {"id", "date", "status"}, "gun": {"uuid", "at"} | null, "end": {"uuid", "at"} | null, "people": [{ "ref": "id:4" | "new:k1", "runner_id": 4 | null, "name", "checked_in_at", "late", "started_at", "finished_at", "seconds", "source": "timer" | "self_reported" | null, "attendance": "walked" | "pubbed" | "timed" | ... | null, "back_at", "predicted", "estimated", "run_number", "milestone", "course_best", "events": {"checkin", "start", "outcome", "back"} }], "placeholders": [{"uuid", "finished_at", "seconds"}], "counts": {"checked_in", "finished", "attendance", "placeholders"}, "server_time" }- A person is a runner when
attendanceis null.walkedis a walker,pubbedis pub only, andtimedis the timer. secondsis their time, or null if they have none.milestoneis true when tonight is a 50th, 100th and so on run;run_numberis that number.course_bestis their best time at this location. A new time below it is a course PB.eventsgives the UUID of the event behind each part of the person's state. Use these UUIDs to undo things (section 5).placeholdersare finishes nobody has been named for yet.
- A person is a runner when
- Send
GET /join-code: returns{"code": "1901"}. Show it large, with the join link/timer/?join=CODE.PATCH /join-code {"code": "PINTS"}: a host changes the code: 3 to 12 letters or numbers, saved in upper case.- Returns
{"code": "PINTS"}. It applies from the next phone that joins and stays until someone changes it again. Phones that already joined keep working. - A bad code gets 422. Someone timing with only a one-night link gets 403.
- Returns
POST /runs/{id}/events: see section 5.POST /runs/{id}/finalise {"force": false}: builds draft results from the events.- On success:
{"finalised": true, "timed", "untimed", "placeholders", "newRunners", "skipped": [...], "unresolved": []}. - If someone is checked in with no time or attendance: 422 with
"unresolved": ["Name", ...]. Offer to send{"force": true}, which records them as "ran, no time". - After finalising, the run is closed. Events and state are refused (404 or 409), so show a "results built" screen and stop polling. A host checks and publishes the run in the site's admin, and any later fixes happen there.
- On success:
5. Events: how every tap is saved
Every timer action is an event. Send events with POST /api/v1/timing/runs/{id}/events:
{"events": [{"uuid": "<made on the phone>", "type": "finish", "payload": {"runners": [{"runner_id": 4}]}, "client_at": "2026-11-30T08:12:03.250Z", "device_id": "timer-<random>"}]}
uuid: generate it on the phone withcrypto.randomUUID(). Resending the same event is safe, because it is stored once.client_at: when the tap happened, in ISO 8601 UTC, on the corrected clock. Every response withserver_timegivesoffset = Date.parse(server_time) - (sentAt + receivedAt) / 2; useDate.now() + offset. A participant's own late start is recorded on the server clock, so the two must agree.device_id: any label up to 64 characters.- Batches: up to 500 events a request. The response is
{"accepted": [...], "rejected": [...], "server_time"}. A rejected UUID was already used elsewhere, so drop it.
The queue (keep it working):
- Every event goes into a queue in
localStorage, keyed by run id, before it is sent. - The queue is sent straight away and on every poll. Only what the server accepted or rejected is removed.
- On a network error, the queue is kept and retried. Taps must never be lost.
- "N taps waiting to send" shows while the queue is not empty.
- On 422, the
errorskeys look likeevents.3.payload.runner_id. Drop only those events and show the message.
Optimistic display: the screen is drawn from the last server state with the queued events applied on top, so a tap shows at once. The next poll corrects it.
Runner references in payloads: {"runner_id": 4} for someone on the roster, or {"new": "k1"} for someone checked in tonight with new_runner.key = "k1".
| Type | Payload | Meaning |
|---|---|---|
checkin |
{"runner_id": 4} or {"new_runner": {"key": "k1", "first_name": "Nina", "last_name": "New", "email": "optional"}}, plus optional "estimate_seconds": 1800 (300 to 7200) |
They are here. New runners are created when results are built. |
attendance |
{"runner": ref, "type": "walked"} |
Walker (walked), pub only (pubbed), timer (timed). It replaces any time they had. |
gun |
{} |
The start. If there is more than one, the last counts. |
start |
{"runner": ref} |
A late arrival's own start. Their time counts from here. |
finish |
{"runners": [ref, ...]} |
Crossed the line, up to 10 together. [] means "someone, name later" (a placeholder). |
assign |
{"finish": "<placeholder uuid>", "runner": ref} |
Names a placeholder. |
self_reported |
{"runner": ref, "seconds": 1490} |
A time set by hand (300 to 7200). |
back |
{"runner": ref} |
A walker is back. |
end |
{} |
The run is over. |
void |
{"target": "<uuid>"} |
Undoes one event. A host can void any event from the site or from participants. |
A person's latest finish, self_reported time or attendance is the one that counts. A time is the finish minus their own start (when it is before the finish), or minus the gun, rounded down to whole seconds.
6. Screen actions and what they send
| Action | Request |
|---|---|
| Participant name search | GET /participant/roster?q= (at least 3 letters; at most 10 {"id", "name"}). Say "Type at least three letters" until then. |
| Participant checks in | POST /participant/checkin with {"runner_id": 4, "kind": "runner" | "walker" | "pub", "estimate_seconds": 1800}, or {"new_runner": {"first_name", "last_name", "email"}, "kind": ...} for someone new (email optional, with a line saying the club only uses it to send results). Returns their state (201). A 409 means already checked in or the name is taken: show the message. |
| Participant starts late | POST /participant/start. The server records the time. |
| Participant: "Not you?" | DELETE /participant/checkin. A 409 means they already have a finish. |
| Show the join code (timer) | GET /timing/join-code. |
| Change the join code (timer) | PATCH /timing/join-code with {"code": "PINTS"}. Pause polling while the host types, so the field is not redrawn. |
| Timer checks someone in | A checkin event, plus an attendance event for walker, pub or timer. Make up the new_runner.key (for example "k" + random). |
| Late runner's start (timer) | A start event. |
| Remove someone (timer) | void their events.back, events.start, events.outcome and events.checkin (whichever are set). |
| Route map and "GPX for your watch" | The run's route_gpx_url. |
| Start | A gun event. |
| Finish | A finish event with that runner. |
| Several finishing together | Select several people, then one finish event with all of them (up to 10). |
| Unchecked runner | finish with "runners": []. |
| Walker back | A back event. Toggle off with void of events.back. |
| Undo last | void the latest finish: the newest events.outcome among finished people, or the newest placeholder uuid. |
| End | An end event. |
| Resume | void the end event's uuid. |
| Fix a time | A self_reported event with the seconds. |
| Swap times | void both people's finish events, then send two new finish events at the original finished_at moments (as client_at) with the runners swapped. Keep anyone else who was in the same finish. |
| Change type | To walker, pub only or timer: an attendance event. Back to runner: void their attendance event (events.outcome). |
| Name an unchecked runner | An assign event from the placeholder to a checked-in runner with no time. |
| Rename anyone else | Not supported by the API. Names are fixed in the site's admin. |
| Build the results | POST /timing/runs/{id}/finalise, with force for "ran, no time". Then show the "results built" screen. |
Participant state (GET /participant/state)
Response:
{
"run": {"id", "date", "status", "location", "route_image_url", "route_gpx_url"},
"gun_at", "ended_at",
"me": {"kind": "runner" | "walker" | "pub", "name", "late", "started_at", "finished_at", "seconds", "back_at", "predicted", "run_number", "milestone", "course_best"} | null,
"counts": {"checked_in", "finished"},
"server_time"
}
meis null until they check in.- The "me" screen shows: the milestone line, walker and pub messages, "You finished" with the time, "Start my run" for a late arrival with no start, a running clock from
started_atorgun_at, the route map, and "Not you? Cancel check-in" (until they have a finish). - Poll every 4 seconds.
7. Developing and testing
- Never use the live site, pub-runners.com, for development.
- The pull request preview (
https://pr<number>.demo.pub-runners.com/timer/) is the real thing: your version of the app, the real sign-in, the security policy and join links, against made-up data. Test every change there before asking for review. - API reference: https://staging.pub-runners.com/api/docs (interactive) and https://staging.pub-runners.com/api/openapi.json (exact schemas). Staging has the same API with made-up data, and can be reset at any time.
- The live reference and guides: https://pub-runners.com/api/docs and https://pub-runners.com/developers. They are built from the release that is running, so they show what is live now.
- Quick work on your own machine (for example
http://localhost:5173): sign-in cookies do not work from another origin, so point the app at https://staging.pub-runners.com and send the demo API key instead (the club sends it with the demo password).- Send the key as
Authorization: Bearer pr_...on timer requests, with no XSRF header. - Keep the key outside the repo, in an environment variable, or in a file named
*.local.*insidepublic/timer(git-ignored). Never commit it. - A key sees only listed runners; hidden runners' names come back as
null. - If a signed-in host has already sent events for a run, the key's events for that run get 409 ("Another app is already timing this run"). Open your own run, or ask the club to reset staging.
ETagis readable from other origins, soIf-None-Matchpolling works from localhost.- Participant requests use their own bearer token, so they work from localhost too.
- Send the key as
- Local work never replaces the preview check: the CSP and the cookie sign-in only apply on the site.
8. Known gaps
These are not built yet. Work on them only when asked.
- A QR code for the join link (the timer shows the code and the link).
- A service worker for offline install.
- Keeping PB alerts across a page reload.
- Renaming anyone except an unchecked runner (needs an API change first).
9. Before asking for review
Check on the pull request preview, on a phone-sized screen:
- CI passes, and the browser console shows no security-policy violations at
/timer/. - No key, password or token is in any committed file.
- What you changed works, and these still work:
- A signed-in host opens tonight's run, shows the code and link, changes the code, checks people in (a new runner with an estimate, a walker and a pub-only person), starts, and records single, group and unnamed finishes.
- Late starts, walkers back, undo, end, fixes (assign, swap, set time, change type, remove), resume, and building results, including "ran, no time".
- On a second phone, a participant joins by code and by link, searches (3+ letters), checks in as a runner, walker, pub only or someone new, starts late, cancels, and sees their time at the finish.
- With the network cut, finish taps show at once, survive a reload, and are sent with their original times when the network returns.
- Milestone and course-PB alerts appear.
- The pull request description says what changed and how you tested it.