Skip to content

Commit

Permalink
fix: check for Blob before creating worker URL, close #4462 (#4674)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasg123 committed Sep 6, 2021
1 parent fdc3212 commit 311026f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
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 @@ -75,15 +75,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

0 comments on commit 311026f

Please sign in to comment.