Next.js Discord

Discord Forum

React's cache function in nextjs

Unanswered
Siamese Crocodile posted this in #help-forum
Open in Discord
Siamese CrocodileOP
Hi. I'd like to fetch the necessery data for my dynamic page /users when users is on the home page.
The code looks like this:

// page.tsx - home 

import { getCachedUsers } from "../users/_components/get-users";

export default async function Home() {
  const data = await getCachedUsers();
  console.log("HOME", data, "HOME");
  return <div />;
}


// page.tsx /users

import { getCachedUsers } from "./_components/get-users";

export default async function UsersPage() {
  const data = await getCachedUsers();
  console.log("USERS", data, "USERS");
  return <div />;
}


// get-users.ts 
export const getCachedUsers = cache(async () => {
  console.log("Fetching users");
  await new Promise((resolve) => setTimeout(resolve, 1000));
  return {
    data: [],
    max_page: 0,
  };
});


And these are the logs when entering home page and then navigating to /users

2 Replies

Siamese CrocodileOP
All pages are dynamic
Siamese CrocodileOP
Closing it. Cache is not needed - we need to add explicit prefetch={true}