Let's say you have a scenario where you want to build a simple web stack (html/css/js) prototype to share with someone.

I am a little unclear on whether or not I want/need to use a small web-server to do so or whether it is appropriate to recommend opening the root html file (lets say index.html) locally in your browser.

In the index.html file, it will reference other local js/css files like so:

    <script src="./test.js"></script>

This seems to work fine for me locally in Chrome, but I am curious if there are any drawbacks/limits to using this approach that aren't immediately obvious, I was having trouble finding documentation on how this works compared to setting up a local web server.

  • 1
    I guess it depends mostly on the "who" and "why". if you were to share it with another developer to get critiques/peer review or to allow them to "run with it"/do their piece for example, then there would be risks of having too many copies flying around. Happen to have a target on who (who can be anything as vague as another developer or friends) to share it with and the intent on sharing, or is that still open-ended? – eparham7861 Oct 19 '20 at 2:04
  • You can use git to facilitate. I'm more concerned with drawbacks of expecting a dev or person testing to rely on the browser resolving files locally without http requests vs. spinning up a small local web server. – z0d14c Oct 19 '20 at 8:48
  • Right, you can use git for that. Didn't want to assume, since it wasn't mentioned. If the requests would be to something else local, like a local running web service that you are referencing, then I could see an issue there, but if the requests are to known "hosted" things, then that should be good. The main thing is to have all of your local pieces (extra js, css, other files, or other built artifacts) together and relatively referenced then ensure that this other person downloads/pulls all of the expected content. Spinning up the small local web server is safer but overkill. – eparham7861 Oct 19 '20 at 14:51

this can become problematic if your JS needs to make any REST requests. The CORS policies will simply reject you. You will not be able to perform any OAuth either. If you want to use any decent js framework like react or angular, then those also have to be transpiled before loading into a browser or you will just see a blank page with a nice error in a js console. normally the way to avoid it in this case is to start a development server provided by the development tools of each of these frameworks.

But yes, if your goal is to create a simple app, like a calculator or something that doesn't need any backends or authentication and doesn't need any transpilation, just go for it! This is definitely the way to go if you're just learning the javascript

  • I think this answer is probably decent enough to accept, although I am interested in reading more about how the individual browsers handle this in more detail/under the hood. – z0d14c Nov 22 '20 at 20:36

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.