Next.js Discord

Discord Forum

Why it will fetch data every refresh instead of cached?

Unanswered
Bloodhound posted this in #help-forum
Open in Discord
BloodhoundOP
export async function fetchFilteredInvoices(
  query: string,
  currentPage: number,
) {
  console.log(query, 'fetchFilteredInvoices');
  // noStore();
  const offset = (currentPage - 1) * ITEMS_PER_PAGE;

  try {
    const invoices = await sql<InvoicesTable>`
      SELECT
        invoices.id,
        invoices.amount,
        invoices.date,
        invoices.status,
        customers.name,
        customers.email,
        customers.image_url
      FROM invoices
      JOIN customers ON invoices.customer_id = customers.id
      WHERE
        customers.name ILIKE ${`%${query}%`} OR
        customers.email ILIKE ${`%${query}%`} OR
        invoices.amount::text ILIKE ${`%${query}%`} OR
        invoices.date::text ILIKE ${`%${query}%`} OR
        invoices.status ILIKE ${`%${query}%`}
      ORDER BY invoices.date DESC
      LIMIT ${ITEMS_PER_PAGE} OFFSET ${offset}
    `;

    return invoices.rows;
  } catch (error) {
    console.error('Database Error:', error);
    throw new Error('Failed to fetch invoices.');
  }
}

11 Replies

BloodhoundOP
I thought cached is by default
Am I wrong?
@Bloodhound I thought cached is by default
you cant just create a function and expect it to be cached
in next.js either a function result is cached, or the whole page is cached
by default the whole page is cached but there are some part of code that disables that
and your code doesnt really indicates whether to opt-out page cache or to opt-in function cache
basically
need more info