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
Using multipart/form-data with multer occasionally causes React app to crash and refresh #566
Comments
Are you referring to your frontend here, that runs in the browser? |
Yes - I have 2 fetch requests which cause this behavior - both are The fetch requests will occasionally work fine. At times, however, something happens when the fetch promise chain is supposed to receive a response from the server. The entire page goes blank, the app resets. (Console reads The strangest thing is that the fetch requests occasionally happen with no problem at all. However, when it fails, the fetch request will seem to be lost with no status code - and yet API actions and functions will successfully happen - Image will be uploaded, data entered, but then the app is flushed of all state and rebooted. There is no error on the back-end, since my API is successfully updated with the info and image - and this does not happen with other |
Could it be that the hot-reloading/file-watching part is detecting the uploaded file as a change, and then restarts/refreshes the app? |
Hello. I've been struggling with this problem also and asked a question on SA before checking here: Now, after what @LinusU asked I checked it in a production environment without my CRA setup, it works and no refreshing as far as I see. So, @Mikhail-MM issued a report on CRA side also: I commented there too. I'm relived this is not my code's fault :) |
@devserkan I am glad to see I'm not the only one with this issue (Well, not GLAD, but you know what I mean :P ) Let me know how your progress goes in finding a solution. I am reluctant to put the blame on CRA, but I am lost in determining the cause. Something happens when the specifically |
@Mikhail-MM, you are not alone for sure :) But, I'm so surprised we are the only ones hit that problem or others don't care? Unfortunately, I'm not so capable to find the reason what causes this problem within CRA since I'm a learner, but what @LinusU said seems very logical to me. After uploading the file hot-reload part thinks there is change on the source so it refreshes, I think? |
Hello again @Mikhail-MM. Now it has started to block my development today :) First I used disabling hot reloading (actually blocking the request) temporarily while I was working that part of my code. I used this solution for that: facebook/create-react-app#2519 (comment) But that bothered me too much. After that I change my code logic. I was uploading files to client's public directory in development environment and some other place in production. I've merged the directories, say in /public/images directory, serve /public statically with Express and use a REACT_APP_SOME_VAR environment variable to use express API URL in my client's code to show the file URLs. |
@devserkan Hey, I had forgotten this issue out of frustration but saw your comment a while back and had an a-ha moment ... I think you are correct, this is intended behavior of the hot-reloader when it detects that a new file has been entered into the project. I've noticed the same refreshing when I have added photos manually to an open React project that I am working on. I think the issue is the refresh occurring in the middle of a fetch() call which never really resolves properly, giving this error, and producing an inconsistency on whether the backend server successfully pushes that file through and does all necessary logics. Somewhat shamefully I haven't actually tried to disable hot-reloader or even run the app in prod mode, but I believe that this issue may be solved when hot reloader is not in play as you mentioned. So good thinking :D |
Hello @Mikhail-MM I'm glad someone approved other than me this is because of hot reloading. Not a bug maybe but it bothers people who hit this problem :) |
Hi, I'm having a very strange bug in my code that I can not understand.
Something simply goes wrong when I use a multipart upload fetch request. It is not a problem when uploading JSON data. When I am using multer in the middle of my action chain, my entire app occasionally crashes and refreshes, resetting user auth token and everything. I think something is wrong with the fetch request itself. It will occasionally return with no status code, and the API simply hang and not proceed to further middleware.
I had trouble using the
cross-fetch
polyfill for formattingmultipart/form-data
requests AT ALL, as it would give me an invalid BodyInit error (This went away as soon as I stopped using the polyfill...)I have tried splitting my requests from a single endpoint which uploads user info and an image at the same time, to a chain of requests which upload JSON (success), proceed to image upload and return file path of new image (occasional success, sometimes resets and refreshes entire app), and update entry with file path JSON (PUT request, again - it never happens if there is a hang / refresh)
I am really out of ideas. The code should be correct, and works when I take out the image uploading. I am thinking of trying out different form-data libs. The fact that it would not send at all until I got rid of a fetch polyfill is extremely confusing.
Here is a gist of my code:
https://gist.github.com/Mikhail-MM/5c843ef8c77cf6d534c09f536ab0c06a
The text was updated successfully, but these errors were encountered: