#User is not cached

18 messages · Page 1 of 1 (latest)

autumn gyro
#

Hi, I'm currently facing an issue that **user is not cached ** and when I exit the page then I enter it again the user is fetched again

Even If I switched to another page that also uses the user it's fetched again

User fetch action:

export async function getUser(): Promise<IUser | null> {
  const c = cookies();
  try {
    const res = await fetch(API_URL + "/users/@me", {
      method: RequestMethod.Get,
      credentials: "include",
      headers: {
        Cookie: c.toString(),
        "Content-Type": "application/json",
      },
      next: {
        tags: ["user"],
      },
    });

    const user = await res.json();

    return user;
  } catch (error) {
    return null;
  }
}

Page 1:

import { getUser } from "@/server-actions/auth.actions";

async function Contact() {
  const user = await getUser();
  return <h1>{JSON.stringify(user)}</h1>;
}

export default Contact;

Page 2:

import { getUser } from "@/server-actions/auth.actions";
import React from "react";

async function Projects() {
  const user = await getUser();
  return <h1>{JSON.stringify(user)}</h1>;
}

export default Projects;

Representation for what I do:

Home > Projects (User is fetched)Projects > Home > Contact (User is fetched)
The user should be cached after being fetched at projects

I want to know if this is the normal behaviour or not

silent masonBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

autumn gyro
#

and is there a way to get the credentials without using cookies() function

tough ridge
autumn gyro
#

and without using it the sessionId is not read

#

when I fetch the user at the server side

#

also credentials: "include" not working

autumn gyro
#

Anyone here?

tough ridge
# autumn gyro Ok I understood that it's a normal behaviour to refetch the user and keep his da...

It looks like the page itself is still be static if possible. I create a showcase repo, to preview this: https://codesandbox.io/p/devbox/serene-sunset-g2jvnw?file=%2Fapp%2Flayout.tsx%3A18%2C30-18%2C39
As you can see, the cookie will be accessed, but if you reload the page the time is still the same (static)

So you might check your pages as well, if they contain anyhing else, so that they will be dynamically rendered

autumn gyro
#

I didn't make hard refresh I have just opened it in new tab

tough ridge
autumn gyro
tough ridge
#

If you are of the opinion that the respective routes can be loaded statically, you can add a

export const dynamic = 'force-static'

on top of your pages (or even on the layout) so all pages that should be statically be served will be served statically