UPCOMING: Magento 2 Bootcamp of four days in The Netherlands (April 29th - May 2nd)
background

May 6, 2020

Disable service workers for the Magento Admin Panel

Yireo Blog Post

It is a small annoyance when combining a PWA and Magento within the same hosting environment: The PWA replaces the original frontend and ships with a new Service Worker. But this also activates that same Service Worker for the Magento Admin Panel. Here is a small workaround to disable that service worker.

TL;DR

I have created a Magento 2 module - Yireo_DisableServiceWorkerInAdmin - that loads a JavaScript snippet in the Magento Admin Panel, that unregisters any Service Worker currently being active for that specific domain. It's a dirty job and somebody needed to do it.

Caching strategies, offline, push notifications

A PWA is able to introduce new features in the frontend progressively. This might include features like caching, in the end leading to offline strategies, and other features like push notifications. With a lot of these PWA features, it is the Service Worker that allows for the magic to happen.

The Service Worker is basically a piece of JavaScript (sw.js or serviceworker.js), custom built or generated, that is added to the page and activated within the browser. The worker process even keeps on working when the website is not available, with the browser closed or the internet not working (offline).

Not for the backend

While these Service Worker features are cool on the frontend, they are often not needed in the backend. Even worse, sometimes caching features prevent the backend from working properly. In these cases, my new Yireo_DisableServiceWorkerInAdmin extension might come in handy. It simpy unregisters any Service Worker found to be running.

Same domain or different domains?

Because Service Workers are always bound to a specific domain, this is only needed when the Magento Admin Panel and the frontend share the same domain. However, what I personally do a lot is use separate domains instead: For instance, a frontend www.example.org and a backend admin.example.org, so that cookies do not conflict, weird session attacks have less impact. Plus, I can use separate PHP processes for each, so that the frontend has a smaller memory_limit than the backend, for instance.

Anyway, using different domains is not mandatory, you can simply use the same domain for both frontend and backend. And that's where this module might come in handy.

Unregistering?

One down-side is that you can only unregister the Service Worker. With every visit on the frontend, it registers. With every visit on the backend, it registers. It feels kind of awkward, strange. But because only admin users have this impact and because the impact simply leads to some pointless JavaScript execution (while you could say that Magento is excellent in executing pointless JavaScript), this is a non-issue.

Hope you enjoy this little widget.

Posted on May 6, 2020

About the author

Author Jisse Reitsma

Jisse Reitsma is the founder of Yireo, extension developer, developer trainer and 3x Magento Master. His passion is for technology and open source. And he loves talking as well.

Sponsor Yireo

Looking for a training in-house?

Let's get to it!

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.

Do not miss out on what we say

This will be the most interesting spam you have ever read

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.