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.

[GenerateSW] Destination paths setup

See original GitHub issue

Hello,

I have configured a SSR React Application where I’m trying to use a service worker using the webpack plugin. My desired output structure would be something like:

├── assets
│   ├── fonts
│   ├── js
│   ├── precache-manifest.dabf9f8d50f363cf5fd594a1b09ac74a.js
│   └── service-worker.js
└── server
    ├── app.js
    └── index.ejs

But my problem is that there is no independency between swDest and importsDirectory properties. So when I configure the plugin with both, the service-worker does not work.

    new GenerateSW({
      swDest: 'assets/service-worker.js',
      importsDirectory: 'assets',
      runtimeCaching: [...],
    }),

If I use that pattern, I achieve my goal and I have my desired output but the problem is the natural behaviour of service worker, because when I register it the service-worker.js file has an import like this importScripts("assets/precache-manifest.dabf9f8d50f363cf5fd594a1b09ac74a.js"); So once the service worker is loaded in my webpage, it requires for the precache manifest following the same domain pattern, because I want to load it like:

  if ('serviceWorker' in navigator) {
      window.addEventListener('load', function() {
          navigator.serviceWorker.register('assets/service-worker.js');
      });
  }

Then, it tries to request the file adding an extra asset part in the url http://localhost:8080/assets/assets/precache-manifest.dabf9f8d50f363cf5fd594a1b09ac74a.js

Knowing that I need to maintain that’s structure because of architecture limitation, is there anything I can add to the webpack setup for solving the problem?

Kind regards.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
laisgomescommented, Mar 12, 2019

Hey, @diemate i have de same architecture of and need of the same solution for this, because without this importScripts() no work. Anyone have any ideia for resolve this for help us please share or even to dribble it this?

Thank’s so much!

1reaction
jeffposnickcommented, Mar 6, 2019

In general, serving your service-worker.js file from inside of an assets/ subpath is not going to work—as per the service worker specification, you need to serve your service-worker.js file from your top-level path if you want its scope to include everything on your site. (If you serve it from assets/, then the maximum scope is also going to be assets/, meaning it can’t control any of your pages that exist outside of assets/.)

There’s more info about this at https://developers.google.com/web/ilt/pwa/introduction-to-service-worker#registration_and_scope

You should use swDest: 'service-worker.js', I believe, given your setup.

I’m going to close this for now, given those restrictions imposed by the service worker specification, but let me know if that doesn’t solve the issue to your satisfaction and we can revisit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

workbox-webpack-plugin - Chrome Developers
This class supports creating a new, ready-to-use service worker file as part of the webpack compilation process. Use an instance of GenerateSW in...
Read more >
Step 1: Design the path for the output destination
Perform this step if you haven't yet designed the full destination path or paths. If you've already designed the paths, go to Step...
Read more >
Generate destination path before file upload - multer
Your issue is that when You start Your app it generates new uuid (once - at app startup) and passes as string to...
Read more >
Connect locations with optimal paths—ArcGIS Pro
Impact of altering the path type and configuration of the destinations. In the powerline example above, the accumulative cost and back-direction rasters were ......
Read more >
File-based destination configuration options for Destination SDK
This section in the destinations configuration generates the Configure ... destination and provide an Amazon S3 bucket name and folder path, ...
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