Performance: Cookie session storage
This affects page load speed and interaction responsiveness by how session data is stored and sent with requests.
import { createCookieSessionStorage } from '@remix-run/node'; const sessionStorage = createCookieSessionStorage({ cookie: { name: 'session', maxAge: 3600, httpOnly: true, secure: true, sameSite: 'lax', path: '/', }, }); // Store only a small session ID in session cookie const session = await sessionStorage.getSession(); session.set('id', 'unique-session-id'); const cookieHeader = await sessionStorage.commitSession(session, { maxAge: 3600 }); // Fetch full session data from server or database on demand
import { createCookieSessionStorage } from '@remix-run/node'; export const sessionStorage = createCookieSessionStorage({ cookie: { name: 'session', maxAge: 3600, httpOnly: true, secure: true, sameSite: 'lax', path: '/', }, }); // Storing large JSON object in session (bloats cookie) const session = await sessionStorage.getSession(); session.set('userData', largeJSON); // largeJSON is big const cookieHeader = await sessionStorage.commitSession(session, { maxAge: 3600 });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large session data in cookie | N/A | N/A | Increases network latency delaying paint | [X] Bad |
| Minimal session ID in cookie | N/A | N/A | Smaller request size speeds up paint | [OK] Good |
userId after reading the session cookie?const sessionStorage = createCookieSessionStorage({
cookie: { name: 'mySession', secrets: ['s3cret'] }
});
const cookieHeader = 'mySession=eyJ1c2VySWQiOiIxMjM0NSJ9';
const session = await sessionStorage.getSession(cookieHeader);
const userId = session.get('userId');const sessionStorage = createCookieSessionStorage({
cookie: { name: 'session', secret: ['key1'] }
});
const session = await sessionStorage.getSession(request.headers.get('Cookie'));
session.set('token', 'abc123');
const cookieHeader = await sessionStorage.commitSession(session);
return new Response('OK', { headers: { 'Set-Cookie': cookieHeader } });Pass the test and this pattern moves unknown