Skip to content

Commit

Permalink
Fix assembly of URL parameters for IE11
Browse files Browse the repository at this point in the history
Using a core-js version 3.x polyfill for URLSearchParams is currently
not possible due to bug emberjs/ember-cli-babel#395
  • Loading branch information
maxhq committed Mar 25, 2021
1 parent 7929970 commit 7e83883
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
36 changes: 27 additions & 9 deletions core/htdocs_source/app/pods/openxpki/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,17 @@ export default class OpenXpkiRoute extends Route {
},
};

// converts plain (not nested!) key => value hash into URL parameter string
let getUrlParamStr = (data) => {
let params = new URLSearchParams();
Object.keys(data).forEach(k => params.set(k, data[k]));
return params.toString();
};

// POST
if (request.action) {
fetchParams.method = "POST";
fetchParams.body = getUrlParamStr({
fetchParams.body = this._toUrlParams({
...data,
_rtoken: this.content.rtoken,
});
}
// GET
else {
url += '?' + getUrlParamStr(data);
url += '?' + this._toUrlParams(data);
}

return fetch(url, fetchParams)
Expand Down Expand Up @@ -250,6 +243,31 @@ export default class OpenXpkiRoute extends Route {
})
}

/*
* Convert plain (not nested!) key => value hash into URL parameter string.
* Source: https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url-search-params.js
*
* TODO: Replace with...
*
* _toUrlParams(data) {
* let params = new URLSearchParams();
* Object.keys(data).forEach(k => params.set(k, data[k]));
* return params.toString();
* }
*
* ...once https://github.com/babel/ember-cli-babel/issues/395 is fixed and we can
* set up @babel/preset-env to use core-js version 3.x
*/
_toUrlParams(entries) {
let result = [];
let URLPARAM_FIND = /[!'()~]|%20/g;
let URLPARAM_REPLACE = { '!': '%21', "'": '%27', '(': '%28', ')': '%29', '~': '%7E', '%20': '+' };
let serialize = it => encodeURIComponent(it).replace(URLPARAM_FIND, match => URLPARAM_REPLACE[match]);

Object.keys(entries).forEach(k => result.push(serialize(k) + '=' + serialize(entries[k])));
return result.join('&');
}

_resolveTarget(requestTarget) {
let target = requestTarget || 'self';
// Pseudo-target "self" is transformed so new content will be shown in the
Expand Down
12 changes: 11 additions & 1 deletion core/htdocs_source/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,18 @@ module.exports = function(defaults) {
includePolyfill: true,
},

// options for @babel/preset-env (evaluated by 'ember-cli-babel' and passed on)
// 'babel': {
// useBuiltIns: 'usage', // auto import polyfills without the need to specify them
// // options for core-js, see https://babeljs.io/docs/en/babel-preset-env#corejs
// corejs: {
// version: '3.9.1',
// },
// },

// fetch() polyfill does not exist in core-js (via ember-cli-babel), so we need to add it:
'ember-fetch': {
preferNative: true
preferNative: true,
},
});

Expand Down
36 changes: 18 additions & 18 deletions core/server/htdocs/assets/openxpki.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7e83883

Please sign in to comment.