Skip to content

Commit

Permalink
fix: exception to sending formdata in webworker (#5139)
Browse files Browse the repository at this point in the history
Co-authored-by: Jay <jasonsaayman@gmail.com>
  • Loading branch information
0x30 and jasonsaayman committed Dec 1, 2022
1 parent 9041c7d commit e3d7594
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/adapters/xhr.js
Expand Up @@ -61,7 +61,7 @@ export default isXHRAdapterSupported && function (config) {
}
}

if (utils.isFormData(requestData) && platform.isStandardBrowserEnv) {
if (utils.isFormData(requestData) && (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv)) {
requestHeaders.setContentType(false); // Let the browser set it
}

Expand Down
19 changes: 19 additions & 0 deletions lib/platform/browser/index.js
Expand Up @@ -31,6 +31,24 @@ const isStandardBrowserEnv = (() => {
return typeof window !== 'undefined' && typeof document !== 'undefined';
})();

/**
* Determine if we're running in a standard browser webWorker environment
*
* Although the `isStandardBrowserEnv` method indicates that
* `allows axios to run in a web worker`, the WebWorker will still be
* filtered out due to its judgment standard
* `typeof window !== 'undefined' && typeof document !== 'undefined'`.
* This leads to a problem when axios post `FormData` in webWorker
*/
const isStandardBrowserWebWorkerEnv = (() => {
return (
typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope &&
typeof self.importScripts === 'function'
);
})();


export default {
isBrowser: true,
classes: {
Expand All @@ -39,5 +57,6 @@ export default {
Blob
},
isStandardBrowserEnv,
isStandardBrowserWebWorkerEnv,
protocols: ['http', 'https', 'file', 'blob', 'url', 'data']
};

0 comments on commit e3d7594

Please sign in to comment.