Created
June 25, 2024 10:49
-
-
Save ali-kamalizade/89155d28543c6d6cbeb4f18628572cd1 to your computer and use it in GitHub Desktop.
Bypassing HTTP requests when using service worker in Angular applications
This file contains hidden or 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 { Injectable } from '@angular/core'; | |
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
/** | |
* See https://angular.io/guide/service-worker-devops#bypassing-the-service-worker for more info. | |
* This interceptor is used to bypass the service worker when making HTTP requests as there are some cases where the service worker might run into issues. | |
* Since we are not using the service worker for caching HTTP requests, we can safely bypass it. | |
* Attention: make sure that this header is explicitly allowed (e.g. by your file storage provider else file fetches might fail due to CORS). | |
*/ | |
@Injectable() | |
export class ServiceWorkerBypassInterceptor implements HttpInterceptor { | |
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { | |
const newRequest = request.clone({ headers: request.headers.append('ngsw-bypass', 'true') }); | |
return next.handle(newRequest); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment