Performance: Login and logout flows
This affects page load speed and interaction responsiveness during user authentication transitions.
import { useFetcher } from '@remix-run/react'; export default function Login() { const fetcher = useFetcher(); return ( <fetcher.Form method="post"> <input name="username" aria-label="Username" /> <input name="password" type="password" aria-label="Password" /> <button type="submit">Login</button> {fetcher.data?.error && <p role="alert">{fetcher.data.error}</p>} </fetcher.Form> ); } // Logout button export function LogoutButton() { const fetcher = useFetcher(); return ( <fetcher.Form method="post" action="/logout"> <button type="submit" aria-label="Logout">Logout</button> </fetcher.Form> ); }
import { redirect, json } from '@remix-run/node'; export async function action({ request }) { const formData = await request.formData(); const username = formData.get('username'); const password = formData.get('password'); // Authenticate user if (username === 'user' && password === 'pass') { // Set cookie and redirect return redirect('/dashboard'); } return json({ error: 'Invalid credentials' }, { status: 401 }); } // Logout export async function loader() { // Clear cookie and redirect return redirect('/login'); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full page reload on login/logout | High (reloads entire DOM) | Multiple (reload triggers full layout) | High (repaints whole page) | [X] Bad |
| Client-side fetcher form submission | Low (partial DOM update) | Single (minimal layout update) | Low (partial repaint) | [OK] Good |
action function in a login flow?action function processes POST requests from forms, such as login forms.export const action = async ({ request }) => {
const form = await request.formData();
const username = form.get('username');
if (username === 'admin') {
return redirect('/dashboard');
}
return json({ error: 'Invalid user' }, { status: 401 });
};export const action = async ({ request }) => {
const session = await getSession(request.headers.get('Cookie'));
session.unset('userId');
return redirect('/login');
};action function handles form POST, validates user credentials securely.loader reads the cookie to verify if user is logged in and controls access.Pass the test and this pattern moves unknown