A learning project exploring offline-first PWA architecture
Built with Next.js, Serwist, and TanStack Query
📖 Read the Tutorial »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
This project was born out of necessity while building a warehouse management system in a remote area of Kazakhstan. The internet there was unreliable or nonexistent, yet the app needed to be fully functional—allowing workers to scan products and tag locations without interruption.
I found plenty of resources on PWAs (Service Workers) and separate resources on State Management (TanStack Query), but very few on how to combine them effectively for a true "Offline First" experience.
This repository serves as that missing link: a reference implementation demonstrating how to merge Serwist (for app shell caching) and TanStack Query (for data persistence and mutation queueing).
Core Capabilities:
- App Shell Caching: Loads instantly even after a hard refresh without network (via Serwist).
- Data Persistence: Reads cached data immediately when offline (via TanStack Query Persister).
- Mutation Queueing: Queues actions (Create/Update/Delete) while offline and replays them automatically when connectivity is restored.
- Optimistic UI: Updates the interface immediately, treating offline actions as "success" until proven otherwise.
- Framework: Next.js (App Router)
- PWA/Service Worker: Serwist
- State Management: TanStack Query (with Persister)
- Storage: LocalForage (IndexedDB wrapper)
- Database: PostgreSQL (via Drizzle ORM)
- Styling: Tailwind CSS & Shadcn UI
To get a local copy up and running, follow these steps.
- Node.js (v18+)
- pnpm
npm install -g pnpm
- Docker (Optional, for running PostgreSQL)
- Clone the repo
git clone https://github.com/nomomon/offline-first-todo-app.git cd offline-first-todo-app - Install packages
pnpm install
- Set up environment variables. Create a
.envfile in the root directory:DATABASE_URL="postgresql://postgres:postgres@localhost:5432/todo_app" NEXTAUTH_SECRET="your_super_secret_key" NEXTAUTH_URL="http://localhost:3000" GITHUB_ID="" GITHUB_SECRET=""
- Start the database (using Docker Compose)
docker-compose up -d db
- Run database migrations
pnpm db:migrate
- Start the development server
pnpm dev
Once running, the application acts as a fully offline-capable PWA.
- Offline Simulation: Open the browser DevTools, go to the Network tab, and set it to "Offline".
- CRUD Actions: Create, edit, or delete todos. Notice the UI updates immediately.
- Sync: Re-enable the network. You will see the pending requests fire automatically to sync with the server.
Distributed under the MIT License.
Mansur Nurmukhambetov - @nomomon
Project Link: https://github.com/nomomon/offline-first-todo-app