Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Service Worker - Failed to load resource: the server responded with a status of 504 (Gateway Timeout)

I am using the Angular-CLI 1.6.6 and @angular/service-worker 5.2.5 in our Angular 5.2.5 app. Everything works fine on the local lite-server, as well as on the production server, except for one error message popping in our production environment:

Failed to load resource: the server responded with a status of 504 (Gateway Timeout)

Looking into the ngsw-worker.js script I found the lines (2466 following) where the error message above is generated:

    async safeFetch(req) {
        try {
            return await this.scope.fetch(req);
        }
        catch (err) {
            this.debugger.log(err, `Driver.fetch(${req.url})`);
            return this.adapter.newResponse(null, {
                status: 504,
                statusText: 'Gateway Timeout',
            });
        }
    } 

Console logging err in the catch puts out the following error:

    TypeError: Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': 'only-if-cached' can be set only with 'same-origin' mode
        at Driver.safeFetch (ngsw-worker.js:2464)
        at Driver.handleFetch (ngsw-worker.js:1954)
        at <anonymous>

An error that seems related to this question: What causes a Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': 'only-if-cached' can be set only with 'same-origin' mode error?

The req that generates this error is any first access to the app:

https://example.com/test/#/connect
https://example.com/test/#/map?token=[accestoken]
...

On app reload the error is not repeated.

Can anybody help me out here? Is there a bug in safeFetch() of the service worker (maybe to support HashLocationStrategy)? Do I have to change anything in my config?

like image 377
tobik Avatar asked Feb 16 '18 13:02

tobik


People also ask

What causes a 504 Gateway Timeout?

A 504 Gateway Timeout error indicates that the web server is waiting too long to respond from another server and “timing out.” There can be many reasons for this timeout: the other server is not functioning properly, overloaded, or down. The other server need not always be external (e.g. CDN, API gateway).

How do I fix 504 Gateway Timeout error in Postman?

This particular request fails, because the server sends a request to a proxy, and gets a timeout error. Your server reports this back to you as status 504. The only way to fix it is to fix the proxy (make it respond in a timely manner), or to change the server to not rely on that proxy. Both are outside your area.


2 Answers

Disabling ETag header from backend solved this issue temporarily.

like image 135
Tibin Thomas Avatar answered Sep 28 '22 16:09

Tibin Thomas


I was getting this error in chrome browser because of expired SSL certificate. Replacing the SSL certificate with a valid one was the solution.

like image 27
Ghasem Avatar answered Oct 01 '22 16:10

Ghasem