Prompt file imported from xthxr/piik.me (
.github/prompts/plan-refactorToReactAndNextJsWithProperFileStructure.prompt.md). Copyright stays with the author.
Plan: Refactor to React and Next.js with Proper File Structure
Migrate the vanilla JS/Express app to a React/Next.js architecture with TypeScript and App Router for better maintainability, SEO, and performance. Convert static HTML/JS to dynamic React components using TSX, replace Express routes with Next.js API routes, and adopt a modular file structure (app/, components/, styles/, lib/, api/). Use GSAP for animations, Tailwind CSS for styling, and ensure proper library usage (e.g., TanStack Query for data fetching, Zustand for state management, Next.js Image/Font for optimization). Implement SEO with metadata API, error boundaries, and testing. This preserves all features like analytics, QR generation, bio links, and real-time updates while improving scalability.
Steps
-
Initialize Next.js project with React, TypeScript, and App Router; install dependencies (Firebase, Socket.io, Three.js, GSAP, TanStack Query, Zustand, Tailwind CSS, etc.); set up ESLint, Prettier, and testing (Jest + React Testing Library); create basic structure (app/, components/, styles/, lib/, api/).
-
Convert HTML pages (index.html, bio.html) to TSX in app/ directory, migrating vanilla JS logic to React components and hooks with TypeScript types; use Next.js metadata API for SEO.
-
Port Express server routes to Next.js API routes (/api/*), handling authentication, Firestore queries, and redirects; implement ISR for static bio links.
-
Implement authentication with Firebase Auth in React, using contexts and hooks for state management across protected routes.
-
Extract reusable UI elements (charts, modals, globe) into components/, integrate real-time updates via Socket.io, style with Tailwind CSS, and implement animations with GSAP; use Next.js Image and Font components.
-
Add error boundaries, loading states, and PWA features; test functionality with automated tests; optimize performance (e.g., lazy loading, code splitting); deploy to Vercel, ensuring feature parity.
-
Prioritize core features (URL shortening, analytics) before advanced ones (3D globe, bio links) to minimize downtime.
-
Address real-time and 3D rendering challenges by using client-side rendering and performance optimizations.
-
Use TypeScript/TSX throughout for type safety and better developer experience; leverage server components for static content.
Migration Commands & Steps
Phase 1: Project Setup
-
Backup current project:
# Create a new branch for migration git checkout -b nextjs-migration -
Initialize Next.js with TypeScript and App Router:
npx create-next-app@latest piik-next --typescript --tailwind --app --src-dir --import-alias "@/*" cd piik-next -
Install core dependencies:
npm install firebase firebase-admin socket.io-client three@^0.181.2 globe.gl@^2.45.0 d3-scale@^4.0.2 d3-scale-chromatic@^3.1.0 nanoid@^3.3.7 qrcode@^1.5.4 -
Install UI and animation libraries:
npm install gsap @tanstack/react-query zustand @headlessui/react @heroicons/react framer-motion recharts -
Install development tools:
npm install --save-dev @types/node @types/react @types/three @types/qrcode eslint prettier eslint-config-prettier jest @testing-library/react @testing-library/jest-dom @testing-library/user-event -
Add PWA and utility dependencies:
npm install next-pwa clsx tailwind-merge class-variance-authority
Phase 2: File Migration
-
Migrate static assets:
# Copy assets from old project cp -r ../piik.me/public/assets ./public/ cp ../piik.me/public/countries.geojson ./public/ -
Create new directory structure:
mkdir -p src/components/{ui,layout,analytics,qr,bio,modals} mkdir -p src/lib/{firebase,utils,hooks,api} mkdir -p src/app/api/{shorten,analytics,user,links,track} mkdir -p src/contexts mkdir -p src/types
Phase 3: Configuration
-
Set up environment variables:
# Create .env.local with Next.js naming cat > .env.local << EOL NEXT_PUBLIC_FIREBASE_API_KEY=your_api_key NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your_auth_domain NEXT_PUBLIC_FIREBASE_PROJECT_ID=your_project_id NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your_storage_bucket NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your_sender_id NEXT_PUBLIC_FIREBASE_APP_ID=your_app_id FIREBASE_PRIVATE_KEY=your_private_key FIREBASE_CLIENT_EMAIL=your_client_email GITHUB_TOKEN=your_github_token EOL -
Configure next.config.js:
// Add support for Socket.io, PWA, and custom webpack config for Three.js -
Update TypeScript configuration:
// Enable strict mode, paths, and proper module resolution -
Configure Tailwind CSS:
# Update tailwind.config.js with custom colors and animations
Phase 4: Code Migration Priorities
-
Priority modules to convert:
- Firebase config →
src/lib/firebase/config.ts - Auth logic (2886 lines in app.js) → React components + hooks
- Socket.io client setup →
src/lib/socket.ts - Bio links (1708 lines) →
src/app/[username]/page.tsx - QR generator →
src/components/qr/QRGenerator.tsx - Globe visualization →
src/components/analytics/GlobeView.tsx
- Firebase config →
-
API routes to port (from server.js - 1200+ lines):
/api/shorten→src/app/api/shorten/route.ts/api/analytics/:shortCode→src/app/api/analytics/[shortCode]/route.ts/api/user/*→src/app/api/user/*/route.ts/:shortCoderedirect →src/app/[shortCode]/page.tsx(with server-side redirect)/:username/:slug→src/app/[username]/[slug]/page.tsx
Phase 5: Testing & Deployment
-
Configure Vercel deployment:
# Update vercel.json for Next.js or remove (Next.js auto-configures) -
Run migration validation:
npm run build npm run lint npm run test
Key Migration Challenges Identified:
- Server.js is 1200+ lines with complex redirect logic and real-time analytics
- App.js is 2886+ lines of vanilla JS requiring careful component extraction
- Socket.io server needs migration to Next.js API route or external service
- Globe.gl and Three.js need client-side only rendering (use 'use client' directive)
- Bio link CSS animations (4185 lines) need GSAP conversion
- Firestore security rules need updating for new architecture