Your site is HTML, CSS and JavaScript on a static host, and now it needs to remember something: a sign-up, a vote, a comment, a list that belongs to each visitor. There is no server of yours to put a database behind, and the host serves the same files to everybody.
You do not have to give up the static host. The data goes somewhere else, and the site talks to it. There are six places it can go. The right one comes down to two questions: who needs to see the data, and who is allowed to change it.
Is your data really a database problem?
Before picking a database, check that you need one. Three common cases do not:
- Data only you change, a few times a month — a menu, a price list, a team page. Keep it in a JSON or Markdown file in the site and publish again when it changes.
- Data only the visitor needs, on their own device — a theme choice, a draft, a game's high score. That is browser storage, and nothing leaves the device.
- Data you only need to receive — contact forms, sign-ups, applications. A form backend collects it and you read it in a dashboard.
Everything else — data visitors share with each other, or data that has to follow someone from their phone to their laptop — needs real storage on a server somewhere.
The 30-second version
| Where the data lives | Who sees it | Works on any static host | Good for |
|---|---|---|---|
The visitor's browser (localStorage, IndexedDB) |
That browser only | Yes | Settings, drafts, offline state |
| The site's own files (JSON, Markdown) | Everybody, read-only | Yes | Content you edit and republish |
| A form backend | You, in a dashboard | Yes | Contact forms, waitlists |
| A database the browser calls (Firestore, Supabase) | Whoever its rules allow | Yes | Apps with accounts and shared data |
| A function in front of a database | Whatever the function returns | Only hosts that run functions | Anything needing a secret key or custom checks |
| Storage the host runs (KV, SQLite, blobs) | Whatever the function returns | Only that host | Counters, sessions, small per-user data |
1. The visitor's browser
localStorage keeps strings per origin, on the device that wrote them. It is synchronous and simple:
localStorage.setItem("theme", "dark");
const theme = localStorage.getItem("theme"); // "dark", on this browser onlyThe limits are the point. MDN puts web storage at about 5 MiB per origin, it holds only strings, and the visitor's other devices never see it. Private windows drop it when they close. For larger or structured data, IndexedDB has the same "this device only" shape with more room.
Use it for preferences and for work in progress. Never use it for anything you need to read yourself: you cannot.
2. The site's own files
A static site generator — or a hand-edited data.json — is a database you query at build time. The page fetches /data/menu.json and renders it, or the build bakes it into HTML.
It is fast, free and cannot break at runtime. It cannot accept anything from a visitor, and every change is a new publish. That makes it right for content the owner controls and wrong for everything below.
3. A form backend
A form backend receives an HTML form and stores what was submitted. Some hosts build one in — Netlify Forms parses forms at deploy time on sites hosted there, and this site's own form collection does the same. Others are separate services, such as Formspree, where the form's action points at their endpoint:
<form action="https://formspree.io/f/your-form-id" method="POST">
<input type="email" name="email" required>
<button>Join the waitlist</button>
</form>It is write-only by design. You read submissions in a dashboard or receive them by email; the page cannot list them back. Free tiers cap submissions per month, and the caps change, so check the current plan before a launch.
4. A database the browser calls directly
Firebase's Cloud Firestore and Supabase both give the browser a client library and a key, and the page reads and writes the database itself. This is the fastest way to a real app with accounts on a static host — GitHub Pages included.
The key in the page is public on purpose. Firebase says its web API keys do not need to be treated as secrets, and Supabase calls its browser key the publishable key, safe to expose. So the key protects nothing. The rules do.
In Firestore, every request from a web client is checked against Security Rules. A rule that keeps each note to its owner:
match /notes/{noteId} {
allow read, update, delete: if request.auth != null && request.auth.uid == resource.data.owner;
allow create: if request.auth != null && request.auth.uid == request.resource.data.owner;
}In Supabase the rules are Postgres Row Level Security, and its docs are blunt: a table in an exposed schema without RLS is readable and writable by anyone holding the key.
alter table notes enable row level security;
create policy "Own notes" on notes for all to authenticated
using ((select auth.uid()) = user_id)
with check ((select auth.uid()) = user_id);The failure mode is always the same: a project goes live in test mode, or with one table missing its policy, and the whole database is open. Write the rules before the first real user, and test them signed out.
5. A function in front of a database
Some data cannot be guarded by rules alone: you need to call a paid API, check a payment, or use a database whose key must stay secret. Then the page calls a function on its own site, and the function holds the key.
Netlify, Vercel and Cloudflare Pages all run functions next to static files, each reading secrets from the environment — Cloudflare passes them on context.env, Netlify on process.env. GitHub Pages runs no server-side code, so there the function has to live on another platform.
The database behind the function can be any hosted one with an HTTP API or a driver that runs in a function: Postgres from Neon or Supabase, SQLite from Turso, Redis from Upstash. This is also the only option where the visitor cannot reach the database at all — they can only call the routes you wrote. The same pattern keeps an API key out of a page; that has a guide of its own.
6. Storage the host runs
If the host runs your functions, it may also store data for them, with no account elsewhere to manage:
- Cloudflare has Workers KV, D1 (SQLite) and Durable Objects. KV is eventually consistent — a write can take 60 seconds or more to be seen elsewhere — which suits configuration and caches, not counters. D1 gives you SQL; Durable Objects give you strongly consistent storage.
- Netlify has Blobs, a key-value store that is eventually consistent unless you ask for strong consistency per call.
- Vercel has Blob for files. Its KV and Postgres products moved to Marketplace integrations (Upstash and Neon) in December 2024.
- dropto.run gives each site's functions a store: keys and text values, listed by prefix, strongly consistent, with an atomic counter.
Consistency is the property that decides what these are good for. Here is a view counter written for an eventually consistent store:
// A counter that loses visits: two calls read 41, both write 42.
export async function onRequestPost({ env }) {
const count = Number((await env.COUNTS.get("visits")) ?? 0) + 1;
await env.COUNTS.put("visits", String(count));
return Response.json({ count });
}Two visitors at once both read 41 and both write 42. On a store with an atomic increment — Redis's INCR, a Durable Object, a SQL UPDATE … SET n = n + 1 — the count is right. Read the consistency line of the docs before you choose; it is never on the pricing page.
When not to do this
Stay with a static host and one of the above while the data is looked up by whose it is: a user's notes, a site's counter, a list of sign-ups.
Move to a real backend when the data is mostly searched across everyone — "every order over $50 this week", full-text search over all posts — or when work has to happen on a schedule rather than on a request. Key-value stores cannot query by value, and functions on static hosts usually cannot run a job at 3 a.m. At that point a server framework with its own database is less work than stretching any of these six.
A tutorial that builds the per-user case end to end — accounts, sessions and private notes, on functions and a key-value store — is in our documentation.
Questions
Can a static site have a database?
Not inside the site — a static host only serves files. But the site's JavaScript can call a database elsewhere: directly (Firestore, Supabase) with access rules, or through a function that holds the key.
Is it safe to put a Firebase or Supabase key in my JavaScript?
Yes, the browser key is meant to be public. What has to be right are the Firestore Security Rules or Supabase Row Level Security policies, because they decide what that public key can do.
Can I use localStorage as a database?
Only for data that belongs to one visitor on one device. Nobody else can read it, including you, and it is limited to a few megabytes of strings.
What is the cheapest way to collect form submissions on a static site?
A form backend. Many hosts include one, and separate services have free tiers. You will read submissions in a dashboard; the page cannot display them.
Does GitHub Pages support a database?
Not directly — it serves files and runs no code. Use browser storage, a form backend, or Firestore or Supabase called from the page, or put a function on another platform in front of a database.
What is the difference between eventually and strongly consistent storage?
After a write to eventually consistent storage, other locations may keep returning the old value for a while. Strongly consistent storage returns the new value to the very next read, anywhere. Counters, sign-up limits and sessions need the second.