Sapper and Service Workers
Asked Answered
T

3

7

This is more of a service worker question, although it could be more specific to Sapper. I really don't know, as I am the first to admin I struggle with Service Workers, have barely used them, and feel like they are a pain the butt very often.

Basically, no matter what I do, I cannot get localhost:3000 to stop loading the old copy of the app. I've unregistered the service worker in various ways, including try to programmatically unregister. I've clear caches, and even cleared all browsing data from the browser. The server from my Sapper dev environment is NOT running.

This is happening with Brave, but behaves the same in Opera, and seems like a general Chromium scenario. I don't user Firefox or Safari, but may test in one soon to see what behavior happens there.

Here is a clip showing how I try to unregister the Service Worker.

https://youtu.be/Cb24I_fEklE

Thusly answered 10/2, 2020 at 17:59 Comment(0)
K
7

I used this little trick that works like a charm. In your rollup.config.js, there's a serviceWorker object in the outputs object.

serviceworker: {
    input: config.serviceworker.input(),
    output: config.serviceworker.output(),
    plugins: [
      resolve(),
      replace({
        "process.browser": true,
        "process.env.NODE_ENV": JSON.stringify(mode),
      }),
      commonjs(),
      !dev && terser(),
    ],

    preserveEntrySignatures: false,
    onwarn,
  },

Define a variable dev on top if not already declared:

const dev = process.env.NODE_ENV === "development";

Now change your service worker config like this:

serviceworker: !dev && {
    input: config.serviceworker.input(),
    output: config.serviceworker.output(),
    plugins: [
      resolve(),
      replace({
        "process.browser": true,
        "process.env.NODE_ENV": JSON.stringify(mode),
      }),
      commonjs(),
      !dev && terser(),
    ],

    preserveEntrySignatures: false,
    onwarn,
  },
Knifeedged answered 23/9, 2020 at 5:27 Comment(0)
B
0

Clear cache, the only thing that works for me to bypass it.

The issue is the service worker is service from the cache, serving from the cache again probably resets this item in the cache as even more valid to send and you get caught in something of a cycle.

--

I found this question because I was considering completely removing the service worker to try to get the performance of my site a little higher...

Is it critically necessary? What are the benefits of it?

Belch answered 12/9, 2020 at 19:56 Comment(2)
I think you can safely remove. Benefits are for accessing resources offline. Anybody else have comments on that?Thusly
@Thusly I removed mine with absolutely no issues.Belch
A
0

Make sure to close any browser tabs that have the app running in it, you can't replace a service worker while it's servicing an existing version of the application. Also try reloading the page once the service worker is installed if there are any caching issues.

Ault answered 17/1, 2021 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.