Skip to content

fix(worker): : Blob URL revoke #19893

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

Closed
wants to merge 8 commits into from
38 changes: 22 additions & 16 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,32 +347,38 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
const blob = typeof self !== "undefined" && self.Blob && new Blob([${
workerType === 'classic'
? ''
: // `URL` is always available, in `Worker[type="module"]`
`'URL.revokeObjectURL(import.meta.url);',`
: // For module workers, append revokeObjectURL to the script
`'self.URL.revokeObjectURL(import.meta.url);',`
}jsContent], { type: "text/javascript;charset=utf-8" });
export default function WorkerWrapper(options) {
let objURL;
try {
objURL = blob && (self.URL || self.webkitURL).createObjectURL(blob);
if (!objURL) throw ''
const worker = new ${workerConstructor}(objURL, ${workerTypeOption});
if (!objURL) throw "";
const worker = new Worker(objURL, {
name: options == null ? void 0 : options.name
});
worker.addEventListener("load", () => {
(self.URL || self.webkitURL).revokeObjectURL(objURL);
});
worker.addEventListener("error", () => {
(self.URL || self.webkitURL).revokeObjectURL(objURL);
});
worker.addEventListener("close", () => {
(self.URL || self.webkitURL).revokeObjectURL(objURL);
});

return worker;
} catch(e) {
return new ${workerConstructor}(
'data:text/javascript;charset=utf-8,' + encodeURIComponent(jsContent),
${workerTypeOption}
} catch (e) {
if (objURL) {
(self.URL || self.webkitURL).revokeObjectURL(objURL);
}
return new Worker(
"data:text/javascript;base64," + encodedJs,
{
name: options == null ? void 0 : options.name
}
);
}${
// For module workers, we should not revoke the URL until the worker runs,
// otherwise the worker fails to run
workerType === 'classic'
? ` finally {
objURL && (self.URL || self.webkitURL).revokeObjectURL(objURL);
}`
: ''
}
}`
: `${jsContent}
Expand Down
Loading