Performance: Routes and layouts
This affects how quickly the main content appears and how smoothly navigation feels by managing page structure and rendering.
import { serve } from "https://deno.land/std/http/server.ts"; const layout = `<header>Header</header><main></main><footer>Footer</footer>`; serve(async (req) => { // Render layout once and inject only route-specific content const routeContent = `<main>Page content for ${req.url}</main>`; return new Response(layout.replace('<main></main>', routeContent), { headers: { "content-type": "text/html" } }); });
import { serve } from "https://deno.land/std/http/server.ts"; serve(async (req) => { // Render full layout and route content on every request const layout = `<header>Header</header><footer>Footer</footer>`; const routeContent = `<main>Page content for ${req.url}</main>`; return new Response(layout + routeContent, { headers: { "content-type": "text/html" } }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full layout re-render on every route | High (full DOM rebuild) | Multiple reflows per request | High paint cost | [X] Bad |
| Static layout with dynamic route content | Low (partial DOM update) | Single reflow per route change | Low paint cost | [OK] Good |
const router = new Router();
router.get('/about', (ctx) => { ctx.response.body = 'About Page'; });
const layout = (content) => `Header ${content}`;
// Middleware to wrap response
app.use(async (ctx, next) => {
await next();
ctx.response.body = layout(ctx.response.body);
});
app.use(router.routes());
app.use(router.allowedMethods());const router = new Router();
router.get('/contact', (ctx) => {
ctx.response.body = 'Contact Page';
});
const layout = (content) => `Header ${content}`;
app.use(router.routes());
app.use(router.allowedMethods());
app.use(async (ctx, next) => {
await next();
ctx.response.body = layout(ctx.response.body);
});Pass the test and this pattern moves unknown