Skip to content

Commit 3bcd0ae

Browse files
Added a polyfill for the URL class on IE
1 parent 8947eba commit 3bcd0ae

File tree

2 files changed

+71
-61
lines changed

2 files changed

+71
-61
lines changed

package-lock.json

Lines changed: 52 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/isomorphic.browser.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ import { Browsers, BrowsersRecord, Global, Host, OSInfo, OSInfoRecord } from "./
33
import { merge, mergeGlobalHost } from "./merge";
44
import { toJSON } from "./to-json";
55

6-
let url = new URL(window.location.href);
7-
let cwdURL = new URL(".", url);
6+
// Determine whether this browser supports creating URL objects
7+
// eslint-disable-next-line @typescript-eslint/brace-style
8+
const canCreateURL = (() => { try { return Boolean(new URL("http://example.com")); } catch (e) { return false; } })();
9+
10+
const url = createURL(window.location.href);
11+
const cwdURL = createURL(".", url);
812

913
/**
1014
* Information about the host environment that the code is running in.
@@ -27,6 +31,19 @@ export const host: Host = {
2731
// Merge the global `host` object (if it exists)
2832
mergeGlobalHost(host, host.global.host);
2933

34+
/**
35+
* Creates a URL object from a URL string
36+
*/
37+
function createURL(url: string, base?: URL): URL {
38+
if (canCreateURL) {
39+
return new URL(url, base);
40+
}
41+
else {
42+
// Poor-man's polyfill for URL on Internet Explorer
43+
return { href: base ? base.href : url } as URL;
44+
}
45+
}
46+
3047
/**
3148
* Returns information about the current Browser host.
3249
*/

0 commit comments

Comments
 (0)