Home FAQ

Blog

Always send cookies during a page refresh for Angular v20 SSR

One issue I came across while building kothakotha was during the implementation of the like system. An anonymous like system requires tracking sessions and that meant using cookies. My issue was that while navigating on kothakotha worked as each cookie was being sent to the backend for each call made, a page refresh would not send a cookie and thus a client would not know whether they had liked a post. What we want to do is always send the cookie for every request in Angular SSR. To do this, we create a cookie forwarding interceptor.

src/app/interceptors/cookie-forwarding.interceptor.ts

import { HttpInterceptorFn } from '@angular/common/http';
import { inject, REQUEST } from '@angular/core';

export const cookieForwardingInterceptor: HttpInterceptorFn = (req, next) => {
  const request = inject(REQUEST, { optional: true }) as Request | null;
  
  let modifiedReq = req;

  if (typeof window === 'undefined' ...
blessbrah · Posted 3 days ago ·