elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.79k stars 96 forks source link

Weird NetworkError with Firefox #49

Closed Aarbel closed 5 years ago

Aarbel commented 5 years ago

Hi @elbywan, thanks a lot for your work !

I face a weird error with Firefox (no errors happen in Chrome and Safari), which cause the browser to refresh. Do you know the origin of the problem ? Format of body ?

Thanks a lot for your help !

const addAuthTokenMiddleware = next => (url, opts) => next(url, {
    ...opts,
    headers: {
        ...opts.headers,
        Authorization: `Bearer ${getToken()}`,
    },
});

const defaultWretch = wretch()
    .url('/api')
    .options({ credentials: 'include' })
    .middlewares([addAuthTokenMiddleware]);

const response = defaultWretch
    .url(url)
    .post(body)
    .json()
    .catch(error => {
        console.error('Clovis ServerApiJSON post error :', {
            error: error,
            url: url,
            body: body,
        });
    });

image

Aarbel commented 5 years ago

Ok i just solved the error after hours of research.

Firefox refreshes the page on default html <button> elements and consider them as "submit". That makes the wretch request fail.

Putting type="button" on the