Skip to content

Resolve webview id for worker clients in service worker#315495

Open
yongsooim wants to merge 3 commits into
microsoft:mainfrom
yongsooim:fix/webview-sw-worker-resource-routing
Open

Resolve webview id for worker clients in service worker#315495
yongsooim wants to merge 3 commits into
microsoft:mainfrom
yongsooim:fix/webview-sw-worker-resource-routing

Conversation

@yongsooim
Copy link
Copy Markdown

Fixes #315494. This appears to be a regression from #311844.

processResourceRequest and processLocalhostRequest allow
worker / sharedworker clients past the guard, but neither function
routes them: the dispatch block runs only when webviewId is non-null,
and a worker client's client.url (typically
blob:https://<webview-origin>/<uuid>) has no id searchParam. The
request store entry is never resolved and the fetch hangs until
requestTimeout() fires; in the renderer this surfaces as
TypeError: Failed to fetch dynamically imported module.

This patch resolves the owning iframe for a worker client by matching
the worker URL's origin against window clients whose pathname is one of
the webview entry points, then reading id from that iframe's
searchParams. Because some webviews can intentionally share an origin,
the match is only accepted when that origin maps to a single webview id.
The relaxed worker / sharedworker exception in the guard is replaced:
a worker client whose owning iframe cannot be resolved unambiguously is
treated the same as a window client without a webview id (notFound()
for resource requests, fetch(event.request) for localhost), preserving
pre-#311844 behavior for that edge.

Validation

  • node --check src/vs/workbench/contrib/webview/browser/pre/service-worker.js
  • git diff --check
  • Local service-worker VM mock:
    • upstream worker resource request sends no load-resource message
    • patched worker resource request sends load-resource
    • patched ambiguous shared-origin case fails closed
    • patched worker localhost request sends load-localhost

I did not run the full VS Code browser test suite from this sparse checkout.

Copilot AI review requested due to automatic review settings May 9, 2026 20:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression in the webview service worker where worker/sharedworker clients could pass the guard but fail routing due to lacking an id search param, causing fetches (notably dynamic imports) to hang until timeout.

Changes:

  • Resolve a webview id for worker/sharedworker clients by mapping the worker URL origin to a single owning webview iframe client.
  • Update processResourceRequest and processLocalhostRequest to use this worker resolution and fail closed when the owner cannot be resolved.
  • Add getWebviewIdForWorkerClient helper to perform the origin-to-iframe-id resolution with ambiguity detection.

Comment thread src/vs/workbench/contrib/webview/browser/pre/service-worker.js Outdated
@yongsooim
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@yongsooim yongsooim changed the title [webview] Resolve webview id for worker clients in service worker Resolve webview id for worker clients in service worker May 9, 2026
@harshagarwalnyu
Copy link
Copy Markdown

I have refactored the service worker to address the code duplication concern raised by the AI reviewer. By extracting the path-checking logic into a helper function isWebviewIframe(url), we keep the logic DRY and reduce the risk of drift if entry points change. @yongsooim, feel free to pull these changes if you find them helpful!

@harshagarwalnyu
Copy link
Copy Markdown

I have opened a companion draft PR (#315498) that extracts the isWebviewIframe helper into a standalone commit, addressing the code duplication concern from the Copilot review. @yongsooim feel free to pull that commit directly — happy to close #315498 if you prefer to squash it into your branch instead.

@yongsooim
Copy link
Copy Markdown
Author

Thanks @harshagarwalnyu for taking a look and for opening the companion PR. I folded a corrected version of that helper extraction directly into this PR in 871021f, keeping the existing getOuterIframeClient JSDoc attached to the function.

@yongsooim yongsooim marked this pull request as ready for review May 14, 2026 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Webview workers can no longer load webview resources after #311844

4 participants