0

I've just started using astro and have been finding a weird behavior when building my site. Specifically, although the build process creates a .css file in an assets folder and that is linked in the resulting .html file, my browser does not recognize the presence of this folder of .css file.

My index.astro looks like this:

<html lang="en">
    <head>
         <meta charset="utf-8" />
         <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
         <meta name="viewport" content="width=device-width" />
         <meta name="generator" content={Astro.generator} />
         <title>Astro</title>
         <style> body {color: red;}</style>
    </head>
    <body>
             <h1>Astro</h1>
    </body>
</html>

The resulting directory structure from npm run build is the following:

  • dist/assets/index.css
  • dist/index.html

Where index.html looks like the following:

<!DOCTYPE html>
<html lang="en" class="astro-5LR5QOZY">
<head>
    <meta charset="utf-8">
    <link rel="icon" type="image/svg+xml" href="/favicon.svg">
    <meta name="viewport" content="width=device-width">
    <meta name="generator" content="Astro v1.7.2">
    <title>Astro</title>
    <link rel="stylesheet" href="/assets/index.css" />
</head>
<body class="astro-5LR5QOZY">
        <h1 class="astro-5LR5QOZY">Astro</h1>
</body>
</html>

And index.css has one line, body {color:red;}. When I open index.html in Chrome, styling is plain and assets is not found within the directory structure:

Sources in Chrome

Any help?

I've tried using type="text/css" and that didn't fix the problem.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • Try removing the leading `/` from the style sheet href to make it `assets/index.css`. This will make it truly relative, rather than starting from the top of the filesystem. – kmoser Dec 20 '22 at 05:12

1 Answers1

1

It looks like you double clicked the file to open it in your browser, this will not work because your build is suppose to be served using a webserver.

Astro has a command astro preview which will run a server locally for you to view your build at http://localhost:3000

Bryce Russell
  • 647
  • 3
  • 8