Skip to content
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

fix: check for Blob before creating worker URL (close #4462) #4674

Merged
merged 2 commits into from Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/playground/worker/__tests__/worker.spec.ts
Expand Up @@ -65,5 +65,6 @@ if (isBuild) {
expect(content).toMatch(`new SharedWorker("/assets`)
// inlined
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
})
}
11 changes: 5 additions & 6 deletions packages/vite/src/node/plugins/worker.ts
Expand Up @@ -71,15 +71,14 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
const content = Buffer.from(code)
if (query.inline != null) {
// inline as blob data url
return `const blob = new Blob([atob(\"${content.toString(
'base64'
)}\")], { type: 'text/javascript;charset=utf-8' });
return `const encodedJs = "${content.toString('base64')}";
const blob = typeof window !== "undefined" && window.Blob && new Blob([atob(encodedJs)], { type: "text/javascript;charset=utf-8" });
export default function WorkerWrapper() {
const objURL = (window.URL || window.webkitURL).createObjectURL(blob);
const objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
try {
return new Worker(objURL);
return objURL ? new Worker(objURL) : new Worker("data:application/javascript;base64," + encodedJs, {type: "module"});
} finally {
(window.URL || window.webkitURL).revokeObjectURL(objURL);
objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL);
}
}`
} else {
Expand Down