跪拜 Guibai
← All articles
Performance Optimization · Frontend · Vue.js

Optimize Paginated Lists: Cache Promises, Patch Rows, and Cut Count Queries

By Setsuna_F_Seiei ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

List pages are the backbone of admin interfaces; these optimizations reduce backend load and make pages feel snappier without complicating the user experience. The promise-caching and stale-response techniques transfer directly to any frontend-backend stack.

Summary

Admin dashboards are full of list pages that hammer the backend with repeated requests, reload the whole view after a single edit, and recompute total counts on every page flip. A new article breaks these problems into three separate optimizations. First, it shows how to cache paginated responses at the request layer: store the promise instead of the resolved data, so concurrent requests for the same page share one network call. It also cancels in-flight duplicates with AbortController, and uses a sequence number in a Vue composable to ignore stale responses when the user flips pages quickly.

Second, after a detail edit, the list can patch the changed row in place without refetching the whole page. The trick is to only trigger a silent reload when the edited fields affect sorting, filtering, or totals, like status, amount, or createdAt. The article provides a composable and a patch helper that keep the list consistent without a full refresh.

Third, the article tackles the total count query. A withTotal parameter lets the frontend request the count only when needed, and the backend can skip the count query otherwise. Counts get a 30-second cache with invalidation on writes, and for large tables, PostgreSQL's reltuples can give an approximate count that's good enough for display lists. The result is a set of concrete, copy-paste-ready patterns that cut backend load and improve perceived performance.

Takeaways
— Cache the response promise, not the resolved data, so concurrent requests for the same page share a single network call.
— Use AbortController to cancel duplicate in-flight requests, with keys built from method, url, params, data via JSON.stringify.
— A sequence number in the composable ignores stale responses when pages change quickly.
— After a detail edit, patch the row locally and only reload the current page if fields like status, amount, or createdAt change.
— Make total count optional via a withTotal query parameter; the backend skips the count query when false.
— Cache total counts with a 30-second TTL and invalidate on writes or filter changes.
— PostgreSQL's reltuples provides an approximate count for display-only lists.
Conclusions

Caching the promise rather than the resolved value is a subtle but powerful way to coalesce duplicate requests; it saves a network call even when the first request hasn't finished.

The article's row-patch approach correctly distinguishes display-only fields from fields that affect ordering, but it pushes the responsibility onto the developer to know which fields matter. A schema or metadata-driven solution could automate this decision.

Approximate counts are pragmatic for large datasets, but the article doesn't discuss communicating the margin of error to users; product owners need to set expectations.

From the discussion
Featured comments
黎大仙

Aren't you killing real-time updates this way? The browser already supports ETag; if you want faster front-end fetches, you could add ETag computation and let the browser use cached data directly. You can't just cache data on the front end—when data updates, you won't get it, and then you have to consider stale time, making it more and more complicated.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗