Why Next.js with Mizu?
Next.js brings several advantages to your Mizu applications: File-based routing - Define routes by creating files in theapp/ directory. No need to configure a router manually.
React Server Components - Write components that run during build time, reducing client-side JavaScript.
App Router - Modern routing with layouts, loading states, error boundaries, and parallel routes.
Developer Experience - Fast Refresh, TypeScript support, and excellent error messages out of the box.
Optimizations - Automatic code splitting, image optimization (with configuration), and font optimization.
Next.js with Mizu vs Standalone Next.js
| Feature | Next.js + Mizu | Standalone Next.js |
|---|---|---|
| Hosting | Single Go binary | Node.js server or Vercel |
| Backend | Go handlers | Next.js API routes |
| Deployment | Any server with Go | Node.js required |
| SSR | β No (static export) | β Yes |
| Static Export | β Yes | β Yes |
| API Routes | Go backend | JavaScript |
| Performance | β‘ Very fast (Go) | β‘ Fast (Node.js) |
| Type Safety | Backend: Go, Frontend: TS | Full-stack TypeScript |
- You want Next.js features (App Router, RSC, file-based routing)
- You prefer Go for backend APIs
- You want a single binary deployment
- Youβre building a static-exportable site
- You need full SSR (server-side rendering at request time)
- You want Next.js API routes
- You prefer Node.js for everything
- Your team is all JavaScript/TypeScript
How Static Export Works
Next.jsβs static export generates HTML files at build time:- Next.js crawls all routes starting from page files
- Server Components execute and generate HTML
- Client Components are bundled into JavaScript
- Static HTML files are created for each route
- Assets are optimized and fingerprinted
- Mizu serves the pre-built HTML files
- Browser loads HTML + JavaScript
- React hydrates the page
- Client-side navigation takes over
- API calls go to Mizu backend (Go)
Quick Start
Create a new Next.js project with the CLI:http://localhost:3000 to see your app!
Project Structure
Configuration
Next.js Configuration
The key to using Next.js with Mizu is configuring static export:frontend/next.config.mjs
- output: βexportβ - Tells Next.js to generate static HTML files instead of running a Node.js server
- distDir: β../buildβ - Outputs files to
build/(one level up fromfrontend/) - images.unoptimized - Disables Next.js Image Optimization API (requires Node.js server)
Backend Configuration
app/server/app.go
app/server/routes.go
File-Based Routing
Next.js uses file-based routing in theapp/ directory. Each folder represents a route segment, and special files define the UI.
Route Files
| File | Purpose | Required |
|---|---|---|
layout.tsx | Shared UI for a segment and its children | Yes (root) |
page.tsx | Unique UI for a route, makes it publicly accessible | Yes |
loading.tsx | Loading UI (Suspense boundary) | No |
error.tsx | Error UI (Error boundary) | No |
not-found.tsx | 404 UI | No |
Example Routes
Root Layout
The root layout wraps your entire application:frontend/app/layout.tsx
Dynamic Routes
Use brackets for dynamic segments:frontend/app/users/[id]/page.tsx
Server Components vs Client Components
Next.js 13+ introduces React Server Components (RSC). Understanding the difference is crucial.Server Components (Default)
Components are Server Components by default. They run at build time (in static export mode):- Zero JavaScript sent to browser for the component logic
- Can read files, query databases (at build time)
- Better performance - less client-side JavaScript
- Cannot use hooks (useState, useEffect, etc.)
- Cannot use browser APIs
- Cannot handle user interactions directly
Client Components
Add'use client' directive for interactive components:
- Can use hooks and state
- Can handle user interactions
- Can use browser APIs
- Can use useEffect for side effects
- JavaScript bundle sent to browser
- Cannot use server-only features (fs, database)
Composition Pattern
Compose Server and Client Components:Data Fetching
Fetching in Client Components
UseuseEffect and fetch:
Using React Query
Install React Query for better data fetching:Metadata and SEO
Next.js makes SEO easy with metadata:Static Metadata
Dynamic Metadata
Image Optimization
Next.jsβs Image component requires a Node.js server for optimization. In static export mode, you have options:Option 1: Use Regular <img> Tags
Option 2: Use next-image-export-optimizer
This package optimizes images at build time:
Option 3: Optimize Manually
Use tools likesharp or ImageMagick to pre-optimize images before adding them to public/.
Development Workflow
Starting Development
- Next.js runs its dev server on port 3001
- Mizu runs on port 3000
- Requests to Mizuβs port 3000 get proxied to Next.js (except
/api) - You visit
http://localhost:3000in your browser - Hot reload works through Mizuβs proxy
Making Changes
Frontend changes:- Edit any file in
frontend/ - Next.js Fast Refresh updates browser instantly
- No restart needed
- Edit Go files
- Restart Mizu server
- Or use
airfor auto-reload:
Building for Production
Build the complete application:cd frontend && npm run build- Next.js static exportgo build -o bin/server cmd/server/main.go- Go binary with embedded frontend
Build Output
Running in Production
- Mizu web server
- Your Go API handlers
- Entire Next.js build embedded
Limitations with Static Export
When usingoutput: 'export', some Next.js features are unavailable:
| Feature | Available | Notes |
|---|---|---|
| File-based routing | β Yes | Works perfectly |
| Server Components | β Yes | Run at build time |
| Client Components | β Yes | Full support |
| Dynamic routes | β Yes | Using [brackets] |
| Layouts | β Yes | Full support |
| Loading UI | β Yes | Full support |
| Error boundaries | β Yes | Full support |
| API Routes | β No | Use Mizu Go handlers instead |
| Server-Side Rendering | β No | Only static export |
getServerSideProps | β No | Use client-side fetching |
revalidate | β No | No ISR in static export |
| Image Optimization API | β No | Use alternatives |
| Middleware | β No | Use Mizu middleware |
| Server Actions | β No | Use Mizu API routes |
Working Around Limitations
Instead of API Routes:Troubleshooting
Build Errors: βoutput: exportβ Issues
Error:app/api/
Solution: Remove app/api/ directory. Use Mizu Go handlers instead.
Hydration Mismatch Errors
Error:Images Not Loading
Error: Images show broken icon Cause: Next.js Image component requires server Solution: Use one of the image optimization alternatives mentioned above.Canβt Find Module
Error:tsconfig.json: