Static Site Hosting: Fast, Cheap and Simple
Learn how static hosting serves ready-made files, how to deploy and cache them, and which features need extra services.
Key takeaways
- A static host returns prebuilt files, which makes pages quick to serve and easy to cache around the world.
- With no server-side application or database to attack, there is much less to patch.
- "Static" describes how files are delivered; pages can still be interactive in the browser.
- Forms, accounts, search and payments need client-side code plus an external service or function.
- Your account, domain and build process still need protection.
Static site hosting serves files exactly as you uploaded them: HTML, CSS, JavaScript, images and downloads. No program on the server assembles a page for each visitor, so there is very little to slow down, break or break into. For documentation, portfolios, marketing pages and most blogs, it is often the best fit.
The limit is just as plain. Anything that must happen on a server, such as processing a form, checking a password or taking a payment, needs another service to do it.
Static compared with dynamic
On a dynamic site, a request triggers code. The code looks up data, fills a template and produces HTML on the spot. On a static site, that work happened earlier, either by hand or by a build tool, and the server only fetches the finished file.
A static page is not a lifeless one. JavaScript in the browser can run animations, filter a table, play media or fetch fresh data from an API after the page loads.
Why it is fast
Delivering a file is about the cheapest thing a server can do. Many static hosts also place copies on a content delivery network, so a visitor receives the page from a nearby location instead of crossing an ocean.
Speed is not automatic, though. A static page with enormous images, several web fonts and a large script bundle will still feel slow on a phone. Delivery is only the first part of loading; the browser still has to process what it receives, as how a browser turns code into pixels describes.
Why it is cheap and sturdy
Files need little compute, so providers can host small static sites at low cost, and several offer free tiers with limits on builds, bandwidth or team members. Check those limits and the terms for commercial use, since they differ and change.
Traffic spikes are easier to absorb because cached files can be served many times over without any extra work per visitor. There is no database to exhaust and no application process to crash. If you use metered object storage as the origin, watch data transfer charges, which grow with popularity.
Where static sites live
There are several workable homes:
| Option | How it works | Good for |
|---|---|---|
| Static hosting platform | Connect a repository; it builds and publishes | Most projects |
| Object storage plus CDN | Upload files to a bucket and put a CDN in front | Teams already on a cloud platform |
| Ordinary web hosting | Upload files over SFTP | Anyone who already has a plan |
| Your own server | Configure a web server to serve a folder | Learning, or special requirements |
Object storage is a service that keeps files addressed by name. When used for a website, pay attention to access settings so that only the public files are readable.
Publishing a change
A common modern workflow builds and deploys automatically.
If your site is a handful of hand-written files, you can skip the build and upload them directly. Generators are useful once you have repeated layouts or many articles; see static site generators explained and Git basics for web projects.
To preview a folder on your own computer, any small local web server will do. If Python 3 is installed, run this inside the site folder:
python3 -m http.server 8000 On Windows the command may be py -m http.server 8000. Then open http://localhost:8000/. This server is for local testing only.
Setting up caching
Because files change only when you publish, they cache well. A reliable pattern:
- Give assets a fingerprint in the file name, such as
app.3f9c2a.css, and let browsers keep them for a long time. - Let HTML be revalidated each time, so visitors see new pages that point to new asset names.
The headers look like this:
# Fingerprinted assets
Cache-Control: public, max-age=31536000, immutable
# HTML pages
Cache-Control: no-cache no-cache allows storing the file but requires checking with the server before reuse. Browser caching and Cache-Control headers explains each directive.
Filling the gaps
You can add dynamic features one at a time.
- Contact forms. Use a form-handling service, or a small server-side function that validates input and sends mail. Plan for spam.
- Search. Build an index at publish time and search it in the browser for small sites, or use a hosted search service.
- Comments. Embed a third-party system, and consider what it tracks.
- Payments. Link to a hosted checkout run by a payment provider, so card details never touch your pages.
- Logins and private content. Use an identity service, and enforce access on the server side. Hiding content with browser code does not protect it.
- Custom logic. Serverless functions run short pieces of code when called.
One rule matters above the rest: anything shipped to the browser is public. Never put private API keys or passwords in static files or in client-side JavaScript, even if the code looks scrambled.
What static hosting does not remove
Security is simpler, not finished. Someone who takes over your hosting account, code repository or domain can replace your site. Protect those accounts with unique passwords and two-factor sign-in.
Third-party scripts run with real power on your pages, so include only what you need. Build tools depend on packages that need updating. HTTP security headers, including a content security policy, still apply.
If you point a custom domain at a platform and later delete the project, remove the DNS record too, or someone else may be able to claim that name on the same platform.
Redirects, error pages and clean addresses
Static hosts differ in how they handle the small server behaviors a site needs. Check that you can do these:
- Redirects. When you rename a page, the old address should send visitors to the new one with a permanent redirect. Platforms usually read a redirect list from a configuration file. See how to redirect pages without losing visitors.
- A custom error page. A missing address should return the 404 status along with a helpful page, not a success code.
- Directory addresses. Requests for
/guides/should serve theindex.htmlinside that folder, and you should pick one style, with or without the trailing slash. - Custom headers. You need these for caching and security settings.
Because these settings are specific to each platform, keep them in your repository where you can, and note any that live only in a dashboard.
Rolling back
One quiet advantage of generated sites is easy recovery. Each deployment is a complete set of files, and many platforms keep previous ones so that you can switch back in moments. If yours does not, you can rebuild an earlier commit from version control.
That only works when the sources are complete. Images stored outside the repository, content fetched from an outside service during the build and secret settings entered in the platform all need their own backup. Try building the site from a fresh copy of the repository now and then. If it fails on a clean machine, you have found the missing piece while there is no pressure.
Connecting your domain
Most platforms ask you to add records at your DNS provider, commonly a CNAME for www and either address records or a provider-specific alias for the bare domain. They then issue a certificate automatically once the name resolves. Follow the platform's current instructions, and choose one canonical address, redirecting the other to it.
Check the result:
curl -I https://www.example.com/
curl -I http://example.com/ The second request should answer with a redirect to your chosen HTTPS address.
A pre-launch checklist
Before pointing visitors at a new static site, walk through these:
- Every page loads over HTTPS, and plain HTTP redirects to it.
- A made-up address returns your error page with a 404 status.
- Internal links work, including links that start from a subfolder.
- No drafts, notes, configuration files or the repository folder were published.
- Images are sized for the places they appear.
- Each page has its own title and description, and you have a sitemap if the site is large.
- Forms reach a real inbox, tested from a device other than yours.
- Someone besides you knows how to publish a change and roll one back.
The fourth item deserves a minute. Because a static host serves whatever is in the published folder, a stray spreadsheet or backup archive is public as soon as someone guesses or finds its address.
When static is the wrong choice
Choose something else when most pages differ for each signed-in user, when content changes second by second and must be correct immediately, or when non-technical editors need to publish and the build workflow would frustrate them. A hosted editing interface connected to a generator can solve the last case, at the cost of another moving part.
Very large sites can also develop long build times, since a change to a shared template may rebuild thousands of pages. Some tools rebuild only what changed.
For everything in between, a mix works well: static pages for the content that everyone sees the same way, and a few focused services for the parts that need to think.