Next.js Discord

Discord Forum

unstable_cache with paginated db query

Unanswered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
Would this be the correct implementation for a db call with pagination? I am guessing if I pass page and pageSize into the getCachedItems() function a different result will be cached when the user interacts with the pagination component. So I should do the pagination/slicing in getItems() function and not in the db query?
import "server-only";
const getCachedItems = unstable_cache(
  async () => {
    return await db.getItems();
  },
  ["items"],
  { tags: ["items"] },
);

const getItems = async (page: number, pageSize: number) => {
  const cachedItems = await getCachedItems();
  return paginated(cachedItems, page, pageSize);
};

0 Replies