AWS Builder Center
I Killed My Server. The API Kept Working Anyway.

I Killed My Server. The API Kept Working Anyway.

I put a real Hono app inside a Service Worker, then shut down the actual server while the demo was running. The API didn't notice.

Cloud Engineer
Normally, when your browser asks a website for something, the answer comes from far away. Your laptop sends a question out over the internet, a computer somewhere else (the server) thinks about it, and an answer travels all the way back. That round trip is just how websites work. Everybody assumes it.
I wanted to see what would happen if I skipped the "far away computer" part entirely, and let the browser answer its own questions.


The trick hiding inside your browser already

Chances are your browser already has a feature called a Service Worker, even if you've never heard of it. Some websites use it to keep working when your Wi-Fi drops, or to show a little popup that says "new version available, refresh?" A Service Worker is a small script that your browser keeps running quietly in the background, and one of its jobs is to notice every single request the page makes and decide what to do with it before the request ever leaves your computer.
Here's the part that made me want to try this. A Service Worker's job, at its core, is: receive a request, hand back a response. That's it. And Hono, the web framework I use for almost everything these days, is built around exactly one function that does exactly that: you give it a request, it gives you back a response. Nothing about Hono cares whether that request came from the internet or from somewhere else. It just wants a request, and it hands back a response.
Two things with the exact same shape, sitting right next to each other. So I connected them.
self.addEventListener("fetch", (event) => { const url = new URL(event.request.url); if (!url.pathname.startsWith("/api/")) return; event.respondWith(app.fetch(event.request));});
That's the whole connection. No adapter, no translation layer, no special "browser mode" for Hono to turn on. The Service Worker catches the request, and Hono answers it, right there inside your browser tab.

Killing the server on purpose

Talk is cheap, so I built the smallest possible version: a page with three buttons, each one calling a different  /api/  route. One says hello, one echoes back whatever you send it, one keeps a running count in memory. I loaded the page, clicked the buttons, and watched real JSON answers show up.
Then I did the thing that actually proves the point. While the page was still open, with the Service Worker already running, I killed the little program that was serving the page in the first place. Not paused it, not slowed it down. Killed it, completely, on purpose.
I tried asking for the page again. Nothing came back, because there was truly nothing left to ask. But I clicked the buttons again anyway.
They still worked.
The API kept answering, perfectly normally, even though the thing that had originally handed the page to my browser no longer existed. Once your browser has loaded the Service Worker once, it doesn't need the original server for  /api/  requests anymore. The little script is already sitting inside your browser, and Hono is already sitting inside the script. The server was a one-time delivery, not something the API depends on to keep breathing.
I checked one more thing to make sure I wasn't fooling myself: how much data actually traveled over the network for one of these answers. The browser itself keeps a record of this, and it said zero. Zero bytes moved. The answer had real content in it, a real JSON message, but none of it crossed any wire to get to me. It was built and delivered entirely inside the browser.

Two things I didn't expect

I opened the same page in two separate tabs, thinking I'd get two separate mini-servers, one per tab. I didn't. Both tabs shared the exact same Service Worker, which meant the counter I mentioned earlier kept counting up no matter which tab I clicked the button in. Tab one, then tab two, then tab one again, and the numbers went one, two, three, in order, like they were talking to the same brain. Which they were.
Then I just reloaded the page, without touching anything else, and the counter reset back down. That one confused me for a minute, until I remembered that browsers don't let these background scripts run forever for no reason; if nothing happens for a little while, the browser shuts the script down to save battery and memory, and quietly starts a fresh copy the next time it's needed. A fresh copy means the running count I'd built up in memory was just gone, like nobody had ever asked for it.
So two tabs pointed at the same website will always be talking to the same tiny brain, but that brain can also fall asleep and forget everything between one page reload and the next. Both of those are true at the same time, and neither one felt obvious to me before I tried it.

The idea I had to give up on

Before I started, I'd pictured something almost silly: a single HTML file, small enough to attach to an email, that you could send a friend and have them double-click it and instantly get a working little API, no setup, no server, ever.
That part doesn't actually work, and it's worth saying plainly instead of pretending it does. Browsers only allow a Service Worker to register itself on pages that were loaded in a reasonably safe way, meaning a real web address starting with  https:// , or the special case of  localhost  for testing on your own machine. Double-clicking a file straight off your desktop doesn't count, no matter how small or clever the file is. Chrome will flatly refuse and say so.
Which means you do still need something, even if it's the smallest possible something: one line running a plain static file server is enough. But once that one line has done its job exactly once, everything after that, all the actual logic, all the routes, all the answers, lives entirely inside the browser. The server's only job was to say hello a single time and then it can leave.

What I actually think this is good for

I'm not going to pretend this replaces a real backend. If you need a database, or you're talking to other services, or more than one person needs to see the same data at the same time in a reliable way, this isn't that.
What it's genuinely good for is anything that's supposed to work entirely on your own device, occasionally offline, without asking a distant server for permission every time. And it's a neat reminder of something about Hono specifically: because it only ever assumes "you give me a request, I give you a response," it fits into a place as unusual as the inside of your own browser without anyone having to write special code to make that happen. It was already shaped the right way.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article