Performance: Server-only modules
HIGH IMPACT
This concept affects the bundle size sent to the browser and the initial page load speed by keeping server-only code out of client bundles.
import { readFile } from './server-utils'; export const dynamic = 'force-dynamic'; export default async function Page() { const data = await readFile('/data.txt'); return <div>{data}</div>; } // server-utils.js export async function readFile(path) { const fs = await import('fs/promises'); return fs.readFile(path, 'utf-8'); }
import fs from 'fs'; export default function Page() { const data = fs.readFileSync('/data.txt', 'utf-8'); return <div>{data}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Import server-only modules in client code | N/A | N/A | High due to large bundle and blocking parse | [X] Bad |
| Use server-only modules in server components or server actions | N/A | N/A | Low bundle size, fast parse | [OK] Good |