react-native: [0.59.x] URLSearchParams 'Error: not implemented'
š Bug Report
I used next code to check support URLSearchParams
if ( !global.URLSearchParams ) {
global.URLSearchParams = require('url-search-params')
}
But now it doesnāt work because you add own āimplementationā https://github.com/facebook/react-native/blob/e6057095adfdc77ccbbff1c97b1e86b06dae340b/Libraries/Blob/URL.js#L69-L71
And a canāt overwrite it
To Reproduce
const a = new URLSearchParams()
a.set('1','2');
a.toString()
Expected Behavior
My code donāt broken!
Remove these polyfills: https://github.com/facebook/react-native/blob/e6057095adfdc77ccbbff1c97b1e86b06dae340b/Libraries/Core/setUpXHR.js#L31-L32
Code Example
// I try force set custom
global.URLSearchParams = require('url-search-params');
// But it works only with
import { polyfillGlobal } from 'react-native/Libraries/Utilities/PolyfillFunctions';
polyfillGlobal('URLSearchParams', () => require('url-search-params'));
Environment
React Native Environment Info:
System:
OS: Linux 4.15 Linux Mint 19.1 (Tessa)
CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Memory: 1.27 GB / 15.51 GB
Shell: 2.7.1 - /usr/bin/fish
Binaries:
Node: 10.15.3 - /usr/bin/node
Yarn: 1.13.0 - /usr/bin/yarn
npm: 6.4.1 - /usr/bin/npm
npmPackages:
react: ^16.8.1 => 16.8.4
react-native: ^0.59.0 => 0.59.0
npmGlobalPackages:
react-native-cli: 2.0.1
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 22
- Comments: 24 (5 by maintainers)
@matthargett the non-standard-compliant polyfill will cause issues because many 3rd-party libraries uses the global
URL
. I donāt think itās reasonable to expect every app developer to throughly test whether this customisedURL
implementation is compatible with all the libraries. (That is why we have web standards in the first place.)As you can see from the comments above, the new version breaks peopleās apps with an obscure error even if it was working before. It means a breaking change was introduced without any documentation or workaround.
If this
URL
implementation is intended to be used by app developers, it should be standard-compliant. I thinkURL
andURLSearchParams
are included in JavaScriptCore which is already provided by React Native 0.59. If that is the case, I donāt understand why we still need this customisedURL
implementation.If this
URL
implementation is only intended to be used internally byBlob
, it should not be exported to theglobal
scope. Or it should be renamed to something likeRNURL
to avoid confusing. App developers can actively decide if they want to use this customised version ofURL
.In any case, its behaviour should be documented in some way. And a clear workaround should be provided with the error message.
Hey! I used an other npm package and had fixed it. search-params
Why is this closed? This is still an issue in 0.61.5
Hi @leethree , thanks for your comments.
Because URL needs to integrate with React Nativeās blob support to enable an opaque reference that avoids huge copies, the URL is customized. Many React Native applications use the included URL and URLSearchParams without issues, even via complex libraries such as apollo-server. Iām open to suggestions (and especially PRs) that are able to meet the needs.
In the meantime, Iām happy to submit a PR myself to fix specific use cases that happen via libraries (such as apollo-server, etc). If you can tell me the use case, I can create a PR with tests pretty quickly.
@matthargett I think the polyfill should be removed from
global
because itās not standard compliant and itās not documented properly in React Native JavaScript Environment.Even the most basic use case will result in broken URL because of this polyfill. I filed a new issue #24428 for it.
I would argue that a broken or incomplete polyfill is worse than no polyfill at all. Exposing it in
global
will let people assume itās safe to use it.Iām also having this issue.
Thanks @A-Tokyo, thatās what I went with in the end, it works like a charm. But still some recommended solution from the maintainers would be better instead of a workaround.
The only way I can get this to work, for example with aws-amplifyās oauth, is to not only polyfill URLSearchParams as mentioned above, but to also delete the global reference:
delete global.URLSearchParams;
This causes other issues, so Iād love to hear if anyone has found a more reliable solution.
Hi! Yes, we purposefully didnāt bring in the full whatwg-url package because it has a major impact on the bundle size due to needing to support many unicode corner cases. I expanded our narrow implementation slightly to support apollo-serverās use of URL. Is there a specific library that isnāt interacting properly?
Once you narrow the use case, I can suggest what can be done. If you need the fully spec-compliant URL implementation for reasons you canāt specify publicly, then the workaround in the comment above will work (at the cost of the bundle size impact).
This seems like an excellent candidate to add to the React Native documentation.
@halileohalilei Check my comment above, it should solve your issue š
Iām facing this issue at 0.61.5 as well
+1 @florian-milky Iām facing this issue at 0.61.5 as well
This is whatās working for me.