[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"verticals":3,"article-nestjs-weekly-interceptor-order":44,"search-suggestions":516,"path-membership-nestjs-weekly-interceptor-order":558,"related-nestjs-weekly-interceptor-order":559,"code:ts:true:1f49yyb":665,"code:ts:true:1k852bt":666,"code:ts:true:s1plld":667,"code:ts:true:8t13q5":668,"code:ts:true:102eqwi":669,"code:ts:true:1795dwh":670,"code:ts:true:p84vnc":671,"comments-01a1a1b0-49e9-711f-aea2-cbc2b73acc75":672},[4,20,32],{"id":5,"slug":6,"name":7,"tagline":8,"description":9,"accentFrom":10,"accentTo":11,"icon":12,"defaultLocale":13,"locales":14,"features":16,"position":19},"019fe637-3d33-714b-b57f-23e163ffca0c","dev","Web Development","Read it. Run it. Prove it.","A post a day on modern web development — most with an editable playground and a quiz that explains every answer. Free, no account needed.","violet-500","cyan-400","◇","en",[13,15],"fa",{"courses":17,"paths":17,"articles":17,"exams":18,"flashcards":18,"packages":17,"community":17,"certificates":17,"teams":17,"commerce":17},true,false,0,{"id":21,"slug":22,"name":23,"tagline":24,"description":25,"accentFrom":26,"accentTo":10,"icon":27,"defaultLocale":13,"locales":28,"features":30,"position":31},"019fe637-3dc2-754c-8657-0f175bfee7c6","lang","Languages","Learn a language the way you learn a codebase.","Grammar explained the way good documentation explains an API — one idea at a time, each with a quiz.","amber-400","⌘",[13,15,29],"es",{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":17,"packages":18,"community":17,"certificates":17,"teams":18,"commerce":18},2,{"id":33,"slug":34,"name":35,"tagline":36,"description":37,"accentFrom":38,"accentTo":39,"icon":40,"defaultLocale":13,"locales":41,"features":42,"position":43},"7b3c16f2-931d-410e-802e-e1fa4edab7de","soft","Soft Skills","The half of the job nobody wrote documentation for.","Weekly, on the parts of working life that decide more than your code does — first weeks, meetings, interviews, promotions, and the people around you. Written from what actually happens, and recorded as a podcast you can listen to on the walk.","emerald-400","teal-300","◉",[13],{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":18,"packages":18,"community":17,"certificates":18,"teams":18,"commerce":18},3,{"id":45,"slug":46,"title":47,"subtitle":48,"excerpt":49,"coverUrl":50,"locale":13,"readingMinutes":51,"publishedAt":52,"viewCount":53,"likeCount":19,"commentCount":19,"author":54,"vertical":59,"topic":60,"tags":63,"_count":74,"playground":76,"body":78,"bodyMd":449,"seo":450,"translationGroupId":452,"series":453,"podcastUrl":48,"verticalId":5,"thread":505,"assessments":507,"translations":512,"quiz":514},"01a1a1b0-49e9-711f-aea2-cbc2b73acc75","nestjs-weekly-interceptor-order","NestJS Interceptor Order: Why Stacking Breaks Your Cache",null,"NestJS interceptor order decides whether a cache hit skips your response transform. Learn the onion model behind next.handle() — plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-interceptor-order.png",16,"2026-11-27T11:44:57.150Z",9,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},"019fe637-3c25-7088-9034-39c9f15dc3c8","Parsa Jiravand","parsa","Frontend engineer · building bestpractic",{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},"nestjs","Nestjs",[64,65,68,71],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},"node","Node",{"slug":69,"name":70,"color":48},"typescript","Typescript",{"slug":72,"name":73,"color":48},"tutorial","Tutorial",{"assessments":75},1,{"slug":46,"title":77},"NestJS interceptor order — onion model playground",{"blocks":79,"version":75},[80,84,87,92,95,104,107,110,113,116,131,135,138,144,147,151,154,157,160,163,166,169,173,176,180,183,186,190,193,196,199,203,206,209,212,216,219,222,225,228,231,237,240,243,246,250,253,256,264,267,275,278,281,284,287,290,294,297,301,304,307,310,313,369,372,380,383,386,389,392,395,398,401,404,407,410,413,416,419,422,425,431,434,437,440,443],{"id":81,"html":82,"type":83},"b1","\u003Cp>User 42 fetches \u003Ccode>GET \u002Fdocuments\u002F7\u003C\u002Fcode>, the document they own. The response includes \u003Ccode>isOwner: true\u003C\u002Fcode>. A minute later, user 7 — who has read access but doesn&#39;t own it — fetches the exact same document. The response \u003Cem>also\u003C\u002Fem> says \u003Ccode>isOwner: true\u003C\u002Fcode>. User 7 now sees an &quot;Edit&quot; button a permission check downstream will eventually reject, but the UI already lied to them, and a client that trusts \u003Ccode>isOwner\u003C\u002Fcode> to gate a destructive action has a real authorization bug, not a cosmetic one.\u003C\u002Fp>","paragraph",{"id":85,"html":86,"type":83},"b2","\u003Cp>Nobody touched the authorization logic. Nobody touched the DTO. Both interceptors involved are individually correct, straight from patterns NestJS&#39;s own documentation shows. The bug is entirely in the order two \u003Ccode>@UseInterceptors()\u003C\u002Fcode> arguments were listed — and until you know what that list actually builds, the order looks arbitrary.\u003C\u002Fp>",{"id":88,"html":89,"text":90,"type":91,"level":31},"b3","What you&#39;ll learn","What you'll learn","heading",{"id":93,"html":94,"type":83},"b4","\u003Cp>By the end of this article you&#39;ll be able to:\u003C\u002Fp>",{"id":96,"type":97,"items":98,"ordered":18},"b5","list",[99,100,101,102,103],"Explain what \u003Ccode>intercept(context, next)\u003C\u002Fcode> and \u003Ccode>CallHandler.handle()\u003C\u002Fcode> actually do to a request, not just that you&#39;re supposed to call them","Predict the exact execution order when interceptors are stacked at the global, controller, and method level","Recognize why an interceptor that skips \u003Ccode>next.handle()\u003C\u002Fcode> (like a cache) silently skips every interceptor nested inside it too","Fix a stale-personalization bug caused by interceptor order, instead of chasing it through the DTO and the authorization layer","Choose between method-level \u003Ccode>@UseInterceptors()\u003C\u002Fcode>, \u003Ccode>app.useGlobalInterceptors()\u003C\u002Fcode>, and the \u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode> provider token",{"id":105,"html":106,"text":106,"type":91,"level":31},"b6","Who this is for",{"id":108,"html":109,"type":83},"b7","\u003Cp>You&#39;ve written at least one NestJS controller with \u003Ccode>@UseGuards()\u003C\u002Fcode> or \u003Ccode>@UseInterceptors()\u003C\u002Fcode> on it, even if you copied the interceptor from the docs without fully tracing what it does. You don&#39;t need prior RxJS experience — the handful of operators used here (\u003Ccode>tap\u003C\u002Fcode>, \u003Ccode>map\u003C\u002Fcode>, \u003Ccode>catchError\u003C\u002Fcode>, \u003Ccode>of\u003C\u002Fcode>) are explained as they appear.\u003C\u002Fp>",{"id":111,"html":112,"type":83},"b8","\u003Cp>This article is written against \u003Cstrong>NestJS 12.x\u003C\u002Fstrong> (\u003Ccode>@nestjs\u002Fcore\u003C\u002Fcode> is at \u003Ccode>12.6.0\u003C\u002Fcode> on npm \u003Ccode>latest\u003C\u002Fcode>, with \u003Ccode>11.2.11\u003C\u002Fcode> the last \u003Ccode>legacy\u003C\u002Fcode>-tagged 11.x release). The interceptor API — \u003Ccode>NestInterceptor\u003C\u002Fcode>, \u003Ccode>CallHandler\u003C\u002Fcode>, \u003Ccode>ExecutionContext\u003C\u002Fcode> — and the global → controller → route composition order are core-container behavior, unchanged in shape since well before v12.\u003C\u002Fp>",{"id":114,"html":115,"text":115,"type":91,"level":31},"b9","Table of contents",{"id":117,"type":97,"items":118,"ordered":18},"b10",[119,120,121,122,123,124,125,126,127,128,129,130],"\u003Ca href=\"#the-problem-a-cache-that-leaks-the-wrong-users-permissions\">The problem: a cache that leaks the wrong user&#39;s permissions\u003C\u002Fa>","\u003Ca href=\"#the-mental-model-interceptors-are-nested-not-listed\">The mental model: interceptors are nested, not listed\u003C\u002Fa>","\u003Ca href=\"#stage-1-what-nexthandle-actually-is\">Stage 1: what \u003Ccode>next.handle()\u003C\u002Fcode> actually is\u003C\u002Fa>","\u003Ca href=\"#stage-2-composing-two-interceptors-by-hand\">Stage 2: composing two interceptors by hand\u003C\u002Fa>","\u003Ca href=\"#stage-3-the-cache-and-personalization-bug-reproduced\">Stage 3: the cache-and-personalization bug, reproduced\u003C\u002Fa>","\u003Ca href=\"#stage-4-global-controller-and-method--the-fixed-nesting\">Stage 4: global, controller, and method — the fixed nesting\u003C\u002Fa>","\u003Ca href=\"#stage-5-binding-without-breaking-di\">Stage 5: binding without breaking DI\u003C\u002Fa>","\u003Ca href=\"#edge-cases-and-gotchas\">Edge cases and gotchas\u003C\u002Fa>","\u003Ca href=\"#best-practices\">Best practices\u003C\u002Fa>","\u003Ca href=\"#faq\">FAQ\u003C\u002Fa>","\u003Ca href=\"#cheat-sheet\">Cheat sheet\u003C\u002Fa>","\u003Ca href=\"#key-takeaways\">Key takeaways\u003C\u002Fa>",{"id":132,"html":133,"text":134,"type":91,"level":31},"b11","The problem: a cache that leaks the wrong user&#39;s permissions","The problem: a cache that leaks the wrong user's permissions",{"id":136,"html":137,"type":83},"b12","\u003Cp>Here&#39;s the setup that produced the bug above. Two interceptors, each individually correct and each a straightforward extension of a pattern NestJS&#39;s own documentation shows:\u003C\u002Fp>",{"id":139,"code":140,"type":141,"language":142,"highlight":143},"b13","\u002F\u002F Adds a per-request field: does the CURRENT caller own this document?\n@Injectable()\nexport class OwnershipInterceptor implements NestInterceptor {\n  intercept(context: ExecutionContext, next: CallHandler): Observable\u003Cany> {\n    const { user } = context.switchToHttp().getRequest();\n    return next.handle().pipe(\n      map((doc) => ({ ...doc, isOwner: doc.ownerId === user.id })),\n    );\n  }\n}\n\n\u002F\u002F Caches the raw document by id — the document itself is the same for\n\u002F\u002F every viewer, so it's cached once, regardless of who's asking.\n@Injectable()\nexport class CacheInterceptor implements NestInterceptor {\n  constructor(private cache: CacheStore) {}\n  intercept(context: ExecutionContext, next: CallHandler): Observable\u003Cany> {\n    const key = context.switchToHttp().getRequest().params.id;\n    const cached = this.cache.get(key);\n    if (cached) return of(cached);\n    return next.handle().pipe(tap((doc) => this.cache.set(key, doc)));\n  }\n}","code","ts",[],{"id":145,"html":146,"type":83},"b14","\u003Cp>Both are individually reasonable: caching a shared resource by its id is a normal optimization, and stamping a per-caller \u003Ccode>isOwner\u003C\u002Fcode> flag onto a response is a normal personalization. Bound on the controller like this, reading top to bottom as &quot;first cache, then personalize&quot;:\u003C\u002Fp>",{"id":148,"code":149,"type":141,"language":142,"highlight":150},"b15","@UseInterceptors(CacheInterceptor, OwnershipInterceptor)\n@Get(':id')\nfindOne(@Param('id') id: string) {\n  return this.documents.findOne(id);\n}",[],{"id":152,"html":153,"type":83},"b16","\u003Cp>User 42 (the owner) requests document 7, a cache miss: \u003Ccode>CacheInterceptor\u003C\u002Fcode> calls \u003Ccode>next.handle()\u003C\u002Fcode>, which runs \u003Ccode>OwnershipInterceptor\u003C\u002Fcode>, which runs the handler, gets back \u003Ccode>{ id: 7, ownerId: 42 }\u003C\u002Fcode>, and stamps \u003Ccode>isOwner: true\u003C\u002Fcode> because the current request&#39;s user is 42. \u003Ccode>CacheInterceptor\u003C\u002Fcode>&#39;s \u003Ccode>tap()\u003C\u002Fcode> then caches that \u003Cem>already-personalized\u003C\u002Fem> object — \u003Ccode>{ id: 7, ownerId: 42, isOwner: true }\u003C\u002Fcode> — keyed only by the document id.\u003C\u002Fp>",{"id":155,"html":156,"type":83},"b17","\u003Cp>User 7 (not the owner) requests the same document 7 next: \u003Ccode>CacheInterceptor\u003C\u002Fcode> finds a cached entry for key \u003Ccode>7\u003C\u002Fcode> and returns \u003Ccode>of(cached)\u003C\u002Fcode> directly — it never calls \u003Ccode>next.handle()\u003C\u002Fcode>, so \u003Ccode>OwnershipInterceptor\u003C\u002Fcode> never runs for \u003Cem>this\u003C\u002Fem> request. The response sent to user 7 is the exact object cached from user 42&#39;s request: \u003Ccode>isOwner: true\u003C\u002Fcode>. User 7 is told they own a document they don&#39;t, because the only code that could have corrected the flag for the new caller never ran.\u003C\u002Fp>",{"id":158,"html":159,"text":159,"type":91,"level":31},"b18","The mental model: interceptors are nested, not listed",{"id":161,"html":162,"type":83},"b19","\u003Cp>\u003Cstrong>The mental model:\u003C\u002Fstrong> \u003Ccode>@UseInterceptors(A, B)\u003C\u002Fcode> does not run \u003Ccode>A\u003C\u002Fcode> then \u003Ccode>B\u003C\u002Fcode> as two independent steps. It builds \u003Ccode>A\u003C\u002Fcode> wrapping \u003Ccode>B\u003C\u002Fcode> wrapping the handler — one nested call, not a flat list. The first interceptor you name becomes the \u003Cstrong>outermost\u003C\u002Fstrong> layer; the last one you name sits \u003Cstrong>closest to the handler\u003C\u002Fstrong>.\u003C\u002Fp>",{"id":164,"html":165,"type":83},"b20","\u003Cp>This matters because \u003Ccode>next.handle()\u003C\u002Fcode> is not &quot;move to the next item in a list.&quot; It is &quot;invoke everything nested inside me, and give me back its result as an \u003Ccode>Observable\u003C\u002Fcode>.&quot; When \u003Ccode>A\u003C\u002Fcode> calls \u003Ccode>next.handle()\u003C\u002Fcode>, that call \u003Cem>is\u003C\u002Fem> \u003Ccode>B\u003C\u002Fcode>&#39;s entire \u003Ccode>intercept()\u003C\u002Fcode> execution, including whatever \u003Ccode>B\u003C\u002Fcode> does before and after its own \u003Ccode>next.handle()\u003C\u002Fcode>. If \u003Ccode>A\u003C\u002Fcode> never calls \u003Ccode>next.handle()\u003C\u002Fcode> — because it short-circuits, exactly like \u003Ccode>CacheInterceptor\u003C\u002Fcode> does on a hit — then \u003Ccode>B\u003C\u002Fcode> never runs. Not &quot;runs with stale data.&quot; Does not run, at all, on that request.\u003C\u002Fp>",{"id":167,"html":168,"type":83},"b21","\u003Cp>This is the same onion composition Express and Koa middleware use, and it is also how NestJS composes guards, interceptors, and pipes across scopes (confirmed by the framework&#39;s own request-lifecycle documentation): code that runs \u003Cem>before\u003C\u002Fem> the nested call executes in registration order, outermost first; code that runs \u003Cem>after\u003C\u002Fem> — the RxJS operators chained onto the \u003Ccode>Observable\u003C\u002Fcode> that \u003Ccode>next.handle()\u003C\u002Fcode> returns — unwinds in the reverse order, innermost first. Draw it as concentric rings, not a pipeline of boxes, and the cache bug above stops being surprising: \u003Ccode>CacheInterceptor\u003C\u002Fcode> is the outer ring, so skipping its \u003Ccode>next.handle()\u003C\u002Fcode> cuts out everything inside that ring, \u003Ccode>OwnershipInterceptor\u003C\u002Fcode> included.\u003C\u002Fp>",{"id":170,"html":171,"text":172,"type":91,"level":31},"b22","Stage 1: what \u003Ccode>next.handle()\u003C\u002Fcode> actually is","Stage 1: what next.handle() actually is",{"id":174,"html":175,"type":83},"b23","\u003Cp>Every \u003Ccode>NestInterceptor\u003C\u002Fcode> implements one method:\u003C\u002Fp>",{"id":177,"code":178,"type":141,"language":142,"highlight":179},"b24","interface NestInterceptor\u003CT = any, R = any> {\n  intercept(context: ExecutionContext, next: CallHandler\u003CT>): Observable\u003CR>;\n}",[],{"id":181,"html":182,"type":83},"b25","\u003Cp>\u003Ccode>context: ExecutionContext\u003C\u002Fcode> is the same object a guard sees — it carries the request, the controller class, and the handler reference. \u003Ccode>next: CallHandler\u003C\u002Fcode> has exactly one method, \u003Ccode>handle()\u003C\u002Fcode>, which invokes the rest of the chain (the next interceptor, or the route handler if there is none left) and returns an \u003Ccode>Observable\u003C\u002Fcode> of its eventual result.\u003C\u002Fp>",{"id":184,"html":185,"type":83},"b26","\u003Cp>\u003Cstrong>Key concept:\u003C\u002Fstrong> code written \u003Cem>before\u003C\u002Fem> \u003Ccode>return next.handle()...\u003C\u002Fcode> runs before the handler. Code chained onto the \u003Ccode>Observable\u003C\u002Fcode> that \u003Ccode>next.handle()\u003C\u002Fcode> returns — via RxJS operators like \u003Ccode>tap\u003C\u002Fcode>, \u003Ccode>map\u003C\u002Fcode>, or \u003Ccode>catchError\u003C\u002Fcode> — runs after the handler resolves, and can transform, log, or replace its result. If an interceptor never calls \u003Ccode>next.handle()\u003C\u002Fcode> and never returns an equivalent \u003Ccode>Observable\u003C\u002Fcode>, the request hangs forever: no response is ever emitted, because nothing inside Nest&#39;s response pipeline was asked to produce one.\u003C\u002Fp>",{"id":187,"code":188,"type":141,"language":142,"highlight":189},"b27","@Injectable()\nexport class LoggingInterceptor implements NestInterceptor {\n  intercept(context: ExecutionContext, next: CallHandler): Observable\u003Cany> {\n    const start = Date.now();\n    return next.handle().pipe(\n      tap(() => console.log(`${context.getHandler().name} took ${Date.now() - start}ms`)),\n    );\n  }\n}",[],{"id":191,"html":192,"type":83},"b28","\u003Cp>This is the minimal correct shape: do setup, call \u003Ccode>next.handle()\u003C\u002Fcode>, pipe a side effect onto the result, return it unchanged. Nothing here reorders anything else — it&#39;s a single ring with nothing nested complicated inside it.\u003C\u002Fp>",{"id":194,"html":195,"text":195,"type":91,"level":31},"b29","Stage 2: composing two interceptors by hand",{"id":197,"html":198,"type":83},"b30","\u003Cp>To see the nesting directly, strip away the decorator and write what \u003Ccode>@UseInterceptors(A, B)\u003C\u002Fcode> effectively builds for a single request:\u003C\u002Fp>",{"id":200,"code":201,"type":141,"language":142,"highlight":202},"b31","function runStack(interceptors: NestInterceptor[], handler: () => Observable\u003Cany>, context: ExecutionContext) {\n  const terminal: CallHandler = { handle: handler };\n  \u002F\u002F Build from the LAST interceptor outward, so the FIRST interceptor ends up outermost.\n  const chain = interceptors.reduceRight\u003CCallHandler>(\n    (next, interceptor) => ({ handle: () => interceptor.intercept(context, next) }),\n    terminal,\n  );\n  return chain.handle();\n}",[],{"id":204,"html":205,"type":83},"b32","\u003Cp>\u003Ccode>reduceRight\u003C\u002Fcode> walks the array back-to-front, so the \u003Cem>last\u003C\u002Fem> interceptor in the array is wrapped first (innermost), and each one before it wraps the previous result — leaving the \u003Cem>first\u003C\u002Fem> interceptor in the array as the outermost \u003Ccode>CallHandler.handle()\u003C\u002Fcode> that actually gets invoked. That&#39;s a simplified model of the mechanism behind &quot;first named, outermost&quot; — Nest&#39;s own implementation builds the same nesting through lazy, forward recursion rather than \u003Ccode>reduceRight\u003C\u002Fcode>, but the resulting order is identical: first interceptor outermost, applied once per scope level.\u003C\u002Fp>",{"id":207,"html":208,"text":208,"type":91,"level":31},"b33","Stage 3: the cache-and-personalization bug, reproduced",{"id":210,"html":211,"type":83},"b34","\u003Cp>Swap the order from the problem section, and the leak closes, because now \u003Ccode>OwnershipInterceptor\u003C\u002Fcode> is outermost and \u003Ccode>CacheInterceptor\u003C\u002Fcode> is innermost:\u003C\u002Fp>",{"id":213,"code":214,"type":141,"language":142,"highlight":215},"b35","@UseInterceptors(OwnershipInterceptor, CacheInterceptor)\n@Get(':id')\nfindOne(@Param('id') id: string) {\n  return this.documents.findOne(id);\n}",[],{"id":217,"html":218,"type":83},"b36","\u003Cp>Walk the same two requests through this order. User 42, cache miss: \u003Ccode>OwnershipInterceptor\u003C\u002Fcode> (outer) calls \u003Ccode>next.handle()\u003C\u002Fcode>, which reaches \u003Ccode>CacheInterceptor\u003C\u002Fcode> (inner), which misses, calls the handler, gets \u003Ccode>{ id: 7, ownerId: 42 }\u003C\u002Fcode>, and caches \u003Cem>that raw object\u003C\u002Fem> — no \u003Ccode>isOwner\u003C\u002Fcode> field at all, because personalization hasn&#39;t happened yet at this ring. The raw object flows back out to \u003Ccode>OwnershipInterceptor\u003C\u002Fcode>, which stamps \u003Ccode>isOwner: true\u003C\u002Fcode> for user 42. Response: \u003Ccode>{ id: 7, ownerId: 42, isOwner: true }\u003C\u002Fcode>.\u003C\u002Fp>",{"id":220,"html":221,"type":83},"b37","\u003Cp>User 7, same document: \u003Ccode>OwnershipInterceptor\u003C\u002Fcode> (outer) still calls \u003Ccode>next.handle()\u003C\u002Fcode> — it has no reason not to, and nothing outside it short-circuited. \u003Ccode>CacheInterceptor\u003C\u002Fcode> (inner) finds a hit and returns the cached \u003Cem>raw\u003C\u002Fem> object without running the handler again. That raw object flows back out to \u003Ccode>OwnershipInterceptor\u003C\u002Fcode>, which — because it is never skipped when it&#39;s the outer ring — stamps a fresh \u003Ccode>isOwner\u003C\u002Fcode> field for \u003Cem>this\u003C\u002Fem> request&#39;s user: \u003Ccode>7 === 42\u003C\u002Fcode> is false. Response: \u003Ccode>{ id: 7, ownerId: 42, isOwner: false }\u003C\u002Fcode>. Correct, every time, for every caller, regardless of which request happened to populate the cache.\u003C\u002Fp>",{"id":223,"html":224,"type":83},"b38","\u003Cp>\u003Cstrong>Key concept:\u003C\u002Fstrong> a ring that must re-evaluate something per request — who&#39;s asking, what they&#39;re allowed to see — has to sit \u003Cem>outside\u003C\u002Fem> anything that can short-circuit, never inside it. \u003Ccode>CacheInterceptor\u003C\u002Fcode>&#39;s short-circuit only ever skips what&#39;s nested inside \u003Ccode>CacheInterceptor\u003C\u002Fcode>; it can never skip a ring wrapped around it.\u003C\u002Fp>",{"id":226,"html":227,"text":227,"type":91,"level":31},"b39","Stage 4: global, controller, and method — the fixed nesting",{"id":229,"html":230,"type":83},"b40","\u003Cp>The same onion extends across scopes, and you cannot reorder it locally. For a single inbound request, Nest&#39;s own request-lifecycle documentation puts interceptors in this order going in, and the exact reverse coming back out:\u003C\u002Fp>",{"id":232,"type":97,"items":233,"ordered":17},"b41",[234,235,236],"\u003Cstrong>Global\u003C\u002Fstrong> interceptors (bound via \u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode> or \u003Ccode>app.useGlobalInterceptors()\u003C\u002Fcode>) — outermost.","\u003Cstrong>Controller-level\u003C\u002Fstrong> interceptors (\u003Ccode>@UseInterceptors()\u003C\u002Fcode> on the class).","\u003Cstrong>Method-level\u003C\u002Fstrong> interceptors (\u003Ccode>@UseInterceptors()\u003C\u002Fcode> on the route handler) — innermost, closest to the actual handler.",{"id":238,"html":239,"type":83},"b42","\u003Cp>On the way back out, it&#39;s the reverse: method-level interceptors&#39; post-handler logic runs first, then controller-level, then global last. A method-level \u003Ccode>CacheInterceptor\u003C\u002Fcode> can never be made to wrap a global \u003Ccode>LoggingInterceptor\u003C\u002Fcode>, no matter which order you list decorators in — global is always the outer ring. If you need a cache to be able to skip a cross-cutting concern like the global logger, the logger has to not depend on seeing every response in the first place, or the cache has to move to a scope at or above it.\u003C\u002Fp>",{"id":241,"html":242,"text":242,"type":91,"level":31},"b43","Stage 5: binding without breaking DI",{"id":244,"html":245,"type":83},"b44","\u003Cp>Three ways to attach an interceptor, each with a different relationship to Nest&#39;s DI container:\u003C\u002Fp>",{"id":247,"code":248,"type":141,"language":142,"highlight":249},"b45","\u002F\u002F Method or controller scope — Nest constructs it through DI\n@UseInterceptors(CacheInterceptor)\n\n\u002F\u002F Global, but constructed by hand — no constructor injection works here\napp.useGlobalInterceptors(new LoggingInterceptor());\n\n\u002F\u002F Global, constructed through DI — constructor injection, module scope, and\n\u002F\u002F test overrides via overrideProvider() all keep working\nproviders: [{ provide: APP_INTERCEPTOR, useClass: CacheInterceptor }]",[],{"id":251,"html":252,"type":83},"b46","\u003Cp>\u003Ccode>CacheInterceptor\u003C\u002Fcode> above takes a \u003Ccode>CacheStore\u003C\u002Fcode> in its constructor. \u003Ccode>app.useGlobalInterceptors(new CacheInterceptor(???))\u003C\u002Fcode> has no way to provide that dependency — you&#39;re back to constructing it by hand outside the container, the same trap \u003Ccode>app.useGlobalGuards()\u003C\u002Fcode> sets for a guard that needs a service injected. \u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode> fixes it the same way \u003Ccode>APP_GUARD\u003C\u002Fcode> does: register it as a provider, let Nest instantiate it through DI, and it becomes global while still receiving its dependencies — and while still being swappable in tests with \u003Ccode>Test.createTestingModule().overrideProvider(CacheStore)\u003C\u002Fcode>.\u003C\u002Fp>",{"id":254,"html":255,"text":255,"type":91,"level":31},"b47","Edge cases and gotchas",{"id":257,"type":97,"items":258,"ordered":18},"b48",[259,260,261,262,263],"\u003Cstrong>An interceptor that never calls \u003Ccode>next.handle()\u003C\u002Fcode> still has to return something RxJS can treat as a stream.\u003C\u002Fstrong> Return a bare value, or nothing at all, and Nest throws immediately — a \u003Ccode>TypeError\u003C\u002Fcode> like &quot;You provided &#39;undefined&#39; where a stream was expected&quot; — which lands in your exception filters. It doesn&#39;t hang; there&#39;s no silent stall to debug. If you&#39;re building a caching or short-circuiting interceptor, always return an \u003Ccode>Observable\u003C\u002Fcode> explicitly — \u003Ccode>of(value)\u003C\u002Fcode> is the simplest correct shape.","\u003Cstrong>A synchronous throw inside \u003Ccode>intercept()\u003C\u002Fcode> doesn&#39;t just skip straight past every interceptor wrapped around it.\u003C\u002Fstrong> Nest invokes each interceptor inside a \u003Ccode>try\u003C\u002Fcode>\u002F\u003Ccode>catch\u003C\u002Fcode> and forwards a synchronous throw via the subscriber&#39;s \u003Ccode>error()\u003C\u002Fcode> callback on the \u003Ccode>Observable\u003C\u002Fcode> that invocation built — which is exactly the \u003Ccode>Observable\u003C\u002Fcode> an \u003Cem>outer\u003C\u002Fem> interceptor&#39;s \u003Ccode>next.handle()\u003C\u002Fcode> returned. So even a throw that happens \u003Cem>before\u003C\u002Fem> this interceptor&#39;s own \u003Ccode>next.handle()\u003C\u002Fcode> call surfaces as a stream error there, and that outer interceptor&#39;s \u003Ccode>catchError()\u003C\u002Fcode> can and does see it. The error only lands directly in Nest&#39;s exception filters, the same as a guard or pipe error, when nothing further out is there to catch it (the throw happens in the outermost interceptor, or no outer interceptor pipes a \u003Ccode>catchError\u003C\u002Fcode>).","\u003Cstrong>Multiple method-level interceptors still nest in the order you list them\u003C\u002Fstrong>, exactly like the global\u002Fcontroller\u002Fmethod scopes do — \u003Ccode>@UseInterceptors(A, B, C)\u003C\u002Fcode> makes \u003Ccode>A\u003C\u002Fcode> outermost and \u003Ccode>C\u003C\u002Fcode> innermost, all at the same method scope.","\u003Cstrong>\u003Ccode>async intercept()\u003C\u002Fcode> works fine, even though it technically returns a \u003Ccode>Promise\u003C\u002Fcode> instead of an \u003Ccode>Observable\u003C\u002Fcode>.\u003C\u002Fstrong> Nest resolves that \u003Ccode>Promise\u003C\u002Fcode> before treating the result as a stream, so \u003Ccode>async intercept(context, next) { await doSomething(); return next.handle().pipe(map(...)); }\u003C\u002Fcode> behaves correctly — you don&#39;t need \u003Ccode>from()\u003C\u002Fcode> just to use \u003Ccode>await\u003C\u002Fcode> inside an interceptor.","\u003Cstrong>Unsubscribing from a timeout doesn&#39;t cancel work the handler already started.\u003C\u002Fstrong> A \u003Ccode>timeout(5000)\u003C\u002Fcode> operator makes Nest stop \u003Cem>waiting\u003C\u002Fem>, but if the handler kicked off a database write before the timeout fired, that write still runs to completion in the background.",{"id":265,"html":266,"text":266,"type":91,"level":31},"b49","Best practices",{"id":268,"type":97,"items":269,"ordered":18},"b50",[270,271,272,273,274],"\u003Cstrong>Order by scope first, list order second.\u003C\u002Fstrong> Decide which cross-cutting concerns (logging, auth context, response envelope) must see \u003Cem>every\u003C\u002Fem> response regardless of what a more specific interceptor does, and put those at the outer scope — global over controller over method — rather than trying to win with list order inside one scope.","\u003Cstrong>Short-circuiting interceptors (cache, dedupe) belong as far inward as the response contract allows\u003C\u002Fstrong>, nested \u003Cem>inside\u003C\u002Fem> anything that must re-evaluate per caller — authorization flags, user-specific fields, anything derived from &quot;who is asking.&quot;","\u003Cstrong>Prefer \u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode> over \u003Ccode>app.useGlobalInterceptors()\u003C\u002Fcode> the moment an interceptor has a constructor dependency.\u003C\u002Fstrong> It costs nothing and keeps the interceptor testable.","\u003Cstrong>Keep one interceptor doing one thing.\u003C\u002Fstrong> A single interceptor that logs, caches, \u003Cem>and\u003C\u002Fem> transforms is unreadable to reorder safely later — the stacking bug above is much easier to spot in review when each interceptor is a named, single-purpose ring.","\u003Cstrong>When a bug looks like &quot;sometimes the response has stale or wrong per-user data,&quot; suspect interceptor ordering before suspecting the data layer.\u003C\u002Fstrong> A field that&#39;s correct for one caller and wrong for the next, correlated with a cache hit, is a strong signal to check which interceptor is nested inside which.",{"id":276,"html":277,"text":277,"type":91,"level":31},"b51","FAQ",{"id":279,"html":280,"text":280,"type":91,"level":43},"b52","Does interceptor order matter if neither one short-circuits?",{"id":282,"html":283,"type":83},"b53","\u003Cp>Less, but not never. If both interceptors always call \u003Ccode>next.handle()\u003C\u002Fcode> and only add side effects or transforms, the \u003Cem>pre-handler\u003C\u002Fem> logic still runs outer-to-inner and the \u003Cem>post-handler\u003C\u002Fem> logic still runs inner-to-outer — so a \u003Ccode>tap()\u003C\u002Fcode> that logs &quot;before&quot; timing in one interceptor and a \u003Ccode>map()\u003C\u002Fcode> that reshapes data in another can still produce a different final object depending on which one is outermost, even though both always run.\u003C\u002Fp>",{"id":285,"html":286,"text":286,"type":91,"level":43},"b54","Can I make a method-level interceptor run before a global one?",{"id":288,"html":289,"type":83},"b55","\u003Cp>No. The scope ordering (global, then controller, then method) is fixed by Nest&#39;s request pipeline and isn&#39;t something \u003Ccode>@UseInterceptors()\u003C\u002Fcode> list order can override — list order only controls nesting \u003Cem>within\u003C\u002Fem> a single scope.\u003C\u002Fp>",{"id":291,"html":292,"text":293,"type":91,"level":43},"b56","What&#39;s the difference between an interceptor and a guard for this kind of short-circuiting?","What's the difference between an interceptor and a guard for this kind of short-circuiting?",{"id":295,"html":296,"type":83},"b57","\u003Cp>A guard&#39;s \u003Ccode>canActivate()\u003C\u002Fcode> returns a boolean before any interceptor runs at all — it rejects or allows the whole request, with no concept of wrapping a result. An interceptor wraps the \u003Ccode>Observable\u003C\u002Fcode> the handler (or the next interceptor) produces, so it can transform, log, cache, or replace that result, but by the time an interceptor runs, a guard has already allowed the request through.\u003C\u002Fp>",{"id":298,"html":299,"text":300,"type":91,"level":43},"b58","Does \u003Ccode>ClassSerializerInterceptor\u003C\u002Fcode> have the same ordering rules?","Does ClassSerializerInterceptor have the same ordering rules?",{"id":302,"html":303,"type":83},"b59","\u003Cp>Yes — it&#39;s a regular interceptor, usually bound via \u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode>, so it sits at whatever scope you register it at and follows the same nesting as any other interceptor relative to others at different scopes.\u003C\u002Fp>",{"id":305,"html":306,"text":306,"type":91,"level":43},"b60","Will a new NestJS version change this ordering?",{"id":308,"html":309,"type":83},"b61","\u003Cp>The global → controller → method scope order and the onion composition are structural to how Nest builds the pipeline, not a detail that has changed across the 10.x–12.x line. Always verify behavior you depend on against the version you&#39;re running if it matters for production correctness.\u003C\u002Fp>",{"id":311,"html":312,"text":312,"type":91,"level":31},"b62","Cheat sheet",{"id":314,"head":315,"rows":319,"type":368},"b63",[316,317,318],"Task","Code","Notes",[320,324,328,332,336,340,344,348,352,356,360,364],[321,322,323],"Define an interceptor","\u003Ccode>intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt;\u003C\u002Fcode>","Must return an \u003Ccode>Observable\u003C\u002Fcode>, usually derived from \u003Ccode>next.handle()\u003C\u002Fcode>",[325,326,327],"Run the handler","\u003Ccode>next.handle()\u003C\u002Fcode>","Returns an \u003Ccode>Observable\u003C\u002Fcode> of the handler&#39;s (or next interceptor&#39;s) result",[329,330,331],"Transform the response","\u003Ccode>next.handle().pipe(map(data =&gt; ({ data })))\u003C\u002Fcode>","Runs after the handler, on the way back out",[333,334,335],"Log around the handler","\u003Ccode>next.handle().pipe(tap(() =&gt; log()))\u003C\u002Fcode>","\u003Ccode>tap\u003C\u002Fcode> doesn&#39;t change the emitted value",[337,338,339],"Short-circuit (skip the handler)","\u003Ccode>return of(cachedValue)\u003C\u002Fcode>","Never calls \u003Ccode>next.handle()\u003C\u002Fcode> — nothing nested inside this interceptor runs",[341,342,343],"Map errors","\u003Ccode>next.handle().pipe(catchError(() =&gt; throwError(() =&gt; new BadGatewayException())))\u003C\u002Fcode>","Only catches errors from inside \u003Ccode>next.handle()\u003C\u002Fcode>, not thrown before it",[345,346,347],"Add a timeout","\u003Ccode>next.handle().pipe(timeout(5000), catchError(mapTimeoutErr))\u003C\u002Fcode>","Stops waiting; does not cancel in-flight work the handler started",[349,350,351],"Bind at method\u002Fcontroller scope","\u003Ccode>@UseInterceptors(A, B)\u003C\u002Fcode>","\u003Ccode>A\u003C\u002Fcode> wraps \u003Ccode>B\u003C\u002Fcode>: \u003Ccode>A\u003C\u002Fcode> is outer, \u003Ccode>B\u003C\u002Fcode> is inner",[353,354,355],"Bind globally, no DI","\u003Ccode>app.useGlobalInterceptors(new X())\u003C\u002Fcode>","Constructor dependencies cannot be injected",[357,358,359],"Bind globally, with DI","\u003Ccode>{ provide: APP_INTERCEPTOR, useClass: X }\u003C\u002Fcode> in a module&#39;s \u003Ccode>providers\u003C\u002Fcode>","Outermost of all scopes; still goes through the container",[361,362,363],"Execution order (inbound)","global → controller → method → handler","Fixed; list order only applies within one scope",[365,366,367],"Execution order (outbound)","handler → method → controller → global","Exact reverse of inbound","table",{"id":370,"html":371,"text":371,"type":91,"level":31},"b64","Key takeaways",{"id":373,"type":97,"items":374,"ordered":18},"b65",[375,376,377,378,379],"\u003Ccode>@UseInterceptors(A, B)\u003C\u002Fcode> nests \u003Ccode>A\u003C\u002Fcode> around \u003Ccode>B\u003C\u002Fcode>, not &quot;A then B&quot; — the first interceptor named is outermost.","\u003Ccode>next.handle()\u003C\u002Fcode> invokes everything nested inside the current interceptor; skip it, and nothing inside that ring runs — including an interceptor that was supposed to apply to every response.","Global interceptors always wrap controller interceptors, which always wrap method interceptors. Scope order is fixed; list order only matters within a scope.","A per-caller field (ownership, role, visibility) that&#39;s wrong specifically on a cache hit is a strong signal to check interceptor nesting before the data layer.","Reach for \u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode> the moment an interceptor needs a constructor dependency — \u003Ccode>app.useGlobalInterceptors(new X())\u003C\u002Fcode> can&#39;t provide one.",{"id":381,"html":382,"type":83},"b66","\u003C!-- playground:start -->",{"id":384,"html":385,"text":385,"type":91,"level":31},"b67","🎮 Try it yourself",{"id":387,"html":388,"type":83},"b68","\u003Cp>\u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-interceptor-order\u002Fplayground\">▶️ Open the interactive playground →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":390,"html":391,"type":83},"b69","\u003Cp>\u003Cem>Runs right in your browser — poke at it and watch the concept react live.\u003C\u002Fem>\u003C\u002Fp>",{"id":393,"html":394,"type":83},"b70","\u003C!-- playground:end -->",{"id":396,"html":397,"type":83},"b71","\u003Cp>Interceptor nesting is easier to see than to read about. The playground below lets you pick the order of a \u003Ccode>CacheInterceptor\u003C\u002Fcode> and an \u003Ccode>OwnershipInterceptor\u003C\u002Fcode>, send a simulated request from the document&#39;s owner followed by a request from a different user for the same document, and watch exactly which ring the second request never reaches.\u003C\u002Fp>",{"id":399,"html":400,"type":83},"b72","\u003C!-- quiz:start -->",{"id":402,"html":403,"text":403,"type":91,"level":31},"b73","🧠 Test yourself",{"id":405,"html":406,"type":83},"b74","\u003Cp>Think it clicked? \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-interceptor-order\u002Fquiz\">Take the 9-question quiz →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":408,"html":409,"type":83},"b75","\u003Cp>\u003Cem>Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.\u003C\u002Fem>\u003C\u002Fp>",{"id":411,"html":412,"type":83},"b76","\u003C!-- quiz:end -->",{"id":414,"html":415,"type":83},"b77","\u003Cp>This series has already covered two other pieces of the same request pipeline: \u003Ca href=\"https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-guards-canactivate-executioncontext-reflector-1onm\">how guards decide who reaches a handler\u003C\u002Fa>, and \u003Ca href=\"https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-request-lifecycle-explained-with-cheat-sheet-227l\">the full middleware → guard → interceptor → pipe → handler order\u003C\u002Fa> interceptors are one stop inside. If constructor injection into a provider like \u003Ccode>CacheStore\u003C\u002Fcode> isn&#39;t fully clear yet, \u003Ca href=\"https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-dependency-injection-explained-with-cheat-sheet-4bem\">the DI container episode\u003C\u002Fa> builds that mental model from scratch.\u003C\u002Fp>",{"id":417,"html":418,"type":83},"b78","\u003Cp>So: has an interceptor you stacked ever produced a response that looked right for one caller and wrong for the next, with nothing in the data layer to blame? What gave it away first — the client, a test, or a support ticket?\u003C\u002Fp>",{"id":420,"html":421,"type":83},"b79","\u003C!-- related:start -->",{"id":423,"html":424,"text":424,"type":91,"level":31},"b80","📚 Read next",{"id":426,"type":97,"items":427,"ordered":18},"b81",[428,429,430],"\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-lifecycle-hook-order\">NestJS 12 Lifecycle Hooks: The Order You Can&#39;t Assume\u003C\u002Fa>","\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-standard-schema-validation\">NestJS 12: Standard Schema Validation Without class-validator\u003C\u002Fa>","\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-testing-provider-overrides\">NestJS Testing Module: Provider Overrides (with Cheat Sheet)\u003C\u002Fa>",{"id":432,"html":433,"type":83},"b82","\u003C!-- related:end -->",{"id":435,"type":436},"b83","divider",{"id":438,"html":439,"type":83},"b84","\u003Cp>🚀 \u003Cstrong>Want more like this?\u003C\u002Fstrong> Every guide, playground, and quiz lives on \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">bestpractic.org\u003C\u002Fa>\u003C\u002Fstrong> — open it and \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">sign up free\u003C\u002Fa>\u003C\u002Fstrong> so the next one finds you.\u003C\u002Fp>",{"id":441,"html":442,"type":83},"b85","\u003Cp>\u003Cem>Thanks for reading! Let&#39;s stay connected:\u003C\u002Fem>\u003C\u002Fp>",{"id":444,"type":97,"items":445,"ordered":18},"b86",[446,447,448],"⭐ \u003Cstrong>GitHub\u003C\u002Fstrong> — follow me and star the projects: \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fparsajiravand\">github.com\u002Fparsajiravand\u003C\u002Fa>","💬 \u003Cstrong>Discord\u003C\u002Fstrong> — join the frontend best-practices community: \u003Ca href=\"https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ\">discord.gg\u002Fd9KRhuAwQ\u003C\u002Fa>","📸 \u003Cstrong>Instagram\u003C\u002Fstrong> — frontend best practices, daily: \u003Ca href=\"https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F\">@bestpractice___\u003C\u002Fa>","User 42 fetches `GET \u002Fdocuments\u002F7`, the document they own. The response includes `isOwner: true`. A minute later, user 7 — who has read access but doesn't own it — fetches the exact same document. The response *also* says `isOwner: true`. User 7 now sees an \"Edit\" button a permission check downstream will eventually reject, but the UI already lied to them, and a client that trusts `isOwner` to gate a destructive action has a real authorization bug, not a cosmetic one.\n\nNobody touched the authorization logic. Nobody touched the DTO. Both interceptors involved are individually correct, straight from patterns NestJS's own documentation shows. The bug is entirely in the order two `@UseInterceptors()` arguments were listed — and until you know what that list actually builds, the order looks arbitrary.\n\n## What you'll learn\n\nBy the end of this article you'll be able to:\n\n- Explain what `intercept(context, next)` and `CallHandler.handle()` actually do to a request, not just that you're supposed to call them\n- Predict the exact execution order when interceptors are stacked at the global, controller, and method level\n- Recognize why an interceptor that skips `next.handle()` (like a cache) silently skips every interceptor nested inside it too\n- Fix a stale-personalization bug caused by interceptor order, instead of chasing it through the DTO and the authorization layer\n- Choose between method-level `@UseInterceptors()`, `app.useGlobalInterceptors()`, and the `APP_INTERCEPTOR` provider token\n\n## Who this is for\n\nYou've written at least one NestJS controller with `@UseGuards()` or `@UseInterceptors()` on it, even if you copied the interceptor from the docs without fully tracing what it does. You don't need prior RxJS experience — the handful of operators used here (`tap`, `map`, `catchError`, `of`) are explained as they appear.\n\nThis article is written against **NestJS 12.x** (`@nestjs\u002Fcore` is at `12.6.0` on npm `latest`, with `11.2.11` the last `legacy`-tagged 11.x release). The interceptor API — `NestInterceptor`, `CallHandler`, `ExecutionContext` — and the global → controller → route composition order are core-container behavior, unchanged in shape since well before v12.\n\n## Table of contents\n\n- [The problem: a cache that leaks the wrong user's permissions](#the-problem-a-cache-that-leaks-the-wrong-users-permissions)\n- [The mental model: interceptors are nested, not listed](#the-mental-model-interceptors-are-nested-not-listed)\n- [Stage 1: what `next.handle()` actually is](#stage-1-what-nexthandle-actually-is)\n- [Stage 2: composing two interceptors by hand](#stage-2-composing-two-interceptors-by-hand)\n- [Stage 3: the cache-and-personalization bug, reproduced](#stage-3-the-cache-and-personalization-bug-reproduced)\n- [Stage 4: global, controller, and method — the fixed nesting](#stage-4-global-controller-and-method--the-fixed-nesting)\n- [Stage 5: binding without breaking DI](#stage-5-binding-without-breaking-di)\n- [Edge cases and gotchas](#edge-cases-and-gotchas)\n- [Best practices](#best-practices)\n- [FAQ](#faq)\n- [Cheat sheet](#cheat-sheet)\n- [Key takeaways](#key-takeaways)\n\n## The problem: a cache that leaks the wrong user's permissions\n\nHere's the setup that produced the bug above. Two interceptors, each individually correct and each a straightforward extension of a pattern NestJS's own documentation shows:\n\n```typescript\n\u002F\u002F Adds a per-request field: does the CURRENT caller own this document?\n@Injectable()\nexport class OwnershipInterceptor implements NestInterceptor {\n  intercept(context: ExecutionContext, next: CallHandler): Observable\u003Cany> {\n    const { user } = context.switchToHttp().getRequest();\n    return next.handle().pipe(\n      map((doc) => ({ ...doc, isOwner: doc.ownerId === user.id })),\n    );\n  }\n}\n\n\u002F\u002F Caches the raw document by id — the document itself is the same for\n\u002F\u002F every viewer, so it's cached once, regardless of who's asking.\n@Injectable()\nexport class CacheInterceptor implements NestInterceptor {\n  constructor(private cache: CacheStore) {}\n  intercept(context: ExecutionContext, next: CallHandler): Observable\u003Cany> {\n    const key = context.switchToHttp().getRequest().params.id;\n    const cached = this.cache.get(key);\n    if (cached) return of(cached);\n    return next.handle().pipe(tap((doc) => this.cache.set(key, doc)));\n  }\n}\n```\n\nBoth are individually reasonable: caching a shared resource by its id is a normal optimization, and stamping a per-caller `isOwner` flag onto a response is a normal personalization. Bound on the controller like this, reading top to bottom as \"first cache, then personalize\":\n\n```typescript\n@UseInterceptors(CacheInterceptor, OwnershipInterceptor)\n@Get(':id')\nfindOne(@Param('id') id: string) {\n  return this.documents.findOne(id);\n}\n```\n\nUser 42 (the owner) requests document 7, a cache miss: `CacheInterceptor` calls `next.handle()`, which runs `OwnershipInterceptor`, which runs the handler, gets back `{ id: 7, ownerId: 42 }`, and stamps `isOwner: true` because the current request's user is 42. `CacheInterceptor`'s `tap()` then caches that *already-personalized* object — `{ id: 7, ownerId: 42, isOwner: true }` — keyed only by the document id.\n\nUser 7 (not the owner) requests the same document 7 next: `CacheInterceptor` finds a cached entry for key `7` and returns `of(cached)` directly — it never calls `next.handle()`, so `OwnershipInterceptor` never runs for *this* request. The response sent to user 7 is the exact object cached from user 42's request: `isOwner: true`. User 7 is told they own a document they don't, because the only code that could have corrected the flag for the new caller never ran.\n\n## The mental model: interceptors are nested, not listed\n\n**The mental model:** `@UseInterceptors(A, B)` does not run `A` then `B` as two independent steps. It builds `A` wrapping `B` wrapping the handler — one nested call, not a flat list. The first interceptor you name becomes the **outermost** layer; the last one you name sits **closest to the handler**.\n\nThis matters because `next.handle()` is not \"move to the next item in a list.\" It is \"invoke everything nested inside me, and give me back its result as an `Observable`.\" When `A` calls `next.handle()`, that call *is* `B`'s entire `intercept()` execution, including whatever `B` does before and after its own `next.handle()`. If `A` never calls `next.handle()` — because it short-circuits, exactly like `CacheInterceptor` does on a hit — then `B` never runs. Not \"runs with stale data.\" Does not run, at all, on that request.\n\nThis is the same onion composition Express and Koa middleware use, and it is also how NestJS composes guards, interceptors, and pipes across scopes (confirmed by the framework's own request-lifecycle documentation): code that runs *before* the nested call executes in registration order, outermost first; code that runs *after* — the RxJS operators chained onto the `Observable` that `next.handle()` returns — unwinds in the reverse order, innermost first. Draw it as concentric rings, not a pipeline of boxes, and the cache bug above stops being surprising: `CacheInterceptor` is the outer ring, so skipping its `next.handle()` cuts out everything inside that ring, `OwnershipInterceptor` included.\n\n## Stage 1: what `next.handle()` actually is\n\nEvery `NestInterceptor` implements one method:\n\n```typescript\ninterface NestInterceptor\u003CT = any, R = any> {\n  intercept(context: ExecutionContext, next: CallHandler\u003CT>): Observable\u003CR>;\n}\n```\n\n`context: ExecutionContext` is the same object a guard sees — it carries the request, the controller class, and the handler reference. `next: CallHandler` has exactly one method, `handle()`, which invokes the rest of the chain (the next interceptor, or the route handler if there is none left) and returns an `Observable` of its eventual result.\n\n**Key concept:** code written *before* `return next.handle()...` runs before the handler. Code chained onto the `Observable` that `next.handle()` returns — via RxJS operators like `tap`, `map`, or `catchError` — runs after the handler resolves, and can transform, log, or replace its result. If an interceptor never calls `next.handle()` and never returns an equivalent `Observable`, the request hangs forever: no response is ever emitted, because nothing inside Nest's response pipeline was asked to produce one.\n\n```typescript\n@Injectable()\nexport class LoggingInterceptor implements NestInterceptor {\n  intercept(context: ExecutionContext, next: CallHandler): Observable\u003Cany> {\n    const start = Date.now();\n    return next.handle().pipe(\n      tap(() => console.log(`${context.getHandler().name} took ${Date.now() - start}ms`)),\n    );\n  }\n}\n```\n\nThis is the minimal correct shape: do setup, call `next.handle()`, pipe a side effect onto the result, return it unchanged. Nothing here reorders anything else — it's a single ring with nothing nested complicated inside it.\n\n## Stage 2: composing two interceptors by hand\n\nTo see the nesting directly, strip away the decorator and write what `@UseInterceptors(A, B)` effectively builds for a single request:\n\n```typescript\nfunction runStack(interceptors: NestInterceptor[], handler: () => Observable\u003Cany>, context: ExecutionContext) {\n  const terminal: CallHandler = { handle: handler };\n  \u002F\u002F Build from the LAST interceptor outward, so the FIRST interceptor ends up outermost.\n  const chain = interceptors.reduceRight\u003CCallHandler>(\n    (next, interceptor) => ({ handle: () => interceptor.intercept(context, next) }),\n    terminal,\n  );\n  return chain.handle();\n}\n```\n\n`reduceRight` walks the array back-to-front, so the *last* interceptor in the array is wrapped first (innermost), and each one before it wraps the previous result — leaving the *first* interceptor in the array as the outermost `CallHandler.handle()` that actually gets invoked. That's a simplified model of the mechanism behind \"first named, outermost\" — Nest's own implementation builds the same nesting through lazy, forward recursion rather than `reduceRight`, but the resulting order is identical: first interceptor outermost, applied once per scope level.\n\n## Stage 3: the cache-and-personalization bug, reproduced\n\nSwap the order from the problem section, and the leak closes, because now `OwnershipInterceptor` is outermost and `CacheInterceptor` is innermost:\n\n```typescript\n@UseInterceptors(OwnershipInterceptor, CacheInterceptor)\n@Get(':id')\nfindOne(@Param('id') id: string) {\n  return this.documents.findOne(id);\n}\n```\n\nWalk the same two requests through this order. User 42, cache miss: `OwnershipInterceptor` (outer) calls `next.handle()`, which reaches `CacheInterceptor` (inner), which misses, calls the handler, gets `{ id: 7, ownerId: 42 }`, and caches *that raw object* — no `isOwner` field at all, because personalization hasn't happened yet at this ring. The raw object flows back out to `OwnershipInterceptor`, which stamps `isOwner: true` for user 42. Response: `{ id: 7, ownerId: 42, isOwner: true }`.\n\nUser 7, same document: `OwnershipInterceptor` (outer) still calls `next.handle()` — it has no reason not to, and nothing outside it short-circuited. `CacheInterceptor` (inner) finds a hit and returns the cached *raw* object without running the handler again. That raw object flows back out to `OwnershipInterceptor`, which — because it is never skipped when it's the outer ring — stamps a fresh `isOwner` field for *this* request's user: `7 === 42` is false. Response: `{ id: 7, ownerId: 42, isOwner: false }`. Correct, every time, for every caller, regardless of which request happened to populate the cache.\n\n**Key concept:** a ring that must re-evaluate something per request — who's asking, what they're allowed to see — has to sit *outside* anything that can short-circuit, never inside it. `CacheInterceptor`'s short-circuit only ever skips what's nested inside `CacheInterceptor`; it can never skip a ring wrapped around it.\n\n## Stage 4: global, controller, and method — the fixed nesting\n\nThe same onion extends across scopes, and you cannot reorder it locally. For a single inbound request, Nest's own request-lifecycle documentation puts interceptors in this order going in, and the exact reverse coming back out:\n\n1. **Global** interceptors (bound via `APP_INTERCEPTOR` or `app.useGlobalInterceptors()`) — outermost.\n2. **Controller-level** interceptors (`@UseInterceptors()` on the class).\n3. **Method-level** interceptors (`@UseInterceptors()` on the route handler) — innermost, closest to the actual handler.\n\nOn the way back out, it's the reverse: method-level interceptors' post-handler logic runs first, then controller-level, then global last. A method-level `CacheInterceptor` can never be made to wrap a global `LoggingInterceptor`, no matter which order you list decorators in — global is always the outer ring. If you need a cache to be able to skip a cross-cutting concern like the global logger, the logger has to not depend on seeing every response in the first place, or the cache has to move to a scope at or above it.\n\n## Stage 5: binding without breaking DI\n\nThree ways to attach an interceptor, each with a different relationship to Nest's DI container:\n\n```typescript\n\u002F\u002F Method or controller scope — Nest constructs it through DI\n@UseInterceptors(CacheInterceptor)\n\n\u002F\u002F Global, but constructed by hand — no constructor injection works here\napp.useGlobalInterceptors(new LoggingInterceptor());\n\n\u002F\u002F Global, constructed through DI — constructor injection, module scope, and\n\u002F\u002F test overrides via overrideProvider() all keep working\nproviders: [{ provide: APP_INTERCEPTOR, useClass: CacheInterceptor }]\n```\n\n`CacheInterceptor` above takes a `CacheStore` in its constructor. `app.useGlobalInterceptors(new CacheInterceptor(???))` has no way to provide that dependency — you're back to constructing it by hand outside the container, the same trap `app.useGlobalGuards()` sets for a guard that needs a service injected. `APP_INTERCEPTOR` fixes it the same way `APP_GUARD` does: register it as a provider, let Nest instantiate it through DI, and it becomes global while still receiving its dependencies — and while still being swappable in tests with `Test.createTestingModule().overrideProvider(CacheStore)`.\n\n## Edge cases and gotchas\n\n- **An interceptor that never calls `next.handle()` still has to return something RxJS can treat as a stream.** Return a bare value, or nothing at all, and Nest throws immediately — a `TypeError` like \"You provided 'undefined' where a stream was expected\" — which lands in your exception filters. It doesn't hang; there's no silent stall to debug. If you're building a caching or short-circuiting interceptor, always return an `Observable` explicitly — `of(value)` is the simplest correct shape.\n- **A synchronous throw inside `intercept()` doesn't just skip straight past every interceptor wrapped around it.** Nest invokes each interceptor inside a `try`\u002F`catch` and forwards a synchronous throw via the subscriber's `error()` callback on the `Observable` that invocation built — which is exactly the `Observable` an *outer* interceptor's `next.handle()` returned. So even a throw that happens *before* this interceptor's own `next.handle()` call surfaces as a stream error there, and that outer interceptor's `catchError()` can and does see it. The error only lands directly in Nest's exception filters, the same as a guard or pipe error, when nothing further out is there to catch it (the throw happens in the outermost interceptor, or no outer interceptor pipes a `catchError`).\n- **Multiple method-level interceptors still nest in the order you list them**, exactly like the global\u002Fcontroller\u002Fmethod scopes do — `@UseInterceptors(A, B, C)` makes `A` outermost and `C` innermost, all at the same method scope.\n- **`async intercept()` works fine, even though it technically returns a `Promise` instead of an `Observable`.** Nest resolves that `Promise` before treating the result as a stream, so `async intercept(context, next) { await doSomething(); return next.handle().pipe(map(...)); }` behaves correctly — you don't need `from()` just to use `await` inside an interceptor.\n- **Unsubscribing from a timeout doesn't cancel work the handler already started.** A `timeout(5000)` operator makes Nest stop *waiting*, but if the handler kicked off a database write before the timeout fired, that write still runs to completion in the background.\n\n## Best practices\n\n- **Order by scope first, list order second.** Decide which cross-cutting concerns (logging, auth context, response envelope) must see *every* response regardless of what a more specific interceptor does, and put those at the outer scope — global over controller over method — rather than trying to win with list order inside one scope.\n- **Short-circuiting interceptors (cache, dedupe) belong as far inward as the response contract allows**, nested *inside* anything that must re-evaluate per caller — authorization flags, user-specific fields, anything derived from \"who is asking.\"\n- **Prefer `APP_INTERCEPTOR` over `app.useGlobalInterceptors()` the moment an interceptor has a constructor dependency.** It costs nothing and keeps the interceptor testable.\n- **Keep one interceptor doing one thing.** A single interceptor that logs, caches, *and* transforms is unreadable to reorder safely later — the stacking bug above is much easier to spot in review when each interceptor is a named, single-purpose ring.\n- **When a bug looks like \"sometimes the response has stale or wrong per-user data,\" suspect interceptor ordering before suspecting the data layer.** A field that's correct for one caller and wrong for the next, correlated with a cache hit, is a strong signal to check which interceptor is nested inside which.\n\n## FAQ\n\n### Does interceptor order matter if neither one short-circuits?\n\nLess, but not never. If both interceptors always call `next.handle()` and only add side effects or transforms, the *pre-handler* logic still runs outer-to-inner and the *post-handler* logic still runs inner-to-outer — so a `tap()` that logs \"before\" timing in one interceptor and a `map()` that reshapes data in another can still produce a different final object depending on which one is outermost, even though both always run.\n\n### Can I make a method-level interceptor run before a global one?\n\nNo. The scope ordering (global, then controller, then method) is fixed by Nest's request pipeline and isn't something `@UseInterceptors()` list order can override — list order only controls nesting *within* a single scope.\n\n### What's the difference between an interceptor and a guard for this kind of short-circuiting?\n\nA guard's `canActivate()` returns a boolean before any interceptor runs at all — it rejects or allows the whole request, with no concept of wrapping a result. An interceptor wraps the `Observable` the handler (or the next interceptor) produces, so it can transform, log, cache, or replace that result, but by the time an interceptor runs, a guard has already allowed the request through.\n\n### Does `ClassSerializerInterceptor` have the same ordering rules?\n\nYes — it's a regular interceptor, usually bound via `APP_INTERCEPTOR`, so it sits at whatever scope you register it at and follows the same nesting as any other interceptor relative to others at different scopes.\n\n### Will a new NestJS version change this ordering?\n\nThe global → controller → method scope order and the onion composition are structural to how Nest builds the pipeline, not a detail that has changed across the 10.x–12.x line. Always verify behavior you depend on against the version you're running if it matters for production correctness.\n\n## Cheat sheet\n\n| Task | Code | Notes |\n| --- | --- | --- |\n| Define an interceptor | `intercept(context: ExecutionContext, next: CallHandler): Observable\u003Cany>` | Must return an `Observable`, usually derived from `next.handle()` |\n| Run the handler | `next.handle()` | Returns an `Observable` of the handler's (or next interceptor's) result |\n| Transform the response | `next.handle().pipe(map(data => ({ data })))` | Runs after the handler, on the way back out |\n| Log around the handler | `next.handle().pipe(tap(() => log()))` | `tap` doesn't change the emitted value |\n| Short-circuit (skip the handler) | `return of(cachedValue)` | Never calls `next.handle()` — nothing nested inside this interceptor runs |\n| Map errors | `next.handle().pipe(catchError(() => throwError(() => new BadGatewayException())))` | Only catches errors from inside `next.handle()`, not thrown before it |\n| Add a timeout | `next.handle().pipe(timeout(5000), catchError(mapTimeoutErr))` | Stops waiting; does not cancel in-flight work the handler started |\n| Bind at method\u002Fcontroller scope | `@UseInterceptors(A, B)` | `A` wraps `B`: `A` is outer, `B` is inner |\n| Bind globally, no DI | `app.useGlobalInterceptors(new X())` | Constructor dependencies cannot be injected |\n| Bind globally, with DI | `{ provide: APP_INTERCEPTOR, useClass: X }` in a module's `providers` | Outermost of all scopes; still goes through the container |\n| Execution order (inbound) | global → controller → method → handler | Fixed; list order only applies within one scope |\n| Execution order (outbound) | handler → method → controller → global | Exact reverse of inbound |\n\n## Key takeaways\n\n- `@UseInterceptors(A, B)` nests `A` around `B`, not \"A then B\" — the first interceptor named is outermost.\n- `next.handle()` invokes everything nested inside the current interceptor; skip it, and nothing inside that ring runs — including an interceptor that was supposed to apply to every response.\n- Global interceptors always wrap controller interceptors, which always wrap method interceptors. Scope order is fixed; list order only matters within a scope.\n- A per-caller field (ownership, role, visibility) that's wrong specifically on a cache hit is a strong signal to check interceptor nesting before the data layer.\n- Reach for `APP_INTERCEPTOR` the moment an interceptor needs a constructor dependency — `app.useGlobalInterceptors(new X())` can't provide one.\n\n\u003C!-- playground:start -->\n\n## 🎮 Try it yourself\n\n**[▶️ Open the interactive playground →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-interceptor-order\u002Fplayground)**\n\n_Runs right in your browser — poke at it and watch the concept react live._\n\n\u003C!-- playground:end -->\n\nInterceptor nesting is easier to see than to read about. The playground below lets you pick the order of a `CacheInterceptor` and an `OwnershipInterceptor`, send a simulated request from the document's owner followed by a request from a different user for the same document, and watch exactly which ring the second request never reaches.\n\n\u003C!-- quiz:start -->\n\n## 🧠 Test yourself\n\nThink it clicked? **[Take the 9-question quiz →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-interceptor-order\u002Fquiz)**\n\n_Instant feedback, a hint on every question, and an explanation for each answer — right or wrong._\n\n\u003C!-- quiz:end -->\n\nThis series has already covered two other pieces of the same request pipeline: [how guards decide who reaches a handler](https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-guards-canactivate-executioncontext-reflector-1onm), and [the full middleware → guard → interceptor → pipe → handler order](https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-request-lifecycle-explained-with-cheat-sheet-227l) interceptors are one stop inside. If constructor injection into a provider like `CacheStore` isn't fully clear yet, [the DI container episode](https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-dependency-injection-explained-with-cheat-sheet-4bem) builds that mental model from scratch.\n\nSo: has an interceptor you stacked ever produced a response that looked right for one caller and wrong for the next, with nothing in the data layer to blame? What gave it away first — the client, a test, or a support ticket?\n\n\u003C!-- related:start -->\n\n## 📚 Read next\n\n- [NestJS 12 Lifecycle Hooks: The Order You Can't Assume](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-lifecycle-hook-order)\n- [NestJS 12: Standard Schema Validation Without class-validator](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-standard-schema-validation)\n- [NestJS Testing Module: Provider Overrides (with Cheat Sheet)](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-testing-provider-overrides)\n\n\u003C!-- related:end -->\n\n---\n\n🚀 **Want more like this?** Every guide, playground, and quiz lives on **[bestpractic.org](https:\u002F\u002Fbestpractic.org\u002F)** — open it and **[sign up free](https:\u002F\u002Fbestpractic.org\u002F)** so the next one finds you.\n\n*Thanks for reading! Let's stay connected:*\n\n- ⭐ **GitHub** — follow me and star the projects: [github.com\u002Fparsajiravand](https:\u002F\u002Fgithub.com\u002Fparsajiravand)\n- 💬 **Discord** — join the frontend best-practices community: [discord.gg\u002Fd9KRhuAwQ](https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ)\n- 📸 **Instagram** — frontend best practices, daily: [@bestpractice___](https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F)",{"title":47,"canonical":451,"description":49},"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-interceptor-order","01a1a1b0-49e9-711f-aea2-cd690567df95",{"name":454,"part":455,"total":455,"items":456},"NestJS Deep Dive",12,[457,462,467,472,476,480,484,488,492,496,500,504],{"slug":458,"title":459,"publishedAt":460,"readingMinutes":461},"nestjs-weekly-request-lifecycle","NestJS Request Lifecycle Explained (with Cheat Sheet)","2026-08-28T17:01:41.941Z",13,{"slug":463,"title":464,"publishedAt":465,"readingMinutes":466},"nestjs-weekly-dependency-injection-providers-scopes","NestJS Dependency Injection Explained (with Cheat Sheet)","2026-09-13T08:40:18.434Z",14,{"slug":468,"title":469,"publishedAt":470,"readingMinutes":471},"nestjs-weekly-module-encapsulation-exports","NestJS Module Encapsulation Explained (with Cheat Sheet)","2026-09-13T08:40:50.070Z",15,{"slug":473,"title":474,"publishedAt":475,"readingMinutes":461},"nestjs-weekly-testing-provider-overrides","NestJS Testing Module: Provider Overrides (with Cheat Sheet)","2026-09-18T17:23:03.842Z",{"slug":477,"title":478,"publishedAt":479,"readingMinutes":466},"nestjs-weekly-guards-canactivate-reflector","NestJS Guards: CanActivate, ExecutionContext & Reflector","2026-09-26T12:25:39.001Z",{"slug":481,"title":482,"publishedAt":483,"readingMinutes":466},"nestjs-weekly-standard-schema-validation","NestJS 12: Standard Schema Validation Without class-validator","2026-10-02T06:11:02.679Z",{"slug":485,"title":486,"publishedAt":487,"readingMinutes":461},"nestjs-weekly-lifecycle-hook-order","NestJS 12 Lifecycle Hooks: The Order You Can't Assume","2026-10-23T06:10:32.276Z",{"slug":489,"title":490,"publishedAt":491,"readingMinutes":471},"nestjs-weekly-optional-not-inherited","NestJS 12: @Optional() Not Inherited by a Redeclared Constructor","2026-10-30T05:36:46.066Z",{"slug":493,"title":494,"publishedAt":495,"readingMinutes":455},"nestjs-weekly-validation-error-target","NestJS 12.2: Why validationError.target Is Now undefined","2026-11-06T06:10:24.442Z",{"slug":497,"title":498,"publishedAt":499,"readingMinutes":471},"nestjs-weekly-app-filter-scope","NestJS APP_FILTER Scope: Why Your Global Filter Might Not Fire","2026-11-21T06:53:12.171Z",{"slug":501,"title":502,"publishedAt":503,"readingMinutes":466},"nestjs-weekly-platform-node-adapter","NestJS platform-node (12.3): The Adapter That Reorders Your Routes","2026-11-27T11:30:39.049Z",{"slug":46,"title":47,"publishedAt":52,"readingMinutes":51},{"id":506,"locked":18},"01a1a1b0-4a2e-75cc-a113-17defeebc3cc",[508],{"id":509,"slug":46,"title":510,"_count":511},"01a1a1b0-4a54-7050-9124-789e4b06a253","NestJS Interceptor Order: CallHandler, next.handle(), and Onion Composition",{"questions":53},[513],{"locale":13,"slug":46},{"id":509,"slug":46,"title":510,"_count":515,"questionCount":53},{"questions":53},[517,521,525,529,531,535,537,541,545,549,551,555],{"slug":518,"name":519,"articles":520},"webdev","Webdev",195,{"slug":522,"name":523,"articles":524},"javascript","Javascript",156,{"slug":526,"name":527,"articles":528},"frontend","Frontend",103,{"slug":72,"name":73,"articles":530},85,{"slug":532,"name":533,"articles":534},"css","Css",56,{"slug":69,"name":70,"articles":536},36,{"slug":538,"name":539,"articles":540},"performance","Performance",33,{"slug":542,"name":543,"articles":544},"programming","Programming",26,{"slug":546,"name":547,"articles":548},"react","React",23,{"slug":66,"name":67,"articles":550},19,{"slug":552,"name":553,"articles":554},"browser","Browser",18,{"slug":556,"name":557,"articles":461},"nextjs","Nextjs",[],{"items":560,"meta":663},[561,573,588,603,618,633,648],{"id":45,"slug":46,"title":47,"subtitle":48,"excerpt":49,"coverUrl":50,"locale":13,"readingMinutes":51,"publishedAt":52,"viewCount":562,"likeCount":19,"commentCount":19,"author":563,"vertical":564,"topic":565,"tags":566,"_count":571,"playground":572,"hasQuiz":17,"hasPlayground":17},10,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[567,568,569,570],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":69,"name":70,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":46},{"id":574,"slug":501,"title":502,"subtitle":48,"excerpt":575,"coverUrl":576,"locale":13,"readingMinutes":466,"publishedAt":503,"viewCount":577,"likeCount":19,"commentCount":19,"author":578,"vertical":579,"topic":580,"tags":581,"_count":586,"playground":587,"hasQuiz":17,"hasPlayground":17},"01a1c5e4-28dd-73bd-882b-dee70aa37883","NestJS 12.3 shipped platform-node, a built-in HTTP adapter that matches routes like Fastify, not Express. Learn the trap, plus a cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-platform-node-adapter.png",44,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[582,583,584,585],{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":66,"name":67,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":501},{"id":589,"slug":497,"title":498,"subtitle":48,"excerpt":590,"coverUrl":591,"locale":13,"readingMinutes":471,"publishedAt":499,"viewCount":592,"likeCount":19,"commentCount":19,"author":593,"vertical":594,"topic":595,"tags":596,"_count":601,"playground":602,"hasQuiz":17,"hasPlayground":17},"01a18121-99fa-77f0-83de-5cc8814cbbd4","A request-scoped APP_FILTER in NestJS can silently stop catching exceptions. Learn the fix NestJS 12 shipped, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-app-filter-scope.png",279,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[597,598,599,600],{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":66,"name":67,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":497},{"id":604,"slug":493,"title":494,"subtitle":48,"excerpt":605,"coverUrl":606,"locale":13,"readingMinutes":455,"publishedAt":495,"viewCount":607,"likeCount":19,"commentCount":19,"author":608,"vertical":609,"topic":610,"tags":611,"_count":616,"playground":617,"hasQuiz":17,"hasPlayground":17},"01a15d15-305d-77da-a29a-d16a7df8ed3f","NestJS 12.2 defaults validationError.target to false, so a custom exceptionFactory reading error.target now gets undefined. The fix, plus a cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-validation-error-target.png",351,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[612,613,614,615],{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":66,"name":67,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":493},{"id":619,"slug":489,"title":490,"subtitle":48,"excerpt":620,"coverUrl":621,"locale":13,"readingMinutes":471,"publishedAt":491,"viewCount":622,"likeCount":19,"commentCount":19,"author":623,"vertical":624,"topic":625,"tags":626,"_count":631,"playground":632,"hasQuiz":17,"hasPlayground":17},"01a11150-cc6d-7657-8fe5-ae7578a8058b","NestJS 12 stops inheriting @Optional() once a subclass redeclares its own constructor — not when it has none at all. Cheat sheet included.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-optional-not-inherited.png",437,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[627,628,629,630],{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":66,"name":67,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":489},{"id":634,"slug":485,"title":486,"subtitle":48,"excerpt":635,"coverUrl":636,"locale":13,"readingMinutes":461,"publishedAt":487,"viewCount":637,"likeCount":19,"commentCount":19,"author":638,"vertical":639,"topic":640,"tags":641,"_count":646,"playground":647,"hasQuiz":17,"hasPlayground":17},"01a1358a-158e-70df-8178-64af7e9a5932","NestJS 12 changed how lifecycle hooks are ordered. Learn what onModuleInit and onApplicationBootstrap really guarantee, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-lifecycle-hook-order.png",468,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[642,643,644,645],{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":66,"name":67,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":485},{"id":649,"slug":481,"title":482,"subtitle":48,"excerpt":650,"coverUrl":651,"locale":13,"readingMinutes":466,"publishedAt":483,"viewCount":652,"likeCount":19,"commentCount":19,"author":653,"vertical":654,"topic":655,"tags":656,"_count":661,"playground":662,"hasQuiz":17,"hasPlayground":17},"01a0f0ef-ccc8-73e8-9b53-e6ce4c7fe9ff","NestJS 12 adds Standard Schema validation for @Body(), @Query(), and @Param() — validate with Zod or Valibot, no class-validator DTO. Cheat sheet included.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-standard-schema-validation.png",785,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[657,658,659,660],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":69,"name":70,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":481},{"page":75,"perPage":664,"total":455,"totalPages":31},7,"\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003Cdiv>10\u003C\u002Fdiv>\u003Cdiv>11\u003C\u002Fdiv>\u003Cdiv>12\u003C\u002Fdiv>\u003Cdiv>13\u003C\u002Fdiv>\u003Cdiv>14\u003C\u002Fdiv>\u003Cdiv>15\u003C\u002Fdiv>\u003Cdiv>16\u003C\u002Fdiv>\u003Cdiv>17\u003C\u002Fdiv>\u003Cdiv>18\u003C\u002Fdiv>\u003Cdiv>19\u003C\u002Fdiv>\u003Cdiv>20\u003C\u002Fdiv>\u003Cdiv>21\u003C\u002Fdiv>\u003Cdiv>22\u003C\u002Fdiv>\u003Cdiv>23\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F Adds a per-request field: does the CURRENT caller own this document?\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Injectable\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">OwnershipInterceptor\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">implements\u003C\u002Fspan> \u003Cspan class=\"shj-class\">NestInterceptor\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-func\">intercept\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>context\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ExecutionContext\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> next\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CallHandler\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Observable\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>any\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n    \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> user \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> context\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">switchToHttp\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">getRequest\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> next\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">handle\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">pipe\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\n      \u003Cspan class=\"shj-func\">map\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>doc\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">...\u003C\u002Fspan>doc\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> isOwner\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> doc\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>ownerId \u003Cspan class=\"shj-oper\">===\u003C\u002Fspan> user\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>id \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\n\u003Cspan class=\"shj-cmnt\">\u002F\u002F Caches the raw document by id — the document itself is the same for\n\u003C\u002Fspan>\u003Cspan class=\"shj-cmnt\">\u002F\u002F every viewer, so it's cached once, regardless of who's asking.\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Injectable\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CacheInterceptor\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">implements\u003C\u002Fspan> \u003Cspan class=\"shj-class\">NestInterceptor\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">constructor\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">private\u003C\u002Fspan> cache\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CacheStore\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n  \u003Cspan class=\"shj-func\">intercept\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>context\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ExecutionContext\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> next\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CallHandler\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Observable\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>any\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n    \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> key \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> context\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">switchToHttp\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">getRequest\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>params\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>id;\n    \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> cached \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">this\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>cache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">get\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>key\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-kwd\">if\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>cached\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">of\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>cached\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> next\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">handle\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">pipe\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-func\">tap\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>doc\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">this\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>cache\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">set\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>key\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> doc\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">@\u003Cspan class=\"shj-class\">UseInterceptors\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-class\">CacheInterceptor\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> \u003Cspan class=\"shj-class\">OwnershipInterceptor\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n@\u003Cspan class=\"shj-class\">Get\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-str\">':id'\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-func\">findOne\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Param\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-str\">'id'\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> id\u003Cspan class=\"shj-type\">: string\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">this\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>documents\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">findOne\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>id\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">interface\u003C\u002Fspan> \u003Cspan class=\"shj-class\">NestInterceptor\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-num\">T\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> any\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> \u003Cspan class=\"shj-num\">R\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> any\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-func\">intercept\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>context\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ExecutionContext\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> next\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CallHandler\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-num\">T\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Observable\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-num\">R\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">@\u003Cspan class=\"shj-class\">Injectable\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">LoggingInterceptor\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">implements\u003C\u002Fspan> \u003Cspan class=\"shj-class\">NestInterceptor\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-func\">intercept\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>context\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ExecutionContext\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> next\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CallHandler\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Observable\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>any\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n    \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> start \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Date\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">now\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> next\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">handle\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">pipe\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\n      \u003Cspan class=\"shj-func\">tap\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> console\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">log\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-str\">`\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">${\u003C\u002Fspan>context\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">getHandler\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>name\u003Cspan class=\"shj-kwd\">}\u003C\u002Fspan>\u003Cspan class=\"shj-str\"> took \u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">${\u003C\u002Fspan>\u003Cspan class=\"shj-class\">Date\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">now\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">-\u003C\u002Fspan> start\u003Cspan class=\"shj-kwd\">}\u003C\u002Fspan>\u003Cspan class=\"shj-str\">ms`\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-func\">runStack\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>interceptors\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">NestInterceptor\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> handler\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Observable\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>any\u003Cspan class=\"shj-oper\">&gt;,\u003C\u002Fspan> context\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ExecutionContext\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> terminal\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CallHandler\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> handle\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> handler \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-cmnt\">\u002F\u002F Build from the LAST interceptor outward, so the FIRST interceptor ends up outermost.\n\u003C\u002Fspan>  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> chain \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> interceptors\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>reduceRight\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-class\">CallHandler\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>next\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> interceptor\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> handle\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> interceptor\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">intercept\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>context\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> next\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    terminal\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n  \u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> chain\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">handle\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">@\u003Cspan class=\"shj-class\">UseInterceptors\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-class\">OwnershipInterceptor\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CacheInterceptor\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n@\u003Cspan class=\"shj-class\">Get\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-str\">':id'\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-func\">findOne\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Param\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-str\">'id'\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> id\u003Cspan class=\"shj-type\">: string\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">this\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>documents\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">findOne\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>id\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F Method or controller scope — Nest constructs it through DI\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">UseInterceptors\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-class\">CacheInterceptor\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\n\u003Cspan class=\"shj-cmnt\">\u002F\u002F Global, but constructed by hand — no constructor injection works here\n\u003C\u002Fspan>app\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">useGlobalInterceptors\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">new\u003C\u002Fspan> \u003Cspan class=\"shj-class\">LoggingInterceptor\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\n\u003Cspan class=\"shj-cmnt\">\u002F\u002F Global, constructed through DI — constructor injection, module scope, and\n\u003C\u002Fspan>\u003Cspan class=\"shj-cmnt\">\u002F\u002F test overrides via overrideProvider() all keep working\n\u003C\u002Fspan>providers\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> provide\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-num\">APP_INTERCEPTOR\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> useClass\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CacheInterceptor\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>",{"locked":18,"total":19,"comments":673},[]]