Issue #485 Update browser.js to not throw with SSR#496
Issue #485 Update browser.js to not throw with SSR#496enkelmedia wants to merge 1 commit intoform-data:masterfrom
Conversation
This updates that browser.js does not throw when rendering on the server and window is not defined. Solves issue form-data#485
|
@coveralls any change that this can be merged? |
|
Can I do anything to help getting this merged? |
|
@3imed-jaberi Great! How can we get this merged? It's really annoying having to fix this manually each time. Can I help in any way? |
|
+1 for this being a great fix to include! |
|
This issue is breaking my webpack build imports. Any chance we can get this merged in. I know it's been awhile but it's needed especially with Axios. |
|
Can you please merge it ? |
|
We're waiting on GitHub access then we can merge and release. |
|
Would love for this to get merged in! |
|
I'm confused. Why would |
|
@ljharb because people build components (e.g. React) or libraries without taking SSR in to consideration and use this lib as a dependency. The PR is a simple fix for scenarios like that. |
|
If a component isn't built for SSR, then the component simply can't be used on the server - but I'm still not clear on why the browser file would ever be invoked on a server. Are you bundling things on the server? (if so, don't do that) |
This comment was marked as duplicate.
This comment was marked as duplicate.
1 similar comment
|
@ljharb its not up to me to choose the downstream dependencies of all packages that I might want to use. In this case the only problem is this package. The PR fixes this problem. Many components would work fine when hydrated on the client. The problem here is that SSR is prevented by this library. I’m trying to help out here, what is your reason not to merge this? What is preventing you? |
|
@enkelmedia i've only recently become a maintainer, so i'm trying to be cautious. Can you help me understand how |
|
This PR does not break anything on your end, I created it years ago and I can’t remember exactly why. Sorry about that. It only checks if you are running in a browser context and if not - backing off. I don’t see any problem from your perspective with accepting this - or could you explain why you are hesitant? Note here that we are not living in a “optimal” world, someone with a great component might have a dependency on this package without understanding that it would impact SSR - the real issue is of course that they are using this package from the beginning - but a good solution would be for this package to not try to do anything in environments where it does not make sense. This is what this PR is fixing. |
|
I'm hesitant because it's not actually beneficial to paper over problems caused elsewhere. Someone using this package should never be directly referencing the browser file - it only should be used via Line 11 in e8f0d80 In other words, this change doesn't make sense because the problem should never happen, and if it's happening, it should be fixed at the place that's doing the wrong thing - which isn't here. |
|
You are right and I agree. In theory. Let’s hope people stop using this package and avoid the problem. |
|
@ljharb this change is still required for any project that uses Vite. Because Vite prefers the So, during SSR, the For now, I've been using this workaround: import FormData from 'form-data/lib/form_data';
const resolveFormData = typeof window === 'object' ? window.FormData : FormData;
const form = new resolveFormData(); |
|
@vaibhavrajsingh2001 wouldn't you only use vite for your browser bundle? If it’s intended to use it for node, then that preference is incorrect. |
Vite is also used popularly for server side rendering. Can we explore using the more modern conditional So instead of we could have This would be a non-breaking change since the same items are being exported. |
|
Unfortunately adding exports is basically always a breaking change; but either way, if vite is intended for server use, it shouldn’t be using the browser field at all in that scenario. I’d suggest filing a bug. |
|
@ljharb could you explain how exports is basically always a breaking change? "exports": {
"node": {
"import": "./lib/form_data.js"
}
}Would that be breaking? |
|
@dargmuesli yes. because any path not listed in exports is not reachable. ESM can import CJS, so there's no need for that whatsoever. If you're using a tool that can't handle that, it's broken. |
This updates that browser.js does not throw when rendering on the server and window is not defined.
Solves issue #485