- Add Express server with vite-express for combined frontend/API serving - Create modular API route structure (server/api/) - Implement Claude API proxy with proper header injection - Support split deployment via API_ONLY and ALLOWED_ORIGINS env vars - Remove Claude proxy from Vite config (now handled by Express) - Add migration plan documentation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
4.8 KiB
4.8 KiB
Vite to Next.js Migration Plan
Goal
Migrate from Vite to Next.js App Router to get proper API route support, with minimal changes to existing code.
Configuration
- Router: App Router with
'use client'on all pages - Rendering: CSR only (no SSR) - simplifies migration since BabylonJS can't SSR
- API Routes: Claude API now, structured for future expansion
- External Proxies: Keep sync/create-db/images as Next.js rewrites to deepdiagram.com
Phase 1: Setup (No Breaking Changes)
1.1 Install Next.js
npm install next
1.2 Create next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{ source: '/sync/:path*', destination: 'https://www.deepdiagram.com/sync/:path*' },
{ source: '/create-db', destination: 'https://www.deepdiagram.com/create-db' },
{ source: '/api/images', destination: 'https://www.deepdiagram.com/api/images' },
];
},
webpack: (config, { isServer }) => {
config.experiments = { ...config.experiments, asyncWebAssembly: true };
return config;
},
};
module.exports = nextConfig;
1.3 Update tsconfig.json
Add path alias:
"baseUrl": ".",
"paths": { "@/*": ["./*"] }
Phase 2: Create New Files
2.1 src/react/providers.tsx (extract from webApp.tsx)
- Move Auth0Provider and FeatureProvider wrapping here
- Add
'use client'directive - Handle window/document checks for SSR safety
2.2 app/layout.tsx
- Root layout with html/body tags
- Metadata (title, favicon from current index.html)
- Import global CSS
2.3 app/globals.css
@import '../src/react/styles.css';
@import '@mantine/core/styles.css';
2.4 app/api/claude/[...path]/route.ts
- POST handler that proxies to api.anthropic.com
- Injects
ANTHROPIC_API_KEYfrom env - Adds
x-api-keyandanthropic-versionheaders
2.5 Page files (all with 'use client')
| Route | File | Component |
|---|---|---|
/ |
app/page.tsx |
About |
/documentation |
app/documentation/page.tsx |
Documentation |
/examples |
app/examples/page.tsx |
Examples |
/pricing |
app/pricing/page.tsx |
Pricing |
/db/[visibility]/[db] |
app/db/[visibility]/[db]/page.tsx |
VrExperience |
| 404 | app/not-found.tsx |
NotFound |
2.6 src/react/components/ProtectedPage.tsx
- Next.js version of route protection
- Uses
useRouterfromnext/navigationfor redirects
Phase 3: Modify Existing Files
3.1 src/react/pages/vrExperience.tsx
Changes:
- Remove
useParams()from react-router-dom - Accept
visibilityanddbas props instead - Replace
useNavigate()withuseRouter()fromnext/navigation
3.2 src/react/pageHeader.tsx
Changes:
- Replace
import {Link} from "react-router-dom"withimport Link from "next/link" - Change
to={item.href}tohref={item.href}on Link components
3.3 src/react/marketing/about.tsx
Changes:
- Replace
useNavigate()withuseRouter()fromnext/navigation - Change
navigate('/path')torouter.push('/path')
3.4 package.json
"scripts": {
"dev": "next dev -p 3001",
"build": "node versionBump.js && next build",
"start": "next start -p 3001",
"test": "vitest",
"socket": "node server/server.js",
"serverBuild": "cd server && tsc"
}
Phase 4: Delete Old Files
| File | Reason |
|---|---|
vite.config.ts |
Replaced by next.config.js |
index.html |
Next.js generates HTML |
src/webApp.ts |
Entry point no longer needed |
src/react/webRouter.tsx |
Replaced by app/ routing |
src/react/webApp.tsx |
Logic moved to providers.tsx |
src/react/components/ProtectedRoute.tsx |
Replaced by ProtectedPage.tsx |
Critical Files to Modify
src/react/pages/vrExperience.tsx- useParams -> propssrc/react/pageHeader.tsx- react-router Link -> Next.js Linksrc/react/marketing/about.tsx- useNavigate -> useRoutersrc/react/webApp.tsx- extract to providers.tsxpackage.json- scripts updatetsconfig.json- path aliases
Migration Order
- Install next, create next.config.js
- Update tsconfig.json
- Create app/globals.css
- Create src/react/providers.tsx
- Create app/layout.tsx
- Create app/api/claude/[...path]/route.ts
- Create src/react/components/ProtectedPage.tsx
- Modify vrExperience.tsx (accept props)
- Create all app/*/page.tsx files
- Modify pageHeader.tsx (Next.js Link)
- Modify about.tsx (useRouter)
- Update package.json scripts
- Delete old files (vite.config.ts, index.html, webApp.ts, webRouter.tsx, webApp.tsx)
- Test all routes
Notes
- Havok WASM: Move
HavokPhysics.wasmtopublic/folder - react-router-dom: Can be removed from dependencies after migration
- vite devDependencies: Can be removed (vite, vite-plugin-cp)