GitHub repo
Check out the GitHub example repo if you’d like to see the full application code. It has a few additional features, like a simple Hono RPC implementation, and a SPA example.
Hono is a great framework to build serverless apps with familiar APIs using Web Standards. It comes with a ton of features out of the box.
One of these features is the ability to compose & render HTML server-side, which
is great for static content.
But what if you want to add some client-side logic to your app?
You can do so by using the hooks provided by Hono. However, they might not work
in your browser — that’s because we’re not shipping any JavaScript to the
browser!
In this guide we’ll go over how to build an Hono app with client-side logic, unlocking the full potential of your projects.
We’re building a simple app that renders a counter component server-side and
hydrates it client-side.
It runs in Cloudflare Workers, leveraging its
static asset bindings
- though the same principles apply to the other supported environments as well.
Using Vite we’ll set up two build steps: one for the client-side logic and one for the server-side logic.
First, let’s get started with scaffolding a new Hono app.
The src
directory contains a single index.ts
file with a simple Hono app.
We’re adding a client
directory with an index and component:
Let’s start by setting up a simple counter component that increments a count when a button is clicked:
Then we import the component & hydrate it in the client entry file:
Your code editor might give you a hint that document
is not defined. Given we
added the client-side logic, we need to tell TypeScript that we’re running in a
browser environment:
We’re going to add some JSX to the src/index.ts
file, so we first need to
change the file extension to .tsx
.
Once that’s done, we can add Hono’s
JSX renderer middleware
to the /
route and return the statically rendered <Counter />
component:
We’re almost there. If you run the app now with the dev
script, you’ll get an
error. Let’s fix that by adding the build steps!
At this point, we have both server-side and client-side logic and need to add two build steps to our project. Let’s install Vite and two plugins to facilitate this.
In the root of your project, create a vite.config.ts
file. We’ll define the
config for both the client-side build and the server-side build:
Now we need to adjust the package.json
scripts to facilitate the new build
steps. Additionally, we set the type to module
to allow for ESM imports:
If you run the app now with the dev
script, you’ll see the counter component
rendered server-side. The client-side script hasn’t been loaded yet, so the
counter component won’t work.
There’s only one step left to make the counter component work. We’re almost there!
As a final step we need to load the client-side script in the document’s head.
For the script that we’re loading we need to make a distinction between a
development and production environment. Vite allows us to do this easily with
its built-in env.
For the dev environment we can load the client’s .tsx
file; for production we
have to read it from the public
directory.
First we add the vite/client
types to the TypeScript config:
Then we adjust the src/index.tsx
file to load the client-side script,
depending on the environment:
Great! You can now run the app with the dev
script and see the counter
component in action.
To deploy the app to Cloudflare Workers we have to update wrangler.toml
so it
points to the correct worker build & resolves the public assets directory.
Lastly, we update the deploy
script.
You can now deploy your app to Cloudflare Workers with the deploy
script:
That’s it! You’ve built a simple Hono app with client-side logic. You can now extend your app with more complex client-side features, such as fetching data from an API route, adding a form to your project, or even building a full SPA.
GitHub repo
Check out the GitHub example repo if you’d like to see the full application code. It has a few additional features, like a simple Hono RPC implementation, and a SPA example.