Do I have to use external CSS and JS files?

Only sometimes

Every article you read will tell you to use external files for your CSS and JavaScript. Is this rule warranted for? Are there any advantages to it? Is it considered best practice? Yes, yes, yes. There are quite some good reasons to do so, but when we leave the world of web pages and enter applications, the rule is not so strict.

The obvious advantage is separating the logic from the presentation. By making the CSS file external, you can make changes to the style without touching your HTML. The same goes for JavaScript.

Especially when your project starts to become big, it becomes really hard to track everything. Having separate parts of your project makes it easier to maintain, and it allows multiple people work on different files much easily.

The alternative is to dump every directly on the page. Kinda like I do it on this website. Go ahead, check the page source. Don't worry I am not doing this just to stick it to the man. I have very good reasons.

Well in my development process, I still use external files. So I have the same workflow as a team who would be working on multiple files. The difference is, I consider my websites to be applications. When I finish testing them on my local machine, I run a build and it creates the necessary files I need to upload it on my production server.

During the build, all the CSS is combined into a single file and added directly to the main template. The same for the JavaScript. The obvious advantages is that I don't have to download extra files for my CSS and JavaScript, so my pages load really fast.

The other advantage is the development process does not change. When the code is modularized, the right CSS is loaded at the right time and I don't have to worry problems with code reuse.

So the answer is you don't have to use external files if you are building applications. The build process takes care of the things that would manually be a pain to do. Also web crawlers don't care how your generated HTML look like, as long as it is correct.


Let's hear your thoughts

For my eyes only

There are no comments added yet.