Skip to content

fix(worker): resolve WebKit compat with inline workers by deferring blob URL revocation #20460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 25, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 22, 2025

Problem

Inline workers generated by Vite were incompatible with Playwright's WebKit (and potentially other WebKit-based browsers) due to premature Blob URL revocation. The issue occurred because Vite's WorkerWrapper code immediately revoked the Blob URL in a finally block, invalidating it before WebKit could properly initialize the worker.

// Problematic code - URL revoked immediately
export default function WorkerWrapper(options) {
  let objURL;
  try {
    objURL = blob && (self.URL || self.webkitURL).createObjectURL(blob);
    const worker = new Worker(objURL, options);
    return worker;
  } catch(e) {
    // fallback...
  } finally {
    objURL && (self.URL || self.webkitURL).revokeObjectURL(objURL); // ❌ Too early!
  }
}

This caused worker initialization failures in WebKit environments, particularly affecting testing with Playwright.

Solution

Following @sapphi-red's suggestion in #19238, this fix moves the URL revocation logic inside the worker script for classic workers (similar to how module workers already handled it), allowing the Blob URL to remain valid throughout the worker's lifecycle.

Changes made:

  1. Added URL revocation to classic worker blob content: Classic workers now include (self.URL || self.webkitURL).revokeObjectURL(self.location.href); at the start of their script
  2. Removed the problematic finally block: No more immediate URL revocation that was breaking WebKit
  3. Maintained existing error handling: Worker error listeners still clean up URLs on failure

Before (classic workers):

new Blob(['', jsContent], { type: "text/javascript;charset=utf-8" })
// + finally block that immediately revokes URL ❌

After (classic workers):

new Blob([
  '(self.URL || self.webkitURL).revokeObjectURL(self.location.href);',
  jsContent
], { type: "text/javascript;charset=utf-8" })
// No immediate revocation ✅

Module workers were already handled correctly and remain unchanged.

Testing

  • ✅ Existing worker tests continue to pass
  • ✅ Generated code verified to include proper URL revocation in worker content
  • ✅ Build process works correctly with the changes
  • ✅ Linting passes

Related

Closes #19238
Closes #19565

Co-authored-by: sapphi-red 49056869+sapphi-red@users.noreply.github.com

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • vite.dev
    • Triggering command: /home/REDACTED/.cache/playwright-bin/chromium_headless_shell-1181/chrome-linux/headless_shell --disable-field-trial-config --disable-REDACTED-networking --disable-REDACTED-timer-throttling --disable-REDACTEDing-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-REDACTED-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=AcceptCHFrame,AutoExpandDetailsElement,AvoidUnnecessaryBeforeUnloadCheckSync,CertificateTransparencyComponentUpdater,DestroyProfileOnBrowserClose,DialMediaRouteProvider,ExtensionManifestV2Disabled,GlobalMediaControls,HttpsUpgrades,ImprovedCookieControls,LazyFrameLoading,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-REDACTEDing --force-color-profile=srgb --metrics-recording-only --no-first-run --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --disable-search-engine-choice-screen --unsafely-disable-devtools-self-xss-warnings --edge-skip-compat-layer-relaunch --enable-automation --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --no-sandbox --disable-setuid-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-YUN3Kl --remote-debugging-pipe --no-startup-window (dns block)
  • workers.cloudflare.com
    • Triggering command: node (vitest 3) (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…rker content

Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Fix this issue based on the suggestion in @vitejs/vite/issues/19238 . the related code is @vitejs/vite/files/packages/vite/src/node/plugins/worker.ts . fix(worker): resolve WebKit compatibility issue with inline workers by deferring Blob URL revocation Jul 22, 2025
@Copilot Copilot AI requested a review from sapphi-red July 22, 2025 13:42
Copilot finished work on behalf of sapphi-red July 22, 2025 13:42
…atibility

Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
@Copilot Copilot AI requested a review from sapphi-red July 22, 2025 14:04
Copilot finished work on behalf of sapphi-red July 22, 2025 14:04
…ibility

Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
@Copilot Copilot AI requested a review from sapphi-red July 22, 2025 14:13
Copilot finished work on behalf of sapphi-red July 22, 2025 14:13
@sapphi-red sapphi-red changed the title fix(worker): resolve WebKit compatibility issue with inline workers by deferring Blob URL revocation fix(worker): resolve WebKit compat with inline workers by deferring blob URL revocation Jul 22, 2025
@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) feat: web workers labels Jul 22, 2025
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

I confirmed that #19238 works with this change.

@sapphi-red sapphi-red marked this pull request as ready for review July 23, 2025 05:05
@patak-dev patak-dev merged commit 8033e5b into main Jul 25, 2025
29 of 30 checks passed
@patak-dev patak-dev deleted the copilot/fix-b5bf4aa1-af84-45b4-93a5-52522fe1cad4 branch July 25, 2025 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inline worker code incompatibility with Playwright’s WebKit
3 participants