Performance: Database integration patterns
This affects page load speed and interaction responsiveness by how data fetching and updates are handled between the frontend and backend.
import { cache } from '~/utils/cache'; import { json } from '@remix-run/node'; export async function loader() { const cachedData = cache.get('items'); if (cachedData) return json(cachedData); const data = await db.query('SELECT * FROM items'); cache.set('items', data); return json(data); }
export async function loader() { const data = await db.query('SELECT * FROM items'); return json(data); } // Called on every page load or interaction
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetch full data every time | Minimal DOM nodes | Multiple reflows due to delayed data | High paint cost due to large payload | [X] Bad |
| Use caching for repeated data | Minimal DOM nodes | Single reflow after data load | Lower paint cost | [OK] Good |
| Load large data upfront | Many DOM nodes | Multiple reflows as data renders | Very high paint cost | [X] Bad |
| Use pagination to limit data | Few DOM nodes per page | Single reflow per page load | Low paint cost | [OK] Good |
| Refetch full data after update | Minimal DOM nodes | Multiple reflows due to full reload | High paint cost | [X] Bad |
| Return minimal update response | Minimal DOM nodes | Single reflow with local update | Low paint cost | [OK] Good |
loader functions to fetch data on the server before rendering.action handles data updates, useEffect and componentDidMount are React client hooks, not Remix server loaders.loader -> Option Dloader [OK]loader, update with action [OK]loader with actioncomponentDidMount runs on serverexport const loader = async () => {
const users = await db.user.findMany({ where: { active: true } });
return { users };
};where: { active: true } to get only active users.users holding the array of filtered user records.where limit results; output matches filtered data [OK]findMany is invalidexport const action = async ({ request }) => {
const formData = await request.formData();
const name = formData.get('name');
await db.user.create({ data: { name } });
return redirect('/users');
};redirect function must be imported from Remix to be used.formData.get is correct to get form values; redirect is synchronous; db.user.create is valid Prisma method.redirect from Remix -> Option Aredirect before use [OK]redirect [OK]redirectinclude with the related model name set to true.where for inclusion; D tries to include a field, not a relation.include to fetch related models [OK]include with relation name to fetch related data [OK]where instead of include for relationsPass the test and this pattern moves unknown