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

Replace importScripts from cdn to local #29

Open
alexlundborgopticon opened this issue Jun 22, 2020 · 0 comments
Open

Replace importScripts from cdn to local #29

alexlundborgopticon opened this issue Jun 22, 2020 · 0 comments

Comments

@alexlundborgopticon
Copy link

Hi!

I'm using create-react-app and I'm trying to modify an auto-generated service worker so that it imports workbox from a local file instead of from a cdn. I don't want to eject or use react-app-rewired or similar solutions since they seem likely to break in the future.

With this tool, I've been able to append the required code at the bottom of the file, but is it possible to replace importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js"); with the appended code so that workbox is only imported locally? If that is not possible, is it possible to keep the precache-manifest import auto-generated and hard code the rest? The precache-manifest import is the only thing that I need to auto-generate because it changes over time.

Thank you!

My service worker:

/**
 * Welcome to your Workbox-powered service worker!
 *
 * You'll need to register this file in your web app and you should
 * disable HTTP caching for this file too.
 * See https://goo.gl/nhQhGp
 *
 * The rest of the code is auto-generated. Please don't update this file
 * directly; instead, make changes to your Workbox build configuration
 * and re-run your build process.
 * See https://goo.gl/2aRDsh
 */

// to be replaced
importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js");

// I want to keep this auto-generated
importScripts(
  "/precache-manifest.d88aedb260a4fc1dbfabddc4949f8bef.js"
);

self.addEventListener('message', (event) => {
  if (event.data && event.data.type === 'SKIP_WAITING') {
    self.skipWaiting();
  }
});

workbox.core.clientsClaim();

/**
 * The workboxSW.precacheAndRoute() method efficiently caches and responds to
 * requests for URLs in the manifest.
 * See https://goo.gl/S9QRab
 */
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});

workbox.routing.registerNavigationRoute(workbox.precaching.getCacheKeyForURL("/index.html"), {
  
  blacklist: [/^\/_/,/\/[^/?]+\.[^/]+$/],
});

// appended code
importScripts('/workbox/workbox-v5.1.3/workbox-sw.js');

workbox.setConfig({
  modulePathPrefix: '/workbox/workbox-v5.1.3/'
});'
});
@alexlundborgopticon alexlundborgopticon changed the title Replace url Replace importScripts from cdn to local Jun 22, 2020
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

1 participant