Performance: Cookie-based session management
This concept affects page load speed and interaction responsiveness by how session data is stored and transmitted with each request.
Set-Cookie: sessionId=shortRandomToken; Path=/; HttpOnly Server stores session data separately keyed by token
Set-Cookie: sessionId=veryLongSessionDataStringThatIncludesUserInfoAndPreferences; Path=/; HttpOnly
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large session data in cookies | Minimal | 0 | Low | [X] Bad |
| Small token in cookies with server session | Minimal | 0 | Low | [OK] Good |
sessionId with value abc123 in Astro?setCookie takes the cookie name as a string and the value as a string.import { setCookie, getCookies } from 'astro:cookies';
export async function get({ request }) {
const cookies = getCookies(request);
if (!cookies.user) {
return new Response('No user cookie', { status: 401 });
}
setCookie('lastVisit', new Date().toISOString());
return new Response(`Welcome back, ${cookies.user}!`);
}user=Alice?cookies.user exists. With user=Alice, it passes the check.setCookie('lastVisit', new Date().toISOString()) lacks the required first Response argument, causing a runtime error before returning the greeting.import { setCookie } from 'astro:cookies';
export async function post({ request }) {
setCookie('session', 'xyz', { secure: true });
return new Response('Session set');
}maxAge: 3600 (seconds). One option uses it correctly with secure: true; another uses incorrect expires value.Pass the test and this pattern moves unknown