TodoMVC
TodoMVC as a full-stack app: a Hono API and the React client run in one Vite dev server. The server keeps the todos in memory and answers after 400 ms, so you can see every change show up before it is saved.
Opens the example in StackBlitz in a new tab. The first start installs packages and takes a few seconds.
import { useSyncExternalStore } from "react";
import type { Todo } from "../../api/todos";
export type Filter = "all" | "active" | "completed";
/** TodoMVC keeps the filter in the hash: `#/`, `#/active` and `#/completed`. Back, reload and links keep it. */
const readFilter = (): Filter => {
const hash = window.location.hash.replace("#/", "");
return hash === "active" || hash === "completed" ? hash : "all";
};
const subscribe = (listener: () => void) => {
window.addEventListener("hashchange", listener);
return () => window.removeEventListener("hashchange", listener);
};
export const useFilter = () => useSyncExternalStore(subscribe, readFilter);
export const filterTodos = (todos: Todo[], filter: Filter) => {
if (filter === "active") return todos.filter((todo) => !todo.completed);
if (filter === "completed") return todos.filter((todo) => todo.completed);
return todos;
};
npx tiged BetterTyped/hyper-fetch/examples/apps/todomvc hyper-fetch-apps-todomvc && cd hyper-fetch-apps-todomvc && npm install && npm run dev- One project, two sides: Hono serves
/apiinside the Vite dev server, the client imports only the server's types (Hono fullstack). - Optimistic writes: add, toggle, rename, delete, toggle all and clear completed change the list at once (Optimistic updates).
- Rollback on failure: a rejected write puts the todo back, then core refetches the list from the server (Undo delete).
- Filters in the URL:
#/activeand#/completedfilter the cached list, so switching costs no request (Filters in the URL). - Save status:
useIsSubmittingand request events report every write in one place (CRUD).
How it works
-
server/app.tsis a Hono app with the TodoMVC routes under/api.vite.config.tsadds@hono/vite-dev-server, which sends every/apirequest to that app whileviteruns. Errors go out as{ message }. -
src/api/todos.tsimports theTodotype from the server withimport type, so both sides share it and no server code reaches the browser. The requests are typed by hand.HonoSchemafrom@hyper-fetch/honocan derive them from the routes instead, see Hono fullstack. -
Every write owns its optimistic change.
updateTodocovers both the checkbox and the rename, and its rollback puts the old todo back where it was:optimistic(getTodos).patch(params.todoId, payload).rollback(({ data, previous }) => restore(data, previous, Number(params.todoId))); -
createTodoinserts a todo with a temporary negative id and swaps it for the saved one in.commit(). While the id is negative,TodoItemdisables the todo. -
toggleAllTodosandclearCompletedTodoschange the whole list with.set()and have no rollback. If they fail, core refetches the list and the server's state replaces the change. -
The server stores every write, so core refetches
getTodosafter the last write settles. Delete a todo withcurl -X DELETE localhost:5173/api/todos/2, then toggle it in the app: the toggle fails with404, is undone, and the refetch removes the todo. -
useFilter()reads the filter from the URL hash withuseSyncExternalStore.TodoAppfilters the one cached list, so the counter and Clear completed always see every todo. -
SaveStatusshowsuseIsSubmitting(client)while writes are running, and the last error fromclient.requestManager.events.onResponse.
- Small full-stack apps and prototypes where the API lives next to the client.
- Lists of small changes that almost always succeed, such as todos, checklists and settings.
- As a starting point for a TypeScript monorepo that shares types between server and client.
Related
Hono runs inside Vite, and the client shares its Todo type through a type-only import. Every write carries its
optimistic change, core rolls back failures and refetches the list from the server, and the hash-based filters only
change what is rendered.
