import { json } from '@remix-run/node'; import { getSession } from '~/sessions'; export async function loader({ request }) { const session = await getSession(request.headers.get('Cookie')); const visits = session.get('visits') || 0; session.set('visits', visits + 1); return json({ visits }, { headers: { 'Set-Cookie': await session.commit() } }); }
The loader reads the 'visits' count from the cookie session. If none exists, it starts at 0. Then it increments and sets it back. The JSON response shows the previous count before increment.
The correct property is secrets (plural) and it must be an array of strings.
export async function action({ request }) { const session = await getSession(request.headers.get('Cookie')); session.set('user', 'alice'); return json({ success: true }); }
After modifying the session, you must commit it and include the 'Set-Cookie' header in the response to update the cookie in the browser.
const session = await getSession(cookieHeader); session.set('cart', ['apple']); session.set('cart', [...session.get('cart'), 'banana']); session.set('cart', session.get('cart').filter(item => item !== 'apple')); const cart = session.get('cart');
Initially 'cart' is ['apple']. Then 'banana' is added, making ['apple', 'banana']. Finally, 'apple' is removed, leaving ['banana'].
Multiple secrets allow Remix to rotate keys without invalidating existing sessions. 'httpOnly' should be true to prevent JavaScript access. 'secure' should be true in production. 'sameSite' helps prevent CSRF attacks and should be set appropriately.
