1+ /**
2+ * Get the base path from environment variable
3+ * This is used to support hosting the app in a subfolder
4+ */
5+ export function getBasePath ( ) : string {
6+ // On the client side, we can get the base path from the environment
7+ if ( typeof window !== 'undefined' ) {
8+ return process . env . NEXT_PUBLIC_BASE_PATH || '' ;
9+ }
10+
11+ // On the server side, use the environment variable
12+ return process . env . BASE_PATH || '' ;
13+ }
14+
15+ /**
16+ * Create a URL that respects the base path configuration
17+ */
18+ export function createUrl ( path : string ) : string {
19+ const basePath = getBasePath ( ) ;
20+
21+ // Remove leading slash from path if it exists
22+ const cleanPath = path . startsWith ( '/' ) ? path . slice ( 1 ) : path ;
23+
24+ // If no base path, just return the path with leading slash
25+ if ( ! basePath ) {
26+ return `/${ cleanPath } ` ;
27+ }
28+
29+ // Combine base path with the route path
30+ return `${ basePath } /${ cleanPath } ` ;
31+ }
32+
33+ /**
34+ * Get the current base path for client-side usage
35+ * This extracts the base path from the current URL
36+ */
37+ export function getCurrentBasePath ( ) : string {
38+ if ( typeof window === 'undefined' ) {
39+ return getBasePath ( ) ;
40+ }
41+
42+ const pathname = window . location . pathname ;
43+
44+ // Known application routes - adjust these based on your app structure
45+ const appRoutes = [ '/' , '/api' ] ;
46+
47+ // Check if current path contains any known route
48+ for ( const route of appRoutes ) {
49+ const routeIndex = pathname . indexOf ( route ) ;
50+ if ( routeIndex > 0 ) {
51+ return pathname . substring ( 0 , routeIndex ) ;
52+ }
53+ }
54+
55+ // If we're at root and there might be a base path
56+ if ( pathname !== '/' ) {
57+ // Check if this could be a base path by looking for common patterns
58+ const segments = pathname . split ( '/' ) . filter ( Boolean ) ;
59+ if ( segments . length === 1 ) {
60+ return `/${ segments [ 0 ] } ` ;
61+ }
62+ }
63+
64+ return '' ;
65+ }
0 commit comments