Architecture
Architecture
Layers, data flow and rendering strategy — how a keystroke becomes a number on the receipt.
Splitmize is a Next.js 16 App Router application with no backend. The interesting code is a handful of pure functions; everything else is a thin, carefully styled shell around them.
Layers
┌───────────────────────────────────────────────────────────────┐
│ app/[locale]/… Routes, metadata, static generation │
├───────────────────────────────────────────────────────────────┤
│ features/ Screens and their state │
│ workbench/ the app at /split (steps, receipt, header)│
│ viewer/ the shared bill at /s │
│ landing/, docs/ marketing page and documentation │
│ bill/store.ts external store: undo, autosave, history │
│ components/ Site chrome: header, footer, toggles │
├───────────────────────────────────────────────────────────────┤
│ design-system/ Tokens, base styles, components, Storybook│
│ (React only: no next/*, no store) │
├───────────────────────────────────────────────────────────────┤
│ lib/ Pure TypeScript, no React │
│ money, allocate integer money and exact splitting │
│ bill/* model, reducer, engine, settlement │
│ pix, share-link BR Code payloads, URL encoding │
│ storage, docs localStorage wrapper, Markdown loader │
├───────────────────────────────────────────────────────────────┤
│ i18n/ Locale config, dictionaries, translator │
└───────────────────────────────────────────────────────────────┘
Dependency rule: arrows only point down. lib/ never imports from features/ or React. design-system/ never imports from next/*, the store or features/, so every component runs in Storybook on plain Vite and stays reusable. This is also what lets the engine be tested in plain Node and reused by the server-rendered landing page (whose hero receipt is a real bill computed at build time).
The app imports UI only from @/design-system. Feature folders compose those components and add layout; they don't restyle them. Details: design system.
Data flow
user input ──► dispatch(action) ──► billReducer(bill, action) ──► new Bill
│
┌───────────────────────────────────────┤
▼ ▼
getBreakdown(bill) (memoized by identity) scheduleSave() ─► localStorage
│ (debounced 250 ms)
▼
components re-render via useSyncExternalStore selectors
- A component calls
dispatch({ type: 'item/add', … }). - The store runs the pure
billReducer. If the result is the same object, nothing happens — reducers return the original reference for no-op actions on purpose. - The previous bill is pushed onto the undo stack (text edits to the same field within 1.2 s collapse into one step).
- Subscribers re-render. Components select narrow slices (
s => s.bill.people), so typing in the title doesn't re-render item cards. getBreakdown/getSettlementrecompute only when the bill object changes.- A debounced write persists the bill and updates the history index. It also flushes on
pagehideand when the tab is hidden.
Details: state and persistence.
Rendering strategy
| Route | Rendering | Why |
|---|---|---|
/, /en, /es | Static (SSG), Server Components | Indexable, fast, almost no JS. The hero receipt is computed at build time. |
/docs/** | Static, Server Components | Markdown rendered at build time. Only the sidebar filter is a client component. |
/split | Static shell + client app | The bill lives in localStorage, which only exists in the browser. A skeleton renders until the store loads. |
/s | Static shell + client | The bill is in the URL fragment, which the server never sees. Marked noindex. |
/[locale]/opengraph-image | Generated at build | Social preview with the same receipt motif. |
Fonts (Bricolage Grotesque and Chivo Mono) are self-hosted through next/font. The design tokens are generated once from TypeScript (tokensToCss()) and inlined in a <style> tag by the root layout, so there is no token stylesheet to fetch. The theme is applied by a tiny inline script before first paint, so dark-mode users never see a light flash.
Routing and locales
The default locale (Portuguese) has no prefix; English and Spanish live under /en and /es. src/proxy.ts (Next 16's name for middleware) rewrites unprefixed paths to /pt/…, redirects /pt/… to the canonical unprefixed URL, and honours an explicit language choice stored in a cookie. Everything else is in internationalization.
Runtime dependencies
| Package | Why it's here |
|---|---|
next, react, react-dom | The framework. |
marked | Markdown → HTML for these docs, at build time only. |
uqr | QR codes for Pix, loaded lazily when a QR is opened (~10 kB). |
sql.js | Reads Splitmize 1.1's SQLite history once, only on devices that have it. See migration. |
@vercel/analytics | Privacy-friendly page-view analytics (no cookies). |
Development-only tooling: TypeScript, ESLint, Prettier, Sass, Vitest (+ jsdom and Testing Library for component tests) and Storybook 9 with the docs, a11y and themes addons.
Version 1 shipped html2pdf.js, framer-motion, uuid, three i18n libraries, nodemailer and a webpack config Next never used. Each removal is explained in the decision log.