|
| 1 | +/** |
| 2 | + * URL utilities for handling baseUrl in Docusaurus deployments |
| 3 | + * Works for dev, GitHub Pages, and custom domains |
| 4 | + * |
| 5 | + * Simple approach: Detect the base path from the current URL |
| 6 | + * No hardcoding - just use whatever path the user is currently on |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Detects the base path from the current URL |
| 11 | + * |
| 12 | + * Docusaurus respects baseUrl even in dev mode. |
| 13 | + * If baseUrl is "/robolearn/", you access the site at http://localhost:3000/robolearn/ |
| 14 | + * |
| 15 | + * So we detect the base path from the current URL pathname. |
| 16 | + */ |
| 17 | +function detectBasePath(): string { |
| 18 | + if (typeof window === 'undefined') return ''; |
| 19 | + |
| 20 | + const pathname = window.location.pathname; |
| 21 | + |
| 22 | + // Extract the first path segment (e.g., /robolearn/docs -> /robolearn) |
| 23 | + const match = pathname.match(/^\/([^/]+)/); |
| 24 | + |
| 25 | + if (match) { |
| 26 | + const firstSegment = match[1]; |
| 27 | + // If first segment is a known app/content path, we're at root |
| 28 | + // These are Docusaurus routes that don't indicate a baseUrl |
| 29 | + // Add custom routes like 'labs', 'chat' to this list |
| 30 | + if (['auth', 'api', 'docs', 'blog', 'search', 'labs', 'chat'].includes(firstSegment)) { |
| 31 | + return ''; |
| 32 | + } |
| 33 | + // Otherwise, we're in a subpath (e.g., /robolearn/) |
| 34 | + return `/${firstSegment}`; |
| 35 | + } |
| 36 | + |
| 37 | + return ''; // Root (pathname is just "/") |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Gets the home URL (with baseUrl if applicable) |
| 42 | + */ |
| 43 | +export function getHomeUrl(): string { |
| 44 | + const basePath = detectBasePath(); |
| 45 | + return basePath ? `${basePath}/` : '/'; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Constructs a redirect URI for OAuth callback |
| 50 | + * Uses the detected base path from current URL - no hardcoding needed |
| 51 | + */ |
| 52 | +export function getRedirectUri(): string { |
| 53 | + if (typeof window === 'undefined') { |
| 54 | + return 'http://localhost:3000/auth/callback'; |
| 55 | + } |
| 56 | + |
| 57 | + const basePath = detectBasePath(); |
| 58 | + return `${window.location.origin}${basePath}/auth/callback`; |
| 59 | +} |
| 60 | + |
0 commit comments