Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable CORS error for localhost #910

Open
louwers opened this issue Jul 12, 2024 · 3 comments
Open

Disable CORS error for localhost #910

louwers opened this issue Jul 12, 2024 · 3 comments

Comments

@louwers
Copy link
Collaborator

louwers commented Jul 12, 2024

When trying to add a source with http, you always see this message:

Error: CORS policy won't allow fetching resources served over http from https, use a https:// domain

However this is even the case for http://localhost, even though CORS does not apply to localhost (I never had any problems at least). This error should not be shown when the host is localhost.

image
@oobayly
Copy link

oobayly commented Sep 20, 2024

I had assumed that this was a blocking error, and not just a simple message, so I never bothered clicking the "Load from URL" button and ended up running my own docker image 🤦
It was only when I looked at fixing this that I though to try to load a http: URL.

A simple fix would be to test the hostname of the url too:

const getUrlParams = (url: string) => {
    let protocol: string | undefined;
    let isLocal = false;

    try {
        const urlObj = new URL(url);
        
        protocol = urlObj.protocol;
        isLocal = /^(localhost|\[::1\]|127(.[0-9]{1,3}){3})/i.test(urlObj.hostname);
    } catch (err) {
    }

    return {protocol, isLocal};
}

const {protocol, isLocal} = getUrlParams(url);

...

  else if (
    protocol &&
    protocol === "http:" &&
    window.location.protocol === "https:" &&
    !isLocal
  ) {

@HarelM
Copy link
Collaborator

HarelM commented Sep 20, 2024

Feel free to submit a PR, I'll be happy to review it.

oobayly added a commit to oobayly/maputnik that referenced this issue Sep 23, 2024
See maplibre#910

Don't show CORS warning for localhost
@roblabs
Copy link
Contributor

roblabs commented Nov 7, 2024

The Vite server supports configuration of CORS. That is when you start development with npm run start, one can get past CORS issues on localhost. I am not sure if it catches @louwers original case of: a) run Maputnik on localhost:8888, and b) serve a styles from localhost:3001. I just haven't tried that case.

See:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants