A
A
Alexey Yarkov2016-12-25 18:33:42
JavaScript
Alexey Yarkov, 2016-12-25 18:33:42

How to use service-worker or angular correctly?

Previously, I used cache.manifest on the project. The file was generated by gulp after the build and in general everything worked.
Now I decided to switch to a service-worker, because I read somewhere that after NG cache.manifest will be deprecated.
And now everything seems to be good too, but there is one BUT!
My entire frontend is static, on angular 1.6. The static is given to Nginx. On the server only API on Express.
And so I build the project, update it, the service-worker connects, the files are cached, and then I need to test the work offline.
I check the Application -> Service Workers -> Offline checkbox in DevTools and everything works until the first attempt to access the API.
Over-doh @ i errors fall into the console one after another, without ceasing:

GET https://domain.ru/api/v1/user/valid net::ERR_INTERNET_DISCONNECTED

Attention, question! Can it somehow be intercepted and processed through the angular Interceptor?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2016-12-29
@yarkov

Look at what error code is returned and write its handling in the interceptor

var interceptor = ['$rootScope', '$q', function ($rootScope, $q) {

        function success(response) {
            return response;
        }

        function error(response) {
            var status = response.status; // error code

            if ((status >= 400) && (status < 500)) {
                $rootScope.broadcast("AuthError", status);
                return;
            }

            if ( (status >= 500) && (status < 600) ) {
                $rootScope.broadcast("ServerError", status);
                return;
            }

            // otherwise
            return $q.reject(response);

        }

        return function (promise) {
            return promise.then(success, error);
        }

    }];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question