Fix the Page Without Styles

The store’s site is live at its own address, and every merge into main will keep it that way. It doesn’t look much like the store yet, though. The words made it onto the internet, but the colors, the layout, and the opening hours seem to have stayed home.

In this lesson, you’ll read what the browser says about the missing files, find out why a build that worked on your laptop breaks in this one place, and fix it with a single setting in vite.config.js. The fix goes through a pull request like every other change, so you’ll watch it deploy and see the styled site at the end. Start by checking where the page’s styles went.

Find Where the Page Looks for Its Files

A page that shows up without its styles usually loaded its HTML but not the files that the HTML points to. The browser keeps a record of every file it couldn’t load, so that’s the first place to look.

Open your live site in VS Code’s integrated browser, where you’ve looked at the site on your laptop. A github.io address doesn’t open there by itself, as the dev server’s Local link does, so start with an empty browser tab.

Open the Command Palette with Ctrl+Shift+P, or Cmd+Shift+P on macOS, and run Browser: Open Integrated Browser. You can also choose View → Browser. Type https://<your-login>.github.io/generic-grocery/ in the address bar at the top of the tab and press Enter.

The integrated browser has the same developer tools as Chrome, because VS Code is built on Chrome’s engine. Open them with the Developer Tools button in the browser’s toolbar, to the right of the address bar, or press F12 while the browser tab is active.

Don’t use Ctrl+Shift+I here, or Cmd+Option+I on macOS, because in VS Code that shortcut opens the tools for VS Code itself. Pick the Console tab, and it lists errors like these, with the relevant filename on the right (shown here below each message, to fit the page):

Language: Text
Failed to load resource: the server responded with a status of 404 ()
    index-tbq6vW2g.js:1
Failed to load resource: the server responded with a status of 404 ()
    styles-6ofwpFpl.css:1
Failed to load resource: the server responded with a status of 404 ()
    modulepreload-polyfill-P2Xu9kJm.js:1
Failed to load resource: the server responded with a status of 404 ()
    hours-DVBN0nQ2.js:1

The letters in your filenames may differ, but the status is the same. A 404 is the server’s answer when it has no file at the address the browser asked for. So GitHub Pages served the page itself, then couldn’t find its stylesheet or any of its scripts. Without the stylesheet, the page has no styles, and without the scripts, nobody fills in the hours.

To see which addresses the page asked for, look at the HTML that GitHub Pages serves. In the same developer tools, pick the Elements tab and expand <head>. Among its elements, these four load the files that failed:

Language: HTML
<script type="module" crossorigin src="/assets/index-tbq6vW2g.js"></script>
<link rel="modulepreload" crossorigin href="/assets/modulepreload-polyfill-P2Xu9kJm.js">
<link rel="modulepreload" crossorigin href="/assets/hours-DVBN0nQ2.js">
<link rel="stylesheet" crossorigin href="/assets/styles-6ofwpFpl.css">

Vite wrote these lines when it built dist/index.html. Every address starts with /, so the path begins at the root of the site, right after the domain name. For your live page, that root is https://<your-login>.github.io/, so the browser asked for https://<your-login>.github.io/assets/styles-6ofwpFpl.css.

That address is outside your site. GitHub Pages publishes a repository’s site as a project site, in a folder named after the repository, /generic-grocery/. The root of <your-login>.github.io belongs to your account as a whole, and it doesn’t have an assets/ folder. The files are at /generic-grocery/assets/, one folder further in.

Your links between pages, such as href="deals.html" in the menu, don’t start with /. They count from the page’s own folder, so Weekly deals still opens the deals page, as unstyled as the home page. Only the addresses that Vite generated for the built files are wrong.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.