• Skip to main content
  • Skip to primary sidebar

Web Development Archive

  • Archive
You are here: Home / NestJS / NoCache Interceptor – NestJS

NoCache Interceptor – NestJS

import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Response } from 'express';
import { Observable } from 'rxjs';

@Injectable()
export class NoCacheInterceptor implements NestInterceptor {
public intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
const res = context.switchToHttp().getResponse<Response>();

res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');

return next.handle();
}
}

Ez egy NestJS interceptor, ami minden HTTP válaszra cache-tiltó fejléceket állít be.

Lépésenként:

  1. @Injectable() + implements NestInterceptor
    Ez azt jelzi, hogy a NoCacheInterceptor egy NestJS interceptor osztály, amit bármelyik route-hoz, controllerhez vagy globálisan alkalmazhatsz.
  2. intercept(context, next)
    Ez az interceptor “belépési pontja”.
    • A context tartalmazza az aktuális HTTP kérést és választ (ExecutionContext).
    • A next.handle() hívás továbbadja a vezérlést a következő middleware-nek vagy magának a route handlernek.
  3. const res = context.switchToHttp().getResponse<Response>();
    Ez kiszedi az aktuális Express Response objektumot, amin keresztül lehet fejléceket, státuszkódokat stb. beállítani.
  4. HTTP fejlécek beállítása res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); res.setHeader('Pragma', 'no-cache'); res.setHeader('Expires', '0');
    • Cache-Control: no-cache, no-store, must-revalidate → A böngésző és proxyk ne cache-eljék, mindig frissen kérjék le.
    • Pragma: no-cache → régi HTTP/1.0 kompatibilitás miatt.
    • Expires: 0 → lejárati idő azonnal lejárt.
    Ezzel megtiltod a kliens és köztes cache rétegek számára a tartalom tárolását.
  5. return next.handle();
    Visszaadja az adatfolyamot változatlanul, csak a fejléceket manipulálta.

Ez az interceptor gondoskodik arról, hogy az API válaszai soha ne kerüljenek cache-be (se böngészőben, se proxyban). Hasznos például dinamikus adatoknál, ahol mindig friss választ akarsz.

Használat a controller-ben:

import { Controller, Get, UseInterceptors } from '@nestjs/common';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { NoCacheInterceptor } from 'src/common/interceptors/no-cache.interceptor';

import { NewsDto } from './dto/news.dto';
import { NewsService } from './news.service';
@UseInterceptors(NoCacheInterceptor)
@Controller('news')
export class NewsController {
public constructor(private readonly _newsService: NewsService) {}

@Get()
@ApiOperation({ summary: 'Get all news', operationId: 'getAll' })
@ApiResponse({
status: 200,
description: 'Successfully retrieved the list of news.',
type: [NewsDto]
})
@ApiResponse({
status: 500,
description: 'Internal server error'
})
public getAll(): readonly NewsDto[] {
return this._newsService.getAll();
}
}

Filed Under: NestJS

About Gabor Flamich

I'm a web developer and designer based in Budapest, Hungary. In recent years, I've documented hundreds of solutions I came across during development. This site is an archive for useful code snippets on WordPress, Genesis Framework and WooCommerce. If You have any questions related to WordPress development, get in touch!

Primary Sidebar

  • angular.io
© 2026 WP Flames - All Right Reserved