9

Really basic question, but I'm hoping to get some clarity here.

Let's say I have a static website with some html, some css and basic javascript. No web app to back it up. When testing the website locally, why do I need to run a web server to serve the content to the browser?

Is it because the relative paths in the URLs used in web pages are relative to the folder where the server is retrieving the content from and thus the various files included from the central HTML page can't quite get located on the file-system?

1
  • 2
    A good example of when a static website works without a webserver is when you save a website, either from a browser (save page as) or with a tool like httrack or wget. Those cases do most of the things you're asking about. – Journeyman Geek Jan 30 '13 at 2:15
8

Can static websites be viewed without a server?

Yes.

No Surprise

When testing the website locally, why do I need to run a web server to serve the content to the browser?

You don't.

Is it because the relative paths in the URLs used in web pages are relative to the folder where the server is retrieving the content from and thus the various files included from the central HTML page can't quite get located on the file-system?

No.

12
  • 1
    Just to clarify, I'm aware that you can load any page on disk into your browser. However, will the local files linked in the html (e.g. <link href="css/mycss.css [...]/> or <script src="js/foo.js" [...]</script>) be referenced correctly? Is there a way to have the same URL work for both the way you showed, and when the same website is served through a server? – glitch Jan 30 '13 at 2:08
  • 1
    Great, thanks for taking the time to put that together :) – glitch Jan 30 '13 at 2:25
  • 1
    Back in ancient times before we added a CGI shopping cart, this was the way we tested for broken links. Xenu link sleuth on a local disk copy beat wasting time on the slow internet of the day. After implementing the shopping cart, we had to build an internal test server for running the CGI efficently for testing. – Fiasco Labs Jan 30 '13 at 2:53
  • 2
    @glitch // is protocol-relative, so I think you'll end up with file://[whatever]/ajax.googleapis.com/.... Which is what you're asking for. – ta.speot.is Jan 30 '13 at 6:39
  • 1
    It can work... Only if all your links are relative. For instance in index.html you need to reference the style.css file as style.css, while in stuff/index.html you need to reference it as ../style.css. If you want to refer to it as /style.css in both places, though luck. – Norswap Jul 20 '13 at 16:21
4

Static websites can be viewed without a server, locally, simply by opening them up in a web browser, and you can do whole big websites this way if you want. But there are limitations and drawbacks, which is why people and systems use local servers for static websites.

One of the reasons you may be asking is because using many of the popular static site generators, like Jekyll, requires that you tell the system to --serve it (or similar), creating a local server for the static site. But if it's static, why do you need a server?

Here are a few reasons:

  • only relative links are safe (/ as a link to home is broken, as is referencing /css/something.css, since / is the root of your computer, not the site);
  • relatedly, directory-level links fail (blog/ shows the files under that directory, instead of looking for blog/index.html or similar);
  • and browsers treat local files differently among themselves and between them and hosted files (Chrome limits AJAX calls, IE uses a different protocol from everyone else, etc.).

So, no, you don't need it, but you do need it if you want those things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.