I can start by explaining the context.
In my project, I have a header on my home page. On the right-hand side of this header, I have connection and registration buttons. If the user logs in (auth is managed via supabase) then the buttons are replaced by an avatar and user information (last name, first name and email). Basically, I had declared the cache control as follows:
export const onGet: RequestHandler = async ({ cacheControl }) => {
cacheControl({
staleWhileRevalidate: 60 * 60 * 24 * 7,
maxAge: 5,
});
};
But I had some side effects:
- When I created an account, confirmed the registration and was redirected to my home page, the top-right button was always displayed. I had to do a browser refresh to see the user information.
- When I logged in to an account, logged out and logged back in, the previous account information was displayed.
So I wondered if it would be a good practice/performant to have a condition if the user is logged in or not to configure cacheControl:
export const onGet: RequestHandler = async ({ cacheControl }) => {
const supabase = createServerClient(
env.get("PUBLIC_SUPABASE_URL")!,
env.get("PUBLIC_SUPABASE_ANON_KEY")!,
{
cookies: {
get(name: string) {
return cookie.get(name)?.value;
},
},
}
);
const { data } = await supabase.auth.getSession();
if (data.session) {
cacheControl(
{
staleWhileRevalidate: 60 * 60 * 24 * 7,
maxAge: 5,
},
"CDN-Cache-Control"
);
} else {
cacheControl(
{
private: true,
staleWhileRevalidate: 3600,
maxAge: 5,
},
"CDN-Cache-Control"
);
}
};