bskimball / tanstack-hono

A simple example of using Tanstack Router SSR with Hono

Repository from Github https://github.combskimball/tanstack-honoRepository from Github https://github.combskimball/tanstack-hono

TanStack Router + Hono SSR

A modern full-stack React application combining TanStack Router with Hono for server-side rendering. This setup delivers fast, SEO-friendly applications with excellent developer experience.

πŸš€ Features

  • πŸ—Ί TanStack Router: Type-safe, file-based routing with powerful data loading
  • ⚑ Hono SSR: Ultra-fast server-side rendering with minimal overhead
  • πŸ”₯ Vite: Lightning-fast development with Hot Module Replacement
  • πŸ“˜ TypeScript: Full type safety across client and server
  • 🎨 Tailwind CSS: Modern utility-first CSS framework
  • 🧹 Biome: Fast linting, formatting, and code quality
  • πŸ§ͺ Vitest: Fast unit testing with great DX

πŸ“ Architecture

src/
β”œβ”€β”€ components/
β”‚   └── Header.tsx            # Reusable UI components
β”œβ”€β”€ routes/                   # File-based routing (auto-generated)
β”‚   β”œβ”€β”€ __root.tsx            # Root layout component
β”‚   β”œβ”€β”€ index.tsx             # Home page route
β”‚   β”œβ”€β”€ about.tsx             # About page route
β”‚   └── -test.ts              # Test route utilities
β”œβ”€β”€ entry-client.tsx          # Client-side hydration entry
β”œβ”€β”€ entry-server.tsx          # Hono server with SSR setup
β”œβ”€β”€ router.tsx                # Router configuration
└── styles.css                # Global styles

πŸ›  Development

Start the development server:

npm install
npm run dev

This starts:

  • Hono server with SSR at http://localhost:3000
  • Vite dev server with HMR for instant updates
  • TanStack Router with file-based routing

πŸ— Production Build

Build and run for production:

# Build client and server bundles
npm run build

# Start production server
npm start

The build creates:

  • Client bundle: Optimized assets with Vite manifest
  • Server bundle: Hono server with embedded SSR

πŸ§ͺ Testing

Run the test suite:

npm run test

🎨 Styling & Code Quality

Tailwind CSS for styling:

# Styles are processed through Vite with Tailwind

Biome for code quality:

npm run lint      # Check for issues
npm run format    # Format code
npm run check     # Lint + format

πŸ”„ SSR Flow

  1. Request: Browser requests a URL
  2. Server: Hono matches route and runs TanStack Router SSR
  3. Render: React components render to HTML string
  4. Response: Full HTML sent to browser with embedded data
  5. Hydration: Client-side React takes over for SPA navigation

πŸ—Ί File-Based Routing

Routes are automatically generated from files in src/routes/:

// src/routes/about.tsx
import { createFileRoute } from "@tanstack/react-router";

export const Route = createFileRoute("/about")({
  component: AboutPage,
});

function AboutPage() {
  return <div>About us!</div>;
}

πŸ”— Navigation with SSR

Links work seamlessly between server and client:

import { Link } from "@tanstack/react-router";

<Link to="/about">About</Link>; // Works with SSR + client routing

πŸ“Š Data Loading

Loaders run on the server and hydrate on the client:

export const Route = createFileRoute("/users")({
  loader: async () => {
    // Runs on server during SSR, then on client for navigation
    const users = await fetch("/api/users").then((r) => r.json());
    return { users };
  },
  component: UsersPage,
});

function UsersPage() {
  const { users } = Route.useLoaderData();
  return (
    <ul>
      {users.map((user) => (
        <li key={user.id}>{user.name}</li>
      ))}
    </ul>
  );
}

🏠 Layouts with SSR

The root layout (src/routes/__root.tsx) wraps all pages:

import { Outlet, createRootRoute } from "@tanstack/react-router";
import { Header } from "../components/Header";

export const Route = createRootRoute({
  component: RootLayout,
});

function RootLayout() {
  return (
    <>
      <Header />
      <main>
        <Outlet /> {/* Child routes render here */}
      </main>
    </>
  );
}

⚑ Performance Benefits

SSR Advantages:

  • SEO: Fully rendered HTML for search engines
  • LCP: Faster Largest Contentful Paint
  • Progressive Enhancement: Works without JavaScript
  • Social Sharing: Rich preview cards with meta tags

Hono Benefits:

  • Small Bundle: Minimal server overhead
  • Edge Ready: Deploy to Cloudflare Workers, etc.
  • Fast Startup: Quick cold start times

πŸš€ Deployment

Deploy to any Node.js hosting platform:

npm run build
NODE_ENV=production node dist/server.js

Popular platforms:

  • Vercel/Netlify: Serverless functions
  • Railway/Render: Container deployment
  • Cloudflare Workers: Edge deployment with Hono
  • Traditional VPS: PM2 + Nginx

πŸ“š Learn More

About

A simple example of using Tanstack Router SSR with Hono


Languages

Language:TypeScript 97.4%Language:CSS 2.6%