symfony / webpack-encore

A simple but powerful API for processing & compiling assets built around Webpack
https://symfony.com/doc/current/frontend.html
MIT License
2.23k stars 199 forks source link

Improve support for service workers #1153

Open codedmonkey opened 2 years ago

codedmonkey commented 2 years ago

Service workers are a big part of modern web apps and while it's technically possible to create a service worker with Encore, to my knowledge it isn't straightforward.

These hurdles can be overcome with some changes to the Encore configuration, but at this point, it still generates manifest.json and entrypoints.json files for the entry, even though they aren't required and actually collide with the web manifest spec. (why two separate specs use the same manifest.json filename is beyond me, but here we are).

Since Encore is optimized for building full web applications, having first-party support for service workers would be awesome. The JS ecosystem isn't my biggest strength, so apologies if this is already possible with plugins.

weaverryan commented 2 years ago

I don't have any direct experience with service workers yet, which is basically why this hasn't been given any specific thought in Encore. I have no idea what would be involved - but I would welcome a proposal of how we think it should work or a PR :).

Cheers!

codedmonkey commented 2 years ago

Same problem here, if I find something I will definitely try to make a PR out of it.

Looks like the "recommended" way is Google's Workbox plugin, but on first examination it doesn't fit my use-case. But it might work for other people.

Ambient-Impact commented 2 years ago

@codedmonkey I'm one of the maintainers of the Drupal Progressive Web App module, and involved in the 2.x port to Workbox. While it was probably true at some point that placing the service worker anywhere but at the root of the domain was iffy, nowadays, at least in my testing, every major browser works completely fine regardless of where the service worker is served from via the scope key in the second parameter to navigator.serviceWorker.register(). This is how we do it:

    navigator.serviceWorker
      .register(drupalSettings.pwa.installPath, {
        scope: drupalSettings.path.baseUrl
      })
      .catch(function (error) {
        // Something went wrong.
        console.warn('Serviceworker Registration failed', error);
      });

I could be mistaken, but I don't think there's any configuration necessary on the server for this?

carsonbot commented 1 week ago

Thank you for this suggestion. There has not been a lot of activity here for a while. Would you still like to see this feature? Every feature is developed by the community. Perhaps someone would like to try? You can read how to contribute to get started.