Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Laravel for Service Workers?

I am learning service workers from Udacity and they are using a nodejs backend to work on service workers. Is there any way to use the same in Laravel framework. I have tried it once, but I don't know how to store the compiled html, css and js on cache. Can anyone help me? Thanks

like image 800
Dishant Kaushik Avatar asked Jun 14 '16 19:06

Dishant Kaushik


People also ask

What is the use of services in Laravel?

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.

What is service provider Laravel?

Service providers are the central place to configure your application. If you open the config/app.php file included with Laravel, you will see a providers array. These are all of the service provider classes that will be loaded for your application.

Does PWA use service workers?

Service workers are a fundamental part of a PWA. They enable fast loading (regardless of the network), offline access, push notifications, and other capabilities.


1 Answers

You don't have to use nodejs in order to work with a service worker. Just register the worker somewhere in app.js and use the install and fetch event in a seperate file (e.g. sw.js) to intervene in Http requests with:

// app.js
navigator.serviceWorker.register('sw.js', {
    scope: './'
});

// sw.js
// fetch
self.addEventListener('fetch', function(event) {
    event.respondWith(
        caches.match(event.request).then(function(response) {
            return response || fetch(event.request);
        })
    );
});
like image 198
Krishan König Avatar answered Oct 14 '22 20:10

Krishan König