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

Create a workflow for sharing scripts for service workers required in public #7425

Open
owlyowl opened this issue Jul 25, 2019 · 1 comment

Comments

@owlyowl
Copy link

owlyowl commented Jul 25, 2019

Is your proposal related to a problem?

At present I can't seem to see a clean way to use imports inside the src folder and then have those scripts available to importScripts inside a serviceWorker

The use case I'm up against is sharing an idb store where in src it's easy enough to import the idb library and create an object to initialise and look up store items.

When it then comes to using that with a service worker via importScripts I can't seem to think of a neat way to copy across the idb library code etc short of a post build event but that would then require pulling the src file out of the node_modules folder.

EG
importScripts('/scripts/idb.js'); importScripts('/scripts/idb-store.js');

idb-store.js - Located in src
`import { openDB } from 'idb';

const idbStore = {
db: null,

init: function () {
    if (idbStore.db) { return Promise.resolve(idbStore.db); }
    return openDB('messages', 1, {
        upgrade(db) {
            db.createObjectStore('pizzaOrders', { autoIncrement: true, keyPath: 'id' });
        }
    }).then(function (db) {
        return idbStore.db = db;
    });
},

pizzaOrders: function (mode) {
    return idbStore.init().then(function (db) {
        return db.transaction('pizzaOrders', 'readwrite');
    })
}

}

export default idbStore;`

I then have to clone that and alter it slightly for use in public/scripts so my service worker can access it:
`var idbStore = {
db: null,

init: function () {
    if (idbStore.db) { return Promise.resolve(idbStore.db); }
    return openDB('messages', 1, {
        upgrade(db) {
            db.createObjectStore('pizzaOrders', { autoIncrement: true, keyPath: 'id' });
        }
    }).then(function (db) {
        return idbStore.db = db;
    });
},

pizzaOrders: function (mode) {
    return idbStore.init().then(function (db) {
        return db.transaction('pizzaOrders', 'readwrite');
    })
}

}`

The main problem is more so the node module idb I'm then using as I have to make a copy of it to put in the public/scripts/ folder so the service worker can use it.

Is there presently a better way?

@eniolorunda73
Copy link

Can I use phone to create app

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

No branches or pull requests

2 participants