By Jenn Schiffer

October 15, 2020

Going from Full-stack to Static

Over the summer we announced fast and free static sites on Glitch, providing you a way to create and share simple webpages that never go to sleep. Static sites are great for ambitious client-side apps, landing pages for educational demos, compelling artworks from code - basically any project that needs only HTML, CSS and client-side JavaScript to come to life! Some of you have reached out asking how to turn your full-stack JavaScript apps into static sites so you can save on Project Hours and keep your projects awake for free, so I’m going to show you how to get started.

Glitch distinguishes between a static and full-stack site by watching for any of three simple configuration files:  package.json,  requirements.txt and glitch.json. Those two last files are for configuring apps built in other experimental-to-Glitch languages; for this tutorial I’m going to focus on full-stack JavaScript apps using Node.js, which are configured in package.json. If you’re not familiar with the anatomy of a Node app, you can check out our free zine on Making Web Apps with Node!

To get started, you’ll need a full-stack app that’s running all of its important code on the client-side. For this tutorial I created ~full-stack-to-static, whose server-side code only contains routes for where the client side files are.

The first thing I do is remove the server-side files and see what breaks and troubleshoot from there. So, I remove package.json, letting Glitch know that this should be a static site. Then I remove server.js, and .env since they will not be run without package.json.

Going to the app view, I see that something looks very wrong. Instead of my index.html content, I see a file listing like this:

This is because my index.html file is still in a“views” folder. Renaming“views/index.html” to“index.html” moves that file in the root of my project and fixes that.

I can now see index.html rendering but I now get errors when trying to load my stylesheet and JavaScript. This is because they are inside a“public” folder but my index.html references them as if they were in the root of my project - something my server-side code had handled before. I can fix this either by updating links in index.html to add the“public/” directory, or by renaming“public/style.css” and“public/script.js” to“style.css” and“script.js”. In this example, I did the latter and now my static site is looking great. Here's an embed of the final result:

Now I have a project that does basically the same thing it was doing before, but it stays awake and it doesn’t count towards my Project Hour limits. I recommend you take a look at your Project Dashboard and see if you have any full-stack projects using up Project Hours that could be refactored to static sites! And for all your projects that need to be full-stack apps, just remember as always that Glitch gives you 1000 free Project Hours each month to run your apps, and subscribers can lift those limits if you need even more.

*The project featured in this post's header image is ~iris-cockroach by @potch. Have a project you want us *to feature? Tweet them to us @glitch or email [email protected]!