Skip to main content
Next.js is a powerful React framework that offers server-side rendering, static site generation, file-based routing, and an exceptional developer experience. When using Next.js with Mizu, you’ll leverage Next.js’s static export mode to generate a fully static site, while Mizu handles your backend API and serves the static files.

Why Next.js with Mizu?

Next.js brings several advantages to your Mizu applications: File-based routing - Define routes by creating files in the app/ 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

When to use Next.js with Mizu:
  • 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
When to use standalone Next.js:
  • 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:
What happens at build time:
  1. Next.js crawls all routes starting from page files
  2. Server Components execute and generate HTML
  3. Client Components are bundled into JavaScript
  4. Static HTML files are created for each route
  5. Assets are optimized and fingerprinted
What happens at runtime:
  1. Mizu serves the pre-built HTML files
  2. Browser loads HTML + JavaScript
  3. React hydrates the page
  4. Client-side navigation takes over
  5. API calls go to Mizu backend (Go)

Quick Start

Create a new Next.js project with the CLI:
Visit 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

Configuration explained:
  • 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 from frontend/)
  • 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 the app/ directory. Each folder represents a route segment, and special files define the UI.

Route Files

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):
Server Component benefits:
  • Zero JavaScript sent to browser for the component logic
  • Can read files, query databases (at build time)
  • Better performance - less client-side JavaScript
Server Component limitations:
  • Cannot use hooks (useState, useEffect, etc.)
  • Cannot use browser APIs
  • Cannot handle user interactions directly

Client Components

Add 'use client' directive for interactive components:
Client Component benefits:
  • Can use hooks and state
  • Can handle user interactions
  • Can use browser APIs
  • Can use useEffect for side effects
Client Component limitations:
  • 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

Use useEffect and fetch:

Using React Query

Install React Query for better data fetching:
Setup provider:
Use in components:

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 like sharp or ImageMagick to pre-optimize images before adding them to public/.

Development Workflow

Starting Development

How it works in development:
  1. Next.js runs its dev server on port 3001
  2. Mizu runs on port 3000
  3. Requests to Mizu’s port 3000 get proxied to Next.js (except /api)
  4. You visit http://localhost:3000 in your browser
  5. 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
Backend changes:
  • Edit Go files
  • Restart Mizu server
  • Or use air for auto-reload:

Building for Production

Build the complete application:
This runs:
  1. cd frontend && npm run build - Next.js static export
  2. go build -o bin/server cmd/server/main.go - Go binary with embedded frontend

Build Output

Running in Production

The binary contains:
  • Mizu web server
  • Your Go API handlers
  • Entire Next.js build embedded

Limitations with Static Export

When using output: 'export', some Next.js features are unavailable:

Working Around Limitations

Instead of API Routes:
Instead of SSR:

Troubleshooting

Build Errors: “output: export” Issues

Error:
Cause: You have API routes in app/api/ Solution: Remove app/api/ directory. Use Mizu Go handlers instead.

Hydration Mismatch Errors

Error:
Cause: Server-rendered HTML doesn’t match client-rendered HTML Solution: Ensure Server Components don’t use time-dependent values:

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:
Cause: Path alias not configured Solution: Check tsconfig.json:

Real-World Example: Blog with Comments

Here’s a complete example showing Server Components, Client Components, and API integration:

Backend

Frontend

When to Choose Next.js

Choose Next.js When:

✅ You want file-based routing without manual configuration ✅ You’re building a primarily static site (blog, marketing site, docs) ✅ You want to use React Server Components ✅ Your team knows React and wants enhanced DX ✅ You want automatic code splitting and optimizations ✅ You need good SEO with metadata support

Choose Vanilla React When:

✅ You want complete control over the setup ✅ You don’t need file-based routing ✅ You prefer a simpler build process ✅ Your app is highly dynamic (not suitable for static export) ✅ Bundle size needs to be minimal ✅ You don’t need Server Components

Next Steps

React Guide

Compare with vanilla React + Vite approach

Nuxt Guide

Vue equivalent of Next.js

API Integration

Best practices for API communication

Next.js Docs

Official Next.js documentation