How vaibhavsingh9798/geo-based-domain-switcher works
Project type and purpose
• Project type: Full-stack web application built with Next.js (app router API + React components).
• Purpose: Render a landing page, detect a visitor’s country via an internal API route (/api/get-country), and redirect or display messages based on geolocation.
File structure
• Top-level directories:
• app/ – Next.js App Router (page.tsx, layout.tsx, global CSS, API routes under app/api)
• components/ – Reusable React components (DomainMessage.tsx, GeoRedirect.tsx)
• lib/ – Business-logic helpers (geo.ts to fetch geolocation)
• Configuration files: next.config.ts, tsconfig.json, ESLint and PostCSS configs.
• Server-side code lives alongside UI code: API route under app/api/get-country/route.ts.
README for additional insights
• Bootstrapped via create-next-app
• Uses Next.js features: app directory, automatic font optimization via next/font.
• No external database is mentioned—only use of a geolocation API inside lib/geo.ts.
a. Main components
1. Client (Browser)
2. Next.js Server (App Router + Server-Side Rendering)
– app/layout.tsx, app/page.tsx
– React Server Components
3. Next.js API Route: /api/get-country (app/api/get-country/route.ts)
4. Business Logic Layer: lib/geo.ts (abstraction over external geolocation service)
5. External Geolocation Service (third-party HTTP API)
b. Relationships and Interactions
1. User ➔ HTTP GET / (root) ➔ Next.js Server ➔ Server Component (page.tsx) renders HTML.
2. Rendering logic may invoke client-side or server-side data fetch:
– Client calls /api/get-country
– Next.js Server routes to route.ts
3. route.ts invokes lib/geo.ts ➔ performs HTTP request to External Geo API ➔ returns JSON.
4. API Route responds to Client; UI components (GeoRedirect, DomainMessage) consume that JSON to drive redirect or display.
c. Architectural patterns and principles
• Monolithic Serverless / SSR pattern (Next.js App Router) combining page rendering and lightweight API endpoints.
• Separation of concerns:
• UI components under components/
• Layout and routing under app/
• Business logic under lib/
• Server Components vs. Client Components: can highlight which components run server-side (page.tsx) vs. client (GeoRedirect).
d. Key technologies
• Next.js App Router, React 18 Server Components
• Node.js runtime (Vercel serverless functions)
• Typescript (.ts, .tsx)
• PostCSS / global CSS (app/globals.css)