Performance: Role-based access control
Role-based access control affects server response time and client rendering by controlling which UI elements and data are loaded based on user roles.
export const loader = async ({ request }) => { const user = await getUser(request); if (!user.roles.includes('admin')) { throw new Response('Unauthorized', { status: 403 }); } return json({ user }); }; export default function AdminDashboard() { const { user } = useLoaderData(); return <AdminPanel />; }
export const loader = async ({ request }) => { const user = await getUser(request); return json({ user }); }; export default function Dashboard() { const { user } = useLoaderData(); return ( <div> {user.roles.includes('admin') && <AdminPanel />} {user.roles.includes('editor') && <EditorPanel />} <CommonPanel /> </div> ); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side role checks rendering all UI then hiding unauthorized parts | High (renders all components initially) | Multiple reflows during hydration and updates | High paint cost due to unnecessary UI elements | [X] Bad |
| Server-side role checks rendering only authorized UI | Low (renders minimal necessary components) | Single reflow on initial render | Low paint cost with minimal UI | [OK] Good |
export async function loader({ request }) {
const user = await getUser(request);
if (user.role !== 'admin') {
return redirect('/login');
}
return json({ message: 'Welcome Admin' });
}export async function loader({ request }) {
const user = getUser(request);
if (user.role !== 'admin') {
throw new Response('Unauthorized', { status: 401 });
}
return json({ data: 'Secret Data' });
}export async function loader({ request }) {
const user = await getUser(request);
// Fill in the role check
}Pass the test and this pattern moves unknown