User is not cached
Unanswered
Polar bear posted this in #help-forum
Polar bearOP
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:
### Page 1:
### Page 2:
### Representation for what I do:
The user should be cached after being fetched at projects
I want to know if this is the normal behaviour or not
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
17 Replies
Polar bearOP
and is there a way to get the credentials without using
cookies() function@Polar bear 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:
tsx
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:
tsx
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:
tsx
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
when you add
force-cache to it, it will be cached thought your fetch call. However, you shouldn't cache auth details. Else there might be stale data in the future. Like that the user can do stuff, without beeing logged in or even with invalid keys. That's shouldn't be what you want@B33fb0n3 when you add force-cache to it, it will be cached thought your fetch call. However, you shouldn't cache auth details. Else there might be stale data in the future. Like that the user can do stuff, without beeing logged in or even with invalid keys. That's shouldn't be what you want
Polar bearOP
Ok I understood that it's a normal behaviour to refetch the user and keep his data up-to-date
but now the another issue I'm facing that the cookies() function I use to get the sessionId is forcing all routes to be dynamically
because I use it in the navbar and the navbar is in the root layout
so is there any solution for this?
but now the another issue I'm facing that the cookies() function I use to get the sessionId is forcing all routes to be dynamically
because I use it in the navbar and the navbar is in the root layout
so is there any solution for this?
and without using it the sessionId is not read
when I fetch the user at the server side
also credentials: "include" not working
Polar bearOP
Anyone here?
@Polar bear Ok I understood that it's a normal behaviour to refetch the user and keep his data up-to-date
but now the another issue I'm facing that the cookies() function I use to get the sessionId is forcing all routes to be dynamically
because I use it in the navbar and the navbar is in the root layout
so is there any solution for this?
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
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
@B33fb0n3 whoops. Updated: https://codesandbox.io/p/devbox/serene-sunset-g2jvnw?file=%2Fapp%2Flayout.tsx%3A18%2C30-18%2C39
Polar bearOP
Even by removing cookies() it doesn't cache the request
GET http://localhost:4000/users/@me 403 in 27ms (cache skip)
│ │ │ Cache skipped reason: (cache-control: no-cache (hard refresh))I didn't make hard refresh I have just opened it in new tab
@Polar bear Even by removing cookies() it doesn't cache the request
ts
GET http://localhost:4000/users/@me 403 in 27ms (cache skip)
│ │ │ Cache skipped reason: (cache-control: no-cache (hard refresh))
I guess there is more that I not know yet, that causes these problems. Btw „403 forbidden“ also does not sound right…
@B33fb0n3 I guess there is more that I not know yet, that causes these problems. Btw „403 forbidden“ also does not sound right…
Polar bearOP
Because the backend doesn't read the cookies so in his pov I'm not logged in
If you are of the opinion that the respective routes can be loaded statically, you can add a
on top of your pages (or even on the layout) so all pages that should be statically be served will be served statically
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
@Polar bear which message of this thread solved your issue?