Skip to content

Commit

Permalink
Add known issues, and release v2.0.2 #18
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrybendy committed Jan 9, 2018
1 parent 2580057 commit 57d8b58
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ For browser, copy the `index.js` file to your project, and add a `script` tag in
Use `URLSearchParams` directly. You can `new` an object from a string or an object.

```javascript
// new a empty object
// new an empty object
var search1 = new URLSearchParams ();

// from a string
Expand Down Expand Up @@ -138,6 +138,33 @@ for (var item of search) {
}
```

## Known Issues

#### Use with fetch ([#18](https://github.com/jerrybendy/url-search-params-polyfill/issues/18))
Via [fetch spec](https://fetch.spec.whatwg.org/#body-mixin), when passing an `URLSearchParams` object as a request body, the request should add a header with `Content-Type: application/x-www-form-urlencoded; charset=UTF-8`. But, browsers which have `fetch` support but no `URLSearchParams` have no this behavior.

Via the data of [caniuse](https://caniuse.com/#search=fetch), there are many browsers support `fetch` but `URLSearchParams`. They are:

| Edge | Chrome | Opera | Sumsung Internet | QQ | Baidu |
| --- | --- | --- | --- | --- | --- |
| 14 - 16 | 40 - 48 | 27 - 35 | 4 | 1.2 | 7.12 |

If you want to be compatible with these browsers, you should add a `Content-Type` header manually, like below (just an example):

```js
function myFetch(url, {headers = {}, body}) {
headers = headers instanceof Headers ? headers : new Headers(headers);

if (body instanceof URLSearchParams) {
headers.set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
}

fetch(url, {
headers,
body
});
}
```

## LICENSE

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
/*
* Apply polifill to global object and append other prototype into it
*/
self.URLSearchParams = (nativeURLSearchParams && !isSupportObjectConstructor) ?
self.URLSearchParams = (nativeURLSearchParams && !isSupportObjectConstructor && self.Proxy) ?
// Safari 10.0 doesn't support Proxy, so it won't extend URLSearchParams on safari 10.0
new Proxy(nativeURLSearchParams, {
construct: function(target, args) {
return new target((new URLSearchParamsPolyfill(args[0]).toString()));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "url-search-params-polyfill",
"version": "2.0.1",
"version": "2.0.2",
"description": "a simple polyfill for javascript URLSearchParams",
"homepage": "https://github.com/jerrybendy/url-search-params-polyfill",
"main": "index.js",
Expand Down

0 comments on commit 57d8b58

Please sign in to comment.