From 62b58918d0e638a89e82951af512ada5d7ad4b44 Mon Sep 17 00:00:00 2001 From: Jakub Knetl Date: Sat, 30 Dec 2023 14:56:43 +0100 Subject: [PATCH] Chapter 8 - use dynamic rendering and simulate timeout --- app/lib/data.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/lib/data.ts b/app/lib/data.ts index 4c699a3..f7d09f6 100644 --- a/app/lib/data.ts +++ b/app/lib/data.ts @@ -9,21 +9,23 @@ import { Revenue, } from './definitions'; import { formatCurrency } from './utils'; +import {unstable_noStore as noStore} from "next/cache"; export async function fetchRevenue() { // Add noStore() here prevent the response from being cached. // This is equivalent to in fetch(..., {cache: 'no-store'}). + noStore() try { // Artificially delay a response for demo purposes. // Don't do this in production :) - // console.log('Fetching revenue data...'); - // await new Promise((resolve) => setTimeout(resolve, 3000)); + console.log('Fetching revenue data...'); + await new Promise((resolve) => setTimeout(resolve, 3000)); const data = await sql`SELECT * FROM revenue`; - // console.log('Data fetch completed after 3 seconds.'); + console.log('Data fetch completed after 3 seconds.'); return data.rows; } catch (error) { @@ -33,6 +35,7 @@ export async function fetchRevenue() { } export async function fetchLatestInvoices() { + noStore(); try { const data = await sql` SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id @@ -53,6 +56,7 @@ export async function fetchLatestInvoices() { } export async function fetchCardData() { + noStore(); try { // You can probably combine these into a single SQL query // However, we are intentionally splitting them to demonstrate @@ -92,6 +96,7 @@ export async function fetchFilteredInvoices( query: string, currentPage: number, ) { + noStore(); const offset = (currentPage - 1) * ITEMS_PER_PAGE; try { @@ -124,6 +129,7 @@ export async function fetchFilteredInvoices( } export async function fetchInvoicesPages(query: string) { + noStore(); try { const count = await sql`SELECT COUNT(*) FROM invoices @@ -145,6 +151,7 @@ export async function fetchInvoicesPages(query: string) { } export async function fetchInvoiceById(id: string) { + noStore(); try { const data = await sql` SELECT @@ -170,6 +177,7 @@ export async function fetchInvoiceById(id: string) { } export async function fetchCustomers() { + noStore(); try { const data = await sql` SELECT @@ -188,6 +196,7 @@ export async function fetchCustomers() { } export async function fetchFilteredCustomers(query: string) { + noStore(); try { const data = await sql` SELECT