-
-
Save jacksnipe24/354048f535966a58cbff98e911e4f563 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TransferState, makeStateKey } from '@angular/platform-browser'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class BrowserStateInterceptor implements HttpInterceptor { | |
constructor( | |
private transferState: TransferState, | |
) { } | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
if (req.method !== 'GET') { | |
return next.handle(req); | |
} | |
const storedResponse: string = this.transferState.get(makeStateKey(req.url), null); | |
if (storedResponse) { | |
const response = new HttpResponse({ body: storedResponse, status: 200 }); | |
return of(response); | |
} | |
return next.handle(req); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment