Sub topic 3
Choosing a loading pattern
This chapter assumes you know the screen state model from earlier in the tutorial (loading, empty, error, success) and that you have fetched data asynchronously in a web or mobile app. You should be able to read short TypeScript and HTML snippets.
Once you decide a screen needs a loading state, you still have to pick how it looks. The four common patterns are spinners, skeleton screens, progress bars, and optimistic updates. Each fits a different kind of wait. A wrong choice makes a fast screen feel slow, or a slow one feel broken.
What the pattern has to communicate
A loading indicator answers up to three questions for the user:
- Is something happening, or is the app frozen?
- What is going to appear?
- How long will it take?
A spinner answers only the first question. A skeleton screen answers the first two. A progress bar answers the first and third. An optimistic update makes the questions moot, because the result appears at once. Choose the pattern whose answers match what the user needs to know.
Four properties of the wait decide which pattern fits:
- Expected duration. Most requests finish in well under a second. Some take several seconds. A few take minutes.
- Whether progress is measurable. A file upload knows how many bytes are sent. A search query does not know how far along it is.
- Whether the layout is predictable. A list of cards has a known shape before the data arrives. A free-form report may not.
- Whether the user just did something. A tap on "Like" is an action the user expects to succeed. A page navigation is a request for content.
Spinners
A spinner is a small animated icon that shows work is in progress. It makes no claim about what is loading or how long it will take. That makes it the most general pattern and the weakest one.
Use a spinner when:
- The wait is short, roughly one to three seconds.
- The result has no predictable shape, such as a confirmation or a search with unknown result count.
- The area is small, such as a button or a single field.
Spinners work best when they are local. A spinner inside a "Save" button, with the button disabled, tells the user exactly what is busy. A full-page spinner tells them only that the whole screen is blocked.
Common failure modes:
- A spinner that never ends. If a request hangs, the spinner runs forever and the user cannot tell the difference between slow and stuck. Always set a timeout and move to the error state when it expires.
- A full-screen spinner over content the user could already use. If part of the screen has loaded, show it. Block only the part that is waiting.
- Stacked spinners. Five spinners on one screen look chaotic and give no sense of order. Prefer one indicator for the region, or switch to skeletons.
Skeleton screens
A skeleton screen is a placeholder that mimics the layout of the content, using grey blocks or lines where text and images will appear. It tells the user what is coming and keeps the layout stable.
Use a skeleton when:
- The layout of the result is known in advance: lists, cards, tables, profile pages.
- The wait is long enough to notice, typically over a few hundred milliseconds.
- The page is loaded for the first time, so there is nothing else to show.
Skeletons help for two reasons. They show the shape of the page, so the eye can settle before content arrives. They also reduce layout shift, because the placeholders reserve space that the real content then fills.
Common failure modes:
- The skeleton does not match the content. If the skeleton shows three rows and the response has twelve, or the real rows have a different height, the page jumps when the data arrives. That jump is the problem the skeleton was meant to prevent. Match heights, and show a plausible count.
- Skeletons for tiny waits. If the data arrives in 150 ms, the skeleton appears for a moment and vanishes. This looks like a flash. Timing rules for this are covered in the chapter on delays and flicker.
- Skeletons for unpredictable content. If you cannot say what the page will look like, a skeleton is a guess. Use a spinner instead.
- Skeletons on refresh. When the user refreshes a screen that already has content, replacing it with grey blocks discards information they could still read. Keep the old content visible and show a small indicator that it is updating.
The skeleton also needs a non-visual equivalent. The HTML below marks the list as busy and keeps a status message for screen readers. The accessibility chapter covers this in more depth.
<div role="status" aria-live="polite">Loading projects…</div>
<ul aria-busy="true">
<li class="skeleton-row"></li>
<li class="skeleton-row"></li>
<li class="skeleton-row"></li>
</ul>Progress bars
A progress bar shows how much of a task is done. It answers the question of how long the wait will be, which neither spinner nor skeleton does.
Use a progress bar when:
- The task is long, usually more than a few seconds.
- The app can measure progress honestly: bytes uploaded, files processed, steps completed.
- The user is likely to leave and come back, or to decide whether to wait.
There are two kinds:
- Determinate shows a real fraction, from 0 to 100 percent.
- Indeterminate shows a moving bar with no fraction. It behaves like a spinner in a different shape, and it should be treated as one.
The native HTML element supports both. With a value it is determinate. Without one it is indeterminate.
<label for="upload">Uploading report.pdf</label>
<progress id="upload" value="40" max="100">40%</progress>Common failure modes:
- A fake progress bar. A bar that fills on a timer, unrelated to real work, reaches 90 percent and sits there. Users notice, and they stop trusting the bar. If you cannot measure progress, use an indeterminate indicator.
- A bar that goes backward or restarts. This happens when a task has several stages and each stage resets the bar. Either show one overall percentage or show the stages as steps ("Step 2 of 3").
- No way out. A long task should offer a way to cancel, or to continue working while it runs. A bar with no exit turns a wait into a trap.
For waits over about ten seconds, add text that says what is happening ("Resizing 12 of 40 images") and, if you can estimate it, the time remaining. Only show a time estimate if you can make it reasonably accurate. A wrong estimate is worse than none.
Optimistic updates
An optimistic update shows the result of an action immediately, before the server confirms it, and corrects the screen if the server rejects it. The user sees no wait at all.
Use it when:
- The user triggered a small, frequent action: like, star, check off a task, reorder, rename.
- The action almost always succeeds.
- The action is easy to undo, so showing a wrong result briefly does little harm.
Do not use it when:
- The action has real-world consequences that are hard to reverse, such as payments, sending a message to another person, or deleting an account.
- The server often rejects the action because of validation, permissions, or conflicts.
- The result depends on server-generated data you cannot predict, such as a new record's final ID or a computed total.
The core of the pattern is three steps: save the previous state, show the new state, and restore the previous state if the request fails.
type Item = { id: string; starred: boolean };
async function toggleStarOptimistic(
items: Item[],
id: string,
save: (id: string, starred: boolean) => Promise<void>,
render: (items: Item[]) => void,
notifyError: (message: string) => void
): Promise<Item[]> {
const target = items.find((item) => item.id === id);
if (!target) return items;
const previous = items;
const next = items.map((item) =>
item.id === id ? { ...item, starred: !item.starred } : item
);
render(next); // Show the result immediately.
try {
await save(id, !target.starred);
return next;
} catch {
render(previous); // Roll back to what the server still has.
notifyError("Couldn't update the star. Try again.");
return previous;
}
}The failure path is the part that most often gets skipped. When the rollback happens, the user has already seen the success. Handle it with care:
- Tell the user. A silent rollback looks like a bug. Show a short message that says what did not save.
- Keep the rollback small. Reverting one star is fine. Reverting a whole form the user thought was saved is not.
- Handle overlapping actions. If the user taps twice quickly, you have two requests in flight. Restoring a stale snapshot can undo the later action. Queue the requests, or disable the control until the first one settles.
- Mark unsaved state when it matters. For actions that take longer, a subtle "Saving…" or "Saved" marker lets users know what the server has confirmed.
Comparing the four patterns
| Pattern | Best for | Needs | Main risk |
|---|---|---|---|
| Spinner | Short waits, small areas, unknown result shape | Nothing | Hangs without a timeout; says nothing about content |
| Skeleton | First load of predictable layouts | Known layout | Layout jumps when content differs from the skeleton |
| Progress bar | Long tasks such as uploads and exports | Measurable progress | Fake or inaccurate progress destroys trust |
| Optimistic update | Small, reversible user actions | Reliable success and a rollback path | Silent or conflicting rollbacks |
A decision flow
The diagram turns the criteria above into a sequence of questions. Treat it as a starting point and check it against real timings in your app.
The same logic can be written as a function. It is useful in a design system, where a shared component picks a default and the team overrides it on purpose.
type WaitProfile = {
expectedMs: number;
progressKnown: boolean;
layoutKnown: boolean;
reversible: boolean;
userAction: boolean;
};
type LoadingPattern =
| "none"
| "spinner"
| "skeleton"
| "progress"
| "optimistic";
function pickPattern(wait: WaitProfile): LoadingPattern {
if (wait.userAction && wait.reversible && wait.expectedMs < 1000) {
return "optimistic";
}
if (wait.progressKnown && wait.expectedMs >= 2000) {
return "progress";
}
if (wait.expectedMs < 300) {
return "none";
}
if (wait.layoutKnown) {
return "skeleton";
}
return "spinner";
}
console.log(
pickPattern({
expectedMs: 800,
progressKnown: false,
layoutKnown: true,
reversible: false,
userAction: false,
})
); // "skeleton"The thresholds (300 ms, 1000 ms, 2000 ms) are starting values, not standards. Measure your own requests on slow connections and adjust.
Combining patterns
Real screens often use more than one pattern, each in its own region.
- A dashboard can show skeleton cards on first load, then update each card with a small spinner when the user changes a filter.
- An upload screen can show a determinate progress bar for the file, then a spinner while the server processes it, because processing has no measurable progress.
- A task list can use optimistic updates for checking off items and a skeleton for the initial list.
Keep the rule that one region has one indicator at a time. When a region moves from skeleton to content, remove the skeleton. When it moves to an error, remove the indicator and show the error state in the same place, so the user does not hunt for what happened.
Slow connections and long waits
Patterns that work at 200 ms can fail at 20 s. Plan for the slow case:
- After a threshold, such as 8 to 10 seconds, change the message from "Loading…" to something more honest, such as "Still working. This is taking longer than usual."
- At a hard timeout, move to the error state with a retry action.
- Keep any content you already have on screen. Stale data with an "updating" marker is more useful than an empty screen.
Summary of choices
- Choose by the shape of the wait, not by taste or by what the framework gives you by default.
- Prefer the pattern that tells the user the most that you can say truthfully.
- Do not show progress you cannot measure, and do not show a layout you cannot predict.
- Plan the failure path for every pattern, especially optimistic updates.
Exercise
A "Mark as read" button on an email list updates one flag. The request usually takes 200 to 400 ms and almost never fails. Which loading pattern fits, and what must you build alongside it?
Show answer
An optimistic update. The action is small, frequent, reversible, and reliable. You must also build the rollback path: keep the previous state, restore it if the request fails, and tell the user that the change did not save. Handle repeated taps so a stale snapshot does not undo a later action.
Exercise
A reports page loads a table of 25 rows from a slow endpoint that takes 2 to 4 seconds. The table layout is fixed. Which pattern would you pick for the first load, and which two mistakes would make it fail?
Show answer
A skeleton screen, because the layout is known and the wait is long enough to notice. It fails if the skeleton's row count or row height differs from the real table, which makes the page jump when data arrives, or if you keep showing it with no timeout so a hung request looks like endless loading.
Exercise
A video export runs for about 90 seconds, but the service reports only "started" and "finished". Can you use a determinate progress bar? What do you show instead?
Show answer
No. A determinate bar needs real measured progress, and a timer-driven bar would be fake and lose trust. Show an indeterminate indicator with text describing the task ("Exporting your video…"), a note that it may take a minute or two, and a way to cancel or keep working. Add a timeout that leads to an error state with a retry.