| 1 | # Murmur on the web |
| 2 | |
| 3 | A single-page app for the Murmur API: React and TypeScript, built by |
| 4 | Vite into static files that nginx serves next to the API. |
| 5 | |
| 6 | ## Run it |
| 7 | |
| 8 | ```sh |
| 9 | make up && make seed # the whole system, and 100,000 users |
| 10 | make web # build into web/dist, served on :8088 |
| 11 | make web-dev # or: http://localhost:5173, with hot reload |
| 12 | ``` |
| 13 | |
| 14 | In that order: until `make web` has run, `web/dist` is empty and |
| 15 | port 8088 answers 404. A new build needs no restart of nginx. |
| 16 | |
| 17 | Sign in with a seeded key, `seed-token-N` for `userN` (try 16726, a |
| 18 | quiet account, or 363, who follows 199), or create an account. |
| 19 | |
| 20 | `npm test -- --run` runs the tests once, `npm run lint` and |
| 21 | `npm run build` check the rest, `npm run format` formats. |
| 22 | |
| 23 | ## How it is organised |
| 24 | |
| 25 | ``` |
| 26 | src/ |
| 27 | main.tsx the query client and the router |
| 28 | routes.tsx every route, in one place |
| 29 | index.css Tailwind, the design tokens, four small classes |
| 30 | lib/ no React here |
| 31 | api.ts request(), the session, ApiError, response types |
| 32 | stream.ts reads GET /live: a parser, and reconnects |
| 33 | toast.ts a tiny store for toasts |
| 34 | time.ts "3 hr. ago" |
| 35 | components/ |
| 36 | Layout.tsx navigation, the right rail, the Live line |
| 37 | Settings.tsx theme and sign out |
| 38 | ui/ Avatar, Notice, More, Toasts |
| 39 | features/ |
| 40 | auth/ welcome, sign up, sign in |
| 41 | feed/ home, composer, posts and likes |
| 42 | profile/ profile, follow, who to follow |
| 43 | notifications/ the list and the unread count |
| 44 | live/ the stream, turned into cache updates |
| 45 | ``` |
| 46 | |
| 47 | ## Decisions, and why |
| 48 | |
| 49 | - **One origin.** The app calls `/api/…`. Vite's dev server and, in |
| 50 | production, nginx (`deploy/nginx.conf`, port 8088) pass it to the |
| 51 | API. No CORS, and the API's own port 8080 stays as it was. |
| 52 | - **The stream is `fetch`, not `EventSource`**, because `EventSource` |
| 53 | can't send the bearer token. `lib/stream.ts` parses the events, |
| 54 | waits 1, 2, 4… up to 30 s after a break (or what `Retry-After` |
| 55 | says), and sends `Last-Event-ID` to get what it missed. |
| 56 | - **The stream feeds the cache, so nothing polls.** A like moves the |
| 57 | badge, the post's count and shows a toast. A post by someone you |
| 58 | follow adds to an "N new posts" pill instead of moving the page. |
| 59 | - **The key and its handle are kept together**, so the shell never |
| 60 | waits to know who you are, and starting costs one request less. |
| 61 | - **Rate limits are 60 requests a minute.** Reads stay fresh for a |
| 62 | minute, nothing refetches on window focus, 4xx answers are not |
| 63 | retried, and a 429 waits for `Retry-After` twice, saying so |
| 64 | quietly; after that the screen shows the error. |
| 65 | - **Likes are optimistic and not refetched.** The worker adds likes up |
| 66 | once a second and reads may come from a replica, so a refetch right |
| 67 | after a like would show the old count. A failed like is put back. |
| 68 | - **Follow shows what you asked for while it's sent** (TanStack |
| 69 | Query's "via the UI" way), then refetches: follower counts are |
| 70 | written at once, and your own reads go to the primary. |
| 71 | - **Tokens, not `dark:`.** Colours are theme variables; dark mode |
| 72 | gives them other values. Components use `bg-paper`, `text-mute`… |
| 73 | - **Long class lists** break over lines inside the string; a style |
| 74 | used everywhere on one element is a class in `index.css` (`.btn`, |
| 75 | `.field`, `.bar`); anything bigger is a component, as Tailwind's |
| 76 | docs suggest. |
| 77 | - **No avatars from the internet**: `Avatar` draws a pattern from the |
| 78 | handle. The font is Inter, bundled by Fontsource. |