0
0
NestJSframework~8 mins

Redirect responses in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Redirect responses
MEDIUM IMPACT
Redirect responses affect page load speed by causing additional HTTP requests and delays before the final content loads.
Handling user navigation with redirects in NestJS
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppController {
  @Get('old-route')
  redirectOld() {
    return 'New content here';
  }

  @Get('new-route')
  newRoute() {
    return 'New content here';
  }
}
Serve the final content directly or use server-side routing to avoid client redirects.
📈 Performance GainEliminates extra HTTP request, improving LCP by 100-300ms.
Handling user navigation with redirects in NestJS
NestJS
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller()
export class AppController {
  @Get('old-route')
  redirectOld(@Res() res: Response) {
    res.redirect('/new-route');
  }
}
This causes an extra HTTP request because the browser must first load 'old-route', then follow the redirect to 'new-route'.
📉 Performance CostTriggers 1 extra HTTP request and delays LCP by 100-300ms depending on network.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Redirect response causing client-side navigationMinimal0Delayed paint due to extra request[X] Bad
Directly serving final content without redirectMinimal0Paint happens faster[OK] Good
Rendering Pipeline
Redirect responses cause the browser to stop rendering the current page and send a new request to the redirected URL, delaying content painting.
Network
HTML Parsing
Style Calculation
Layout
Paint
⚠️ BottleneckNetwork latency due to extra HTTP request
Core Web Vital Affected
LCP
Redirect responses affect page load speed by causing additional HTTP requests and delays before the final content loads.
Optimization Tips
1Avoid unnecessary redirects to reduce extra HTTP requests.
2Use server-side routing to serve final content directly.
3Check Network panel in DevTools to identify redirect chains.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance cost of using redirect responses in NestJS?
AIncreased DOM nodes causing reflows
BExtra HTTP requests causing slower page load
CMore CSS calculations slowing paint
DLarger JavaScript bundle size
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page, and look for HTTP status codes 3xx indicating redirects.
What to look for: Multiple requests with 3xx status before final 200 response indicate redirects slowing page load.