Files
openmonetis/src/features/dashboard/fetch-dashboard-data.ts
Felipe Coutinho e32fb85006 perf(cache): migração para diretiva use cache do Next.js
Todas as queries cacheadas do dashboard migram de `unstable_cache` para
a diretiva `use cache` com `cacheTag` e `cacheLife({ revalidate: 3 })`.

Todas as páginas e o layout do dashboard passam a chamar `connection()`
para garantir renderização dinâmica. O root layout envolve os filhos em
`<Suspense>`. `next.config.ts` remove `turbopackFileSystemCacheForDev`
e adota `cacheComponents: true`.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-01 14:14:23 +00:00

65 lines
2.5 KiB
TypeScript

import { cacheLife, cacheTag } from "next/cache";
import { fetchDashboardAccounts } from "./accounts-queries";
import { fetchDashboardCategoryOverview } from "./category-overview-queries";
import { fetchDashboardCurrentPeriodOverview } from "./current-period-overview-queries";
import { fetchDashboardInvoices } from "./invoices-queries";
import { fetchDashboardNotes } from "./notes-queries";
import { fetchDashboardPayers } from "./payers-queries";
import { fetchDashboardPeriodOverview } from "./period-overview-queries";
async function fetchDashboardDataInternal(userId: string, period: string) {
const [
periodOverview,
accountsSnapshot,
invoicesSnapshot,
currentPeriodOverview,
categoryOverview,
pagadoresSnapshot,
notesData,
] = await Promise.all([
fetchDashboardPeriodOverview(userId, period),
fetchDashboardAccounts(userId),
fetchDashboardInvoices(userId, period),
fetchDashboardCurrentPeriodOverview(userId, period),
fetchDashboardCategoryOverview(userId, period),
fetchDashboardPayers(userId, period),
fetchDashboardNotes(userId),
]);
return {
metrics: periodOverview.metrics,
accountsSnapshot,
invoicesSnapshot,
billsSnapshot: currentPeriodOverview.billsSnapshot,
goalsProgressData: categoryOverview.goalsProgressData,
paymentStatusData: currentPeriodOverview.paymentStatusData,
incomeExpenseBalanceData: periodOverview.incomeExpenseBalanceData,
pagadoresSnapshot,
notesData,
paymentConditionsData: currentPeriodOverview.paymentConditionsData,
paymentMethodsData: currentPeriodOverview.paymentMethodsData,
recurringExpensesData: currentPeriodOverview.recurringExpensesData,
installmentExpensesData: currentPeriodOverview.installmentExpensesData,
topEstablishmentsData: currentPeriodOverview.topEstablishmentsData,
topExpensesAll: currentPeriodOverview.topExpensesAll,
topExpensesCardOnly: currentPeriodOverview.topExpensesCardOnly,
purchasesByCategoryData: currentPeriodOverview.purchasesByCategoryData,
incomeByCategoryData: categoryOverview.incomeByCategoryData,
expensesByCategoryData: categoryOverview.expensesByCategoryData,
};
}
/**
* Cached dashboard data fetcher.
* Uses "use cache" with tags for revalidation on mutations.
* Cache is keyed by userId + period, and invalidated via user-scoped tags.
*/
export async function fetchDashboardData(userId: string, period: string) {
"use cache";
cacheTag(`dashboard-${userId}`);
cacheLife({ revalidate: 3 });
return fetchDashboardDataInternal(userId, period);
}
export type DashboardData = Awaited<ReturnType<typeof fetchDashboardData>>;