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.

Service Worker script does not exist at the given path

See original GitHub issue

Hello, we have a storybook running in a subdirectories (for PRs and also one stable version). Msw was not working out of the box for this setup, but then I used #75 setup.

  serviceWorker: {
    url: `blabla/blabla-storybook/${STORYBOOK_SHA ?? "latest"}/mockServiceWorker.js`,
  }

Issues is, we are still getting an error that file does not exist in the path. But when I click on the link in the error message, I am taken to mockServiceWorker.js, so it obliviously exist in the location.

Uncaught (in promise) Error: [MSW] Failed to register a Service Worker for scope ('https://cdn.blabla.no/') with script ('https://cdn.blabla.no/blabla/blablabla-storybook/${commitSHA}/mockServiceWorker.js'): Service Worker script does not exist at the given path.

Did you forget to run "npx msw init <PUBLIC_DIR>"

We are using React with CRA and latest version of storybook.

Is anyone able to help with this, please? I have no clue at this point what might be the issue.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
WesleyKapowcommented, Nov 15, 2022

Just ran into this as well.

Work around is to tell MSW where to look for the file. One gotcha is you’ll likely want to have it be different for github pages vs running locally.

// preview.js

let options = {};
if (location.hostname === "username.github.io") {
  options = {
    serviceWorker: {
      url: "/repo-name/mockServiceWorker.js",
    },
  };
}

initialize(options);

https://msw-sb.vercel.app/?path=/story/guides-getting-started--page#configuring-msw https://mswjs.io/docs/api/setup-worker/start#serviceworker

0reactions
guissimatheuscommented, Oct 28, 2022

So, I kinda have the same problem. I have a SignIn View that mocks the login request at the stories in the Storybook. If I run it local, it works as expected, but it won’t work at my GitHub repository as it can’t find the mockServiceWorker.js file.

My application: https://guissimatheus.github.io/design-system-react My repo: https://github.com/guissimatheus/design-system-react

If you go to the browser network tab, you can see the msw get request url https://guissimatheus.github.io/mockServiceWorker.js with status code 404. I can access the worker file at https://guissimatheus.github.io/design-system-react/mockServiceWorker.js

I dunno how I can config my project to change the url and get the mock to work on GitHub.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Service Worker Registration issue - Stack Overflow
This script inside the page is just unable to locate /ServiceWorker.js and could be due to 1 thing out of 2.
Read more >
Browser - Getting Started - Mock Service Worker Docs
Browser. Setup. Mock Service Worker operates client-side by registering a Service Worker responsible for requests interception.
Read more >
Service Workers - W3C
If objectMap [ serviceWorker ] does not exist, then: Let serviceWorkerObj be a new ServiceWorker in environment 's Realm, and associate it with ......
Read more >
Using Service Workers - Web APIs | MDN
The path to your service worker file is not written correctly — it needs to be written relative to the origin, not your...
Read more >
Service workers - web.dev
Not all browsers support service workers. Even when present your service worker won't be available on first load or while it's waiting to...
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