Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

How to distribute service worker? How will the API be? #7

Open
linonetwo opened this issue May 14, 2018 · 3 comments
Open

How to distribute service worker? How will the API be? #7

linonetwo opened this issue May 14, 2018 · 3 comments
Labels
need/community-input Needs input from the wider community

Comments

@linonetwo
Copy link

It's difficult to import script in service worker. There is no window or global in SW, and we can't get an object that exports by an npm package.

I think https://cdn.jsdelivr.net/npm/ipfs/dist/index.js is a good example to follow, it injects a Ipfs object to the context.

Then how will the API be? Are we going to export a function likes getFile and let user write this boilerplate code?

self.addEventListener('install', event => {
  // kick previous sw after install
  console.log('Service worker is installing.');
  event.waitUntil(self.skipWaiting());
});

self.addEventListener('fetch', event => {
  if (event.request.url.startsWith(`${self.location.origin}/${ipfsRoute}`)) {
    const multihashOrContentName = event.request.url.split(`/${ipfsRoute}/`)[1];
    console.log(`Service worker getting ${multihashOrContentName}`);
    event.respondWith(getFile(multihashOrContentName));
  }
});
@vasco-santos
Copy link
Member

The first goal here is to allow a website to import this service-worker-gateway package and use it for getting files from an IPFS node running in the service-worker, abstracting everything from the app.

@linonetwo
Copy link
Author

@vasco-santos I thought that was the goal of ipfs-service-worker?
And service-worker-gateway is letting a website have a route that will load content from IPFS.

@linonetwo
Copy link
Author

linonetwo commented May 15, 2018

sw-toolbox let user write two lines of code:

// public/my-service-worker.js
importScripts('bower_components/sw-toolbox/sw-toolbox.js'); // Update path to match your own setup

and

// index.js
navigator.serviceWorker.register('my-service-worker.js');

@vasco-santos vasco-santos added the need/community-input Needs input from the wider community label May 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
need/community-input Needs input from the wider community
Projects
None yet
Development

No branches or pull requests

2 participants