mirror of
https://github.com/maputnik/editor.git
synced 2026-08-24 22:17:25 +00:00
issue/910: Fix CORS warning for localhost
See maplibre/maputnik#910 Don't show CORS warning for localhost
This commit is contained in:
@@ -10,16 +10,22 @@ function validate(url: string, t: TFunction): JSX.Element | undefined {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let error;
|
let error;
|
||||||
const getProtocol = (url: string) => {
|
const getUrlParams = (url: string) => {
|
||||||
|
let protocol: string | undefined;
|
||||||
|
let isLocal = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const urlObj = new URL(url);
|
const urlObj = new URL(url);
|
||||||
return urlObj.protocol;
|
|
||||||
}
|
protocol = urlObj.protocol;
|
||||||
catch (err) {
|
// Basic check against localhost; 127.0.0.1/8 and IPv6 localhost [::1]
|
||||||
return undefined;
|
isLocal = /^(localhost|\[::1\]|127(.[0-9]{1,3}){3})/i.test(urlObj.hostname);
|
||||||
|
} catch (err) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return { protocol, isLocal };
|
||||||
};
|
};
|
||||||
const protocol = getProtocol(url);
|
const {protocol, isLocal} = getUrlParams(url);
|
||||||
const isSsl = window.location.protocol === "https:";
|
const isSsl = window.location.protocol === "https:";
|
||||||
|
|
||||||
if (!protocol) {
|
if (!protocol) {
|
||||||
@@ -40,7 +46,8 @@ function validate(url: string, t: TFunction): JSX.Element | undefined {
|
|||||||
else if (
|
else if (
|
||||||
protocol &&
|
protocol &&
|
||||||
protocol === "http:" &&
|
protocol === "http:" &&
|
||||||
window.location.protocol === "https:"
|
window.location.protocol === "https:" &&
|
||||||
|
!isLocal
|
||||||
) {
|
) {
|
||||||
error = (
|
error = (
|
||||||
<SmallError>
|
<SmallError>
|
||||||
|
|||||||
Reference in New Issue
Block a user