-
Notifications
You must be signed in to change notification settings - Fork 200
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
TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation #46
Comments
Ah interesting, the |
You mean webpack setup? |
I have the same issue using Webpack 3 and Babel (babel-preset-env). Original code: import fetch from 'unfetch';
const test = fetch('http://localhost:8080'); Compiled code: /* harmony import */ var __WEBPACK_IMPORTED_MODULE_2_unfetch__ = __webpack_require__(420);
const test = __WEBPACK_IMPORTED_MODULE_2_unfetch__["a" /* default */]('http://localhost:8080'); Note how Fails on Chrome with
and on Firefox with
Interestingly, Safari doesn't care. The following workaround works well. import unfetch from 'unfetch';
// Fix a bug making unfetch not being properly bound with webpack.
const fetch = unfetch;
const test = fetch('http://localhost:8080'); A simple fix for export default typeof fetch=='function' ? fetch : function(url, options) {
// [...]
} with something like export default typeof fetch=='function' ? fetch.bind() : function(url, options) {
// [...]
} If it is of any use, here is my webpack config. const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const resolve = relPath => path.resolve(__dirname, relPath);
module.exports = {
entry: {
experiment: ['babel-polyfill', './experiment'],
'results-viewer': ['babel-polyfill', './results-viewer']
},
output: {
path: resolve('build'),
publicPath: '/',
filename: './[name].js'
},
context: resolve('src'),
resolve: {
extensions: ['.js', '.json', '.jsx', '.css'],
alias: {
'state-machine': 'javascript-state-machine'
}
},
module: {
rules: [{
test: /\.pug/,
include: resolve('src'),
use: 'pug-loader'
}, {
test: /\.s?css$/,
include: resolve('src'),
use: ExtractTextPlugin.extract({
use: [{
loader: 'css-loader',
options: { sourceMap: true }
}, {
loader: 'postcss-loader',
options: { sourceMap: true }
}, {
loader: 'sass-loader',
options: { sourceMap: true }
}]
})
}, {
test: /\.css$/,
exclude: resolve('src'),
use: ExtractTextPlugin.extract({
use: [{
loader: 'css-loader',
options: { sourceMap: true }
}]
})
}, {
test: /\.m?js$/,
include: resolve('src'),
use: 'babel-loader'
}]
},
plugins: [
new ExtractTextPlugin({ filename: '[name].css', disable: false, allChunks: true }),
]
}; and my .babelrc {
"presets": [["env", {
"targets": { "browsers": "last 1 chrome version, last 1 safari version, last 1 firefox version" },
"modules": false,
"loose": true
}]],
"plugins": ["transform-runtime"]
} |
Fix fetch errors.
Seems like this is still a thing. I'm using:
On server side it's working as expected, but when I try something like making a call inside a simple |
Running into the same issue with Parcel v2.0beta.3 :-/ |
Fetch doesn't work in browsers when it's not bound to the window — for example, `fetch.call({}, "...")` raises `TypeError: Can only call Window.fetch on instances of Window` in Safari and `TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation` in Chrome. Webpack currently generates something that looks like: ```ts const just_fetch_1 = tslib_1.__importDefault(require("@foxglove/just-fetch")); ... await just_fetch_1.default(...) ``` And this means the fetch function executes with `this` being just_fetch_1 rather than window. I'm not sure how Webpack normally handles issues like this (see also developit/unfetch#46) but we can work around it with an explicit bind.
The script crashed with error
TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
After webkack it is looks:
The text was updated successfully, but these errors were encountered: