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

live server doesn't work in relative file paths #293

Open
forghan89 opened this issue Feb 15, 2019 · 10 comments
Open

live server doesn't work in relative file paths #293

forghan89 opened this issue Feb 15, 2019 · 10 comments

Comments

@forghan89
Copy link

forghan89 commented Feb 15, 2019

vscode-live-server doesn't work in html when add css or js relative file paths such as:

<link rel="stylesheet" href="../bootstrap/bootstrap.min.css">

my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <link rel="stylesheet" href="../bootstrap/bootstrap.min.css">
</head>
<body>
    
    <h1>Heading</h1>


    <script src="../js/jquery-3.3.1.min.js"></script>
    <script src="../js/popper.min.js"></script>
    <script src="../bootstrap/bootstrap.min.js"></script>
</body>
</html>

result:

liveserver

@viditvarshney
Copy link

Just specify the type of file , Sometimes it happens
type="text/css"

if it doesn't work let me know

@viditvarshney
Copy link

@forghan89 It is working absolutely fine
check your css file Live server works with relative path

@forghan89
Copy link
Author

forghan89 commented Feb 24, 2019

Just specify the type of file , Sometimes it happens
type="text/css"

if it doesn't work let me know

I add type="text/css" to my code But did not work:
(This bug might be related to VScode)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <link rel="stylesheet" type="text/css" href="../bootstrap/bootstrap.min.css">
</head>
<body>
    
    <h1>Heading</h1>


    <script src="../js/jquery-3.3.1.min.js"></script>
    <script src="../js/popper.min.js"></script>
    <script src="../bootstrap/bootstrap.min.js"></script>
</body>
</html>

@viditvarshney
Copy link

viditvarshney commented Feb 24, 2019 via email

@Anas-jaf
Copy link

i countered the same problem and the reason is that liveserver does not open js and css files so you need to download liveserver extention then it will work perfect

@aderchox
Copy link

aderchox commented Jul 26, 2021

I encountered the same problem and it doesn't work with the type="text/css" as well.
E.g., https://127.0.0.1:5500/temp.css is served where https://127.0.0.1:5500/../temp.css must be served.
Actually this seems to be "the expected" and "fine" behavior, because (if I'm not wrong) a web server must not serve files outside its web root(the path at which it started serving files).

So for instance, let's say we have such a structure:

/parent
    style1.css
    /project
        index.html
        /assets
            /stylesheets
                style2.css
            /fonts
                roboto.woff

assuming we are in the "/project", it will be our current path, and when we start the live server there, it (our current path, i.e., the "/project") will become our "web root", denoted by a slash ( / ) after the IP address.
Now if we try to add the roboto font in style2.css, it would be possible using a relative path like this: url(../fonts/roboto.woff), and it works because it is still completely INSIDE the web root space.
But a relative path won't work if tries to access a file outside the web root space, for example, if index.html tries to link to the stylesheet style1.css, it will fail, because it is above (outside of) the realm of the served web root(i.e., the / which in example above is the /project directory).
Hope it helps.

@VladdyHell
Copy link

I just recently solved this issue, I found out that whenever you use relative paths in an HTML file or any other files, it uses the very root of your directory of your computer as I saw in the source code of the HTML, I used live-server via CLI, but try this in your VSCode settings if it works:

{
    "liveServer.settings.mount": [
        ["/"],
    ]
}

I don't know the exact settings for this since I don't use VSCode, I just researched the settings for the live-server for VSCode.

As long as the root is mounted, relative paths will work.

@Arnold6666
Copy link

I just recently solved this issue, I found out that whenever you use relative paths in an HTML file or any other files, it uses the very root of your directory of your computer as I saw in the source code of the HTML, I used live-server via CLI, but try this in your VSCode settings if it works:

{
    "liveServer.settings.mount": [
        ["/"],
    ]
}

I don't know the exact settings for this since I don't use VSCode, I just researched the settings for the live-server for VSCode.

As long as the root is mounted, relative paths will work.

Thanks a lot. I had the same problem when i added js relative path

@phuxuan2k3
Copy link

I encountered the same problem and it doesn't work with the type="text/css" as well. E.g., https://127.0.0.1:5500/temp.css is served where https://127.0.0.1:5500/../temp.css must be served. Actually this seems to be "the expected" and "fine" behavior, because (if I'm not wrong) a web server must not serve files outside its web root(the path at which it started serving files).

So for instance, let's say we have such a structure:

/parent
    style1.css
    /project
        index.html
        /assets
            /stylesheets
                style2.css
            /fonts
                roboto.woff

assuming we are in the "/project", it will be our current path, and when we start the live server there, it (our current path, i.e., the "/project") will become our "web root", denoted by a slash ( / ) after the IP address. Now if we try to add the roboto font in style2.css, it would be possible using a relative path like this: url(../fonts/roboto.woff), and it works because it is still completely INSIDE the web root space. But a relative path won't work if tries to access a file outside the web root space, for example, if index.html tries to link to the stylesheet style1.css, it will fail, because it is above (outside of) the realm of the served web root(i.e., the / which in example above is the /project directory). Hope it helps.

I encountered the same problem and it doesn't work with the type="text/css" as well. E.g., https://127.0.0.1:5500/temp.css is served where https://127.0.0.1:5500/../temp.css must be served. Actually this seems to be "the expected" and "fine" behavior, because (if I'm not wrong) a web server must not serve files outside its web root(the path at which it started serving files).

So for instance, let's say we have such a structure:

/parent
    style1.css
    /project
        index.html
        /assets
            /stylesheets
                style2.css
            /fonts
                roboto.woff

assuming we are in the "/project", it will be our current path, and when we start the live server there, it (our current path, i.e., the "/project") will become our "web root", denoted by a slash ( / ) after the IP address. Now if we try to add the roboto font in style2.css, it would be possible using a relative path like this: url(../fonts/roboto.woff), and it works because it is still completely INSIDE the web root space. But a relative path won't work if tries to access a file outside the web root space, for example, if index.html tries to link to the stylesheet style1.css, it will fail, because it is above (outside of) the realm of the served web root(i.e., the / which in example above is the /project directory). Hope it helps.

thank you, it help me alot

@zinthose
Copy link

This is one of those "it's a feature, not a bug" scenarios.

I needed a means to run a dev set of files while still being able to use some of the resources in root while using the CSS files in dev.

This allows me to use "css/styles.css" from "./dev/index.html" and not from ".dist/css/styles.css"

It's a hack, but it works for my needs.

{
    "liveServer.settings.root": "/dist",
    "liveServer.settings.mount": [
        ["/", "./"], // Needed to ensure relative paths to subfolders work in /dev
        ["/dev", "./dev"],
        ["/dev/images", "./dist/images"],
    ],
}

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

8 participants