stackblitz / webcontainer-core

Dev environments. In your web app.
https://webcontainers.io
MIT License
3.94k stars 174 forks source link

Support for service-workers #846

Open manoldonev opened 2 years ago

manoldonev commented 2 years ago

Describe the bug

Mock Service Worker fails to initialize on startup (works on local VSCode):

Uncaught (in promise) Error: [MSW] Failed to register a Service Worker for scope ('https://visualization-assignment--5173.local.webcontainer.io/') with script ('https://visualization-assignment--5173.local.webcontainer.io/visualization-assignment/mockServiceWorker.js'): Service Worker script does not exist at the given path.

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

Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/init
    at getWorkerInstance (msw.js?v=750958eb:21157:17)
    at async startWorkerInstance (msw.js?v=750958eb:22192:28)
    at async initializeMockServiceWorker (index.tsx:17:5)
    at async main (index.tsx:33:3)

Running the command npx msw init <PUBLIC_DIR> does not help. Generally the script location is resolved thru a call like ${import.meta.env.BASE_URL}mockServiceWorker.js so the problem might be there as well.

Link to the blitz that caused the error

https://stackblitz.com/~/github.com/manoldonev/visualization-assignment

Steps to reproduce

  1. Execute pnpm start.
  2. Error is logged in console -- screen remains blank

Expected behavior

App should load.

Parity with Local

Screenshots

No response

Platform

Version = 1.67.0
Hash = c994f4b06d30d4ac09341f7888800531bc4a8e9c
WebContainer = 3e4f172269cb19a250be0dfdff1ab657cb850d0e

Browser name  = Chrome
Full version  = 106.0.0.0
Major version = 106
navigator.appName = Netscape
navigator.userAgent = Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
performance.memory = {
  "totalJSHeapSize": 168194126,
  "usedJSHeapSize": 157514542,
  "jsHeapSizeLimit": 4294705152
}

Additional context

No response

jrvidal commented 2 years ago

:wave: @manoldonev Unfortunately, WebContainer does not support registering a SW on the served page. It's true that the error is a bit confusing.

samdenty commented 2 years ago

This is tracked, and it's something we do want to support in the future - but it might be difficult

markerikson commented 4 months ago

I would realllly like to see this. I'm revamping the Redux tutorials and was considering switching our embeds to StackBlitz, but they rely on MSW to work and I just ran into this issue.