question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert "Import" to "Const" when using WorkBox from Local

See original GitHub issue

Library Affected: workbox-navigation-preload

Browser & Platform: “all browsers”

Issue or Feature Request Description: Hello, im trying to use workbox from my localhost instead of CDN, i understand that in this case i have to use and convert “Import” to “Const” as it’s been mentioned here: LINK. when i convert this line of code : import {CacheableResponse} from 'workbox-cacheable-response'; to this : const {CacheableResponse} = workbox.cacheableResponse; at it seems to be correct with no error.

but i have some trouble converting this line to Const: import * as navigationPreload from 'workbox-navigation-preload'; as it been mentioned here : LINK (creating an offline page fallback). i’ve tried : const {navigationPreload} = workbox.navigationPreload; but it’s not working and it drops an Error in Console : Uncaught TypeError: Cannot read property 'enable' of undefined

can any of you guys give me a hint on how can i convert the codes like that (mentioned in WorkBox Documentation) when i’m using workbox locally ?

i appreciate it 😉

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffposnickcommented, May 25, 2021

I don’t think that’s the right approach for what you describe. Using workbox-precaching implies that you have a build process set up to generate the list of assets to precache, and that doesn’t appear to be the case based on your example.

The easiest approach would probably be to follow the examples from workbox-recipes, along the lines of:

const {NetworkOnly, CacheOnly} = workbox.strategies;
const {registerRoute} = workbox.routing;
const {offlineFallback, warmStrategyCache} = workbox.recipes;
const {enable} = workbox.navigationPreload;

enable();

offlineFallback({
  pageFallback: '/offline/index.html',
});

// This route will make use of the navigation preload if online.
// If offline, the offlineFallback logic will kick in.
registerRoute(
  ({request}) => request.mode === 'navigate',
  new NetworkOnly(),
);

const offlineResourcesStrategy = new CacheFirst({
  cacheName: 'offline-resources',
});
// This will populate the cache of offline resources.
warmStrategyCache({
  strategy: offlineResourcesStrategy,
  urls: [
    '/offline/style.css',
    '/offline/logo.png',
    // Add any other URLs that are referenced in /offline/index.html
  ],
})

// This route will match all of the resource requests that start with /offline/
// and fulfill them from the cache.
registerRoute(
  ({url}) => url.pathname.startsWith('/offline/'),
  offlineResourcesStrategy
);
1reaction
jeffposnickcommented, May 24, 2021

The equivalent statement would be

const {enable} = workbox.navigationPreload;

// The call enable() to turn on navigation preload.
Read more comments on GitHub >

github_iconTop Results From Across the Web

workbox-sw - Chrome Developers
The workbox-sw module provides an extremely easy way to get up and running with the Workbox modules, simplifies the loading of the Workbox...
Read more >
Workbox service worker: Cannot use import statement ...
Current Answer. According to Mozilla's documentation it should work now, but keep an eye on the up-to-date implementation status in each ...
Read more >
Dynamic imports
The import(module) expression loads the module and returns a promise that resolves into a module object that contains all its exports. It can...
Read more >
JavaScript modules - MDN Web Docs
Import maps allow modules to be imported using bare module names (as in Node.js), and can also simulate importing modules from packages, both ......
Read more >
Setup PWA Workbox Webpack Plugin for React Application
In this blog we will learn how to convert a React App into a PWA using Workbox WebpackPlugin, workbox-window etc. We will do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found