Skip to content

Instantly share code, notes, and snippets.

Last active November 8, 2022 16:57
What would you like to do?
import {Injectable} from '@angular/core';
import {HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
import {of} from 'rxjs';
import {makeStateKey, TransferState} from '@angular/platform-browser';
export class BrowserStateInterceptor implements HttpInterceptor {
constructor(private transferState: TransferState) {}
intercept(request: HttpRequest<unknown>, next: HttpHandler) {
if (request.method === 'GET') {
const key = makeStateKey(request.url);
const storedResponse = this.transferState.get(makeStateKey(key), undefined);
if (storedResponse) {
const response = new HttpResponse({body: storedResponse, status: 200});
return of(response);
return next.handle(request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment