All work

The Hershey Company, July 2026 to now

An offline-first app for field reps who work all day without signal

NGS-RSC (Retail Store Companion) is the app Hershey’s field sales reps use on iPads inside stores. Many work a full day with no network and only sync in the evening. I designed and built its offline architecture, and I’m the top committer among the ten engineers on the project.

Role
Senior engineer, owner of the offline architecture
Users
1,200–1,500 field reps
Status
Production pilot with reps
Stack
React, TypeScript, Workbox, IndexedDB, NestJS, Prisma

Do a store visit with no network

Store visit

Changes saved on this device0 waiting

    Tap a step. It updates instantly, even offline.

    Offline. Tap the steps, then switch the network back on.

    Context

    A rep plans the day in a sibling routing app, then opens RSC for each store and works through four stages: a pre-visit brief with embedded Power BI insights, an assessment, planning and executing actions, and a recap.

    When I joined, the app had no offline support at all. A visit closed without signal was simply lost, which was the single biggest risk for the iPad pilot.

    Two constraints shaped everything: the routing app and RSC share one Postgres database with separate migration histories, and the two apps run on different origins and deploy independently.

    Offline architecture

    I split offline support into two layers, each doing the job it’s good at.

    Layer 1: the service worker

    • the app shell is precached, so the app opens with no network at all
    • data reads try the network first and fall back to the last good response after five seconds
    • mutable routes are never cached, which removed a whole class of stale-data bugs

    Layer 2: the query cache and the mutation queue

    • visit state and notes are persisted to IndexedDB, so queued work survives the app being closed
    • one shared helper updates every affected screen the moment a rep taps, and rolls back if the server later rejects the change
    • queued changes replay one at a time, oldest first, so the server never sees “end visit” before “start visit”
    • the app flushes the queue to disk before navigating away, and queued changes are exempt from cache expiry
    • the server’s end-visit endpoint is idempotent, so a replay that runs twice is a harmless no-op

    Connectivity comes from a short health-check probe rather than the browser’s online flag, because Safari on iPad reports “online” on captive or stalled networks.

    Key decisions

    Two offline layers
    Instead ofOne hand-written service worker holding everything
    WhyMutations need optimistic updates and typed rollbacks at the React level. Keeping the worker generic also means it can be extracted into a shared library.
    A health-check probe for connectivity
    Instead ofThe browser’s navigator.onLine
    WhyOn iPad Safari it says “online” on captive or stalled networks. A probe with a short timeout tells the truth.
    One global queue, replayed in order
    Instead ofPer-visit queues, or replaying in parallel
    WhyA rep can only have one active visit at a time, so ordering across visits matters. Parallel replay was getting end-before-start requests rejected.
    Save instantly with “saved on this device”
    Instead ofA holding screen until the queue drains
    WhyReps do several visits a day and get signal only in the evening. A blocking screen would stall the whole route.
    Build the recap from data already on the device
    Instead ofThree extra server calls to build the recap
    WhyThe on-device lists are already updated at tap time, so the recap works fully offline and has one source of truth.
    Never put offline fixes behind feature flags
    Instead ofGating them like every other feature
    WhyFlags fail closed when offline, which would switch the fix off exactly when it’s needed.

    Hard bugs

    A visit that disappeared on Save

    A visit started and finished offline vanished when the rep tapped Save. The app queued “end visit” and immediately redirected to the routing app, so the write to disk never ran. On reopen, the queue replayed out of order and the server rejected the end. The fix combined four changes: flush before leaving, ordered replay, no expiry for queued changes, and an idempotent end-visit endpoint.

    Three offline bugs with one cause

    Actions went missing, didn’t complete, and blocked the Next button, but only offline. All three came from screens refreshing only after the server confirmed a change, and offline the server never does. One shared helper that updates the screens at tap time closed all three.

    Sign-in banners that shouldn’t appear

    Reps saw “Sign in again” about 86 minutes into a session, even though token refresh existed. Power BI returns 403 for an expired token and 401 for missing access, the reverse of what the code assumed, so refresh never fired. The server now reads Power BI’s own error header, and the client tells “sign in again” apart from “ask your admin”.

    A failure inside someone else’s iframe

    A few reps lost an AI-generated narrative that only appears inside the embedded report. I ruled out the obvious causes by test rather than assumption, then added diagnostics and logging so the next comparison is based on evidence instead of guesses.

    Performance under load

    A 500-user load test produced 16 HTTP 500 errors. I traced all 16 to one root cause: under load the database connection pool filled up, ordinary queries waited and succeeded, but transactions gave up after a two-second default wait.

    • I shipped a fix that gives transactions a realistic wait budget and one retry on that specific error, verified against more than 1,300 server unit tests
    • I prepared a second fix with two indexes, backed by query plans showing a full table scan that slowed from about 80 ms at rest to around a second under load
    • I sized the real constraint as a capacity model: pool size, times instances, times both apps sharing the database, against the database’s connection limit

    Platform and developer experience

    Before features could ship, the app needed a working path to production. I built the deploy pipeline, an opt-in rollback job, feature flags and an end-to-end test coverage harness, then contributed the reusable pieces back to Hershey’s shared project template. Three projects now use them.

    The first deploy was blocked by an identity setting outside the repo: tokens were issued in an older format than the auth layer expected. Finding it took evidence from the deploy logs, and it’s now documented so nobody loses a week to it again.

    I also made onboarding easier: a new engineer can create a personal database schema, load a safe, repeatable seed, and test every feature from one written plan.

    Results

    MeasureBeforeAfter
    Visits closed fully offlineLost (reproduced)0 lost in testing
    App launch with no networkBlank pageOpens from cache
    Offline action bugs3 openClosed by one shared helper
    Recap server calls per visit30 for the current visit
    Unneeded sign-in banners after token expiryEvery timeSilent refresh
    HTTP 500s in a 500-user load test16, one root causeFix shipped, re-test pending
    Deploy, rollback and test toolingThis app onlyReused by three projects

    The offline architecture, the deploy pipeline and the shared response format for read endpoints were designed and built by me. Most visit-stage features were built against product tickets, with teammates extending several of them since.

    How I work on it

    I use Claude Code as a coding agent with written project memory. Each story’s non-obvious decisions, rejected options and environment gotchas are recorded and reused in later sessions. The agent proposes and implements; it never deploys or merges on its own, and every merge request is still reviewed.

    Want to go deeper on this? Let’s talk.