Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service worker cache localisation #1453

Closed
robinglen opened this issue Jul 25, 2019 · 4 comments
Closed

Service worker cache localisation #1453

robinglen opened this issue Jul 25, 2019 · 4 comments

Comments

@robinglen
Copy link

robinglen commented Jul 25, 2019

Sorry this is not really an issue but a question / problem I was having and wasn't sure where to ask.

I'm trying to implement a service worker that will sit at the root of our website however it will need to cache assets that it is unaware of. They are coming from a collection of domain specific microservices and I don't want teams to need to release SW as part of their deployment pipeline.

Todo this when each service is built it creates it's own asset manifest similar to this:

{
    application: {
        name: 'app name,
        id: 'app-id
    },
     css: [
          { filename: 'name.hash.css' }
     ],
     js: [
          { filename: 'vendor.hash.js' }
     ],
     fonts: [
          { filename: 'font.woff2' }
     ]
 }

There is then a service that collects all our services assets manifests and generates a sw cache manifest:

{
    'app-id': ['/asset/name.hash.css','/asset/vendor.hash.js', '/asset/font.woff2']
    ...
}

This is uploaded along with our assets to a bucket that is internet facing. Then our service worker loads in this json file at boot time and passes a merged array of assets to workbox.precaching.precacheAndRoute.

This means that our service worker doesn't need to have an understanding of what our separate teams are releasing, or even if we add a new service, the cache can always be up to date. However my issue does in because we also support multiple languages.

This means I want to load different assets into the cache based on the users set language, I looked at a few different options:

So my final thought was to register a service worker dynamically based out the url:

$ curl https://www.my-site.com/en-gb/

if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register(`/{en-sw.js`);
  });
}

From here then the service worker could call the sw cache manifest which could return something like:

{
    'app-id':{
        en: ['/asset/name-en.hash.css','/asset/vendor-en.hash.js', '/asset/font.woff2'],
        de: ['/asset/name-de.hash.css','/asset/vendor-de.hash.js', '/asset/font.woff2']
    }
}

Then it would know as the en-sw to use the en array.

However this would involve either generating multiple service workers (one per language) or service workers are actually generated programmatically based on the in bound request.

At this point I'm not entirely sure if I'm over thinking this and there is a much simpler way of doing it... so just wondering if this is a problem anyone else has had and how they solved it.

Thanks,
Robin

@tomayac
Copy link
Contributor

tomayac commented Jul 26, 2019

Maybe some of the ideas outlined by @jeffposnick in this StackOverflow answer are applicable?

@jeffposnick
Copy link
Contributor

It's also worth reading https://web.dev/google-search-sw/#problem:-experiments-and-dynamism, which describes the approach the Google Search team took when implementing a service worker: they took advantage of the control offered by dynamically generating a service worker script server-side, based on the incoming HTTP request.

There's an extra burden of maintaining the server-side code, and it's a different approach when you start thinking about using the service worker API "dynamically," but it does give you the most flexibility.

@robinglen
Copy link
Author

I will check out this article. I was wondering if I was over complicating things by thinking about a dynamic service worker but it would give me the flexibility I need to handle some of our requirements.

@tomayac @jeffposnick thanks for getting back to me.

I will populate this issue with my findings incase anyone is interested in what we did.

@robinglen
Copy link
Author

robinglen commented Dec 6, 2019

@jakearchibald if you are interested here is how we went around solving our issues: https://medium.com/@robinglen/how-we-built-a-service-worker-for-our-micro-frontends-881eabfbf267

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants