@@ -44,6 +44,24 @@ interface MenuItem {
44
44
icon ?: React . ReactNode ;
45
45
}
46
46
47
+ /** ---- BASE URL HELPERS (shared pattern across files) ---- */
48
+ function normalizeBasePrefix ( raw : string | undefined | null ) : string {
49
+ const trimmed = ( raw ?? "" ) . trim ( ) ;
50
+ if ( ! trimmed ) return "" ; // no base
51
+ // strip leading/trailing slashes then rebuild as "/segment/"
52
+ const core = trimmed . replace ( / ^ \/ + / , "" ) . replace ( / \/ + $ / , "" ) ;
53
+ return core ? `/${ core } /` : "/" ;
54
+ }
55
+ const BASE_PREFIX = normalizeBasePrefix ( process . env . NEXT_PUBLIC_BASE_URL ) ;
56
+ /** Builds an app-relative path under the configured base prefix. */
57
+ function withBase ( path : string ) : string {
58
+ // Accepts paths like "/virtual-keys" or "/?page=..." and prefixes with BASE_PREFIX when present.
59
+ const body = path . startsWith ( "/" ) ? path . slice ( 1 ) : path ;
60
+ const combined = `${ BASE_PREFIX } ${ body } ` ;
61
+ return combined . startsWith ( "/" ) ? combined : `/${ combined } ` ;
62
+ }
63
+ /** -------------------------------------------------------- */
64
+
47
65
const Sidebar2 : React . FC < SidebarProps > = ( {
48
66
accessToken,
49
67
setPage,
@@ -229,7 +247,7 @@ const Sidebar2: React.FC<SidebarProps> = ({
229
247
const pushToRootWithPage = ( page : string , useReplace = false ) => {
230
248
const params = new URLSearchParams ( ) ;
231
249
params . set ( "page" , page ) ;
232
- const url = `/?${ params . toString ( ) } ` ;
250
+ const url = withBase ( `/?${ params . toString ( ) } ` ) ;
233
251
if ( useReplace ) {
234
252
router . replace ( url ) ;
235
253
} else {
@@ -242,8 +260,8 @@ const Sidebar2: React.FC<SidebarProps> = ({
242
260
// Special-case: Virtual Keys target
243
261
if ( page === "api-keys" ) {
244
262
if ( refactoredUIFlag ) {
245
- // Go to the dedicated /virtual-keys page
246
- router . push ( "/virtual-keys" ) ;
263
+ // Go to the dedicated /virtual-keys page (always under base)
264
+ router . push ( withBase ( "/virtual-keys" ) ) ;
247
265
return ; // do not call setPage here (parity with previous behavior)
248
266
}
249
267
// Legacy behavior
@@ -255,8 +273,8 @@ const Sidebar2: React.FC<SidebarProps> = ({
255
273
// All other pages
256
274
if ( refactoredUIFlag ) {
257
275
// If currently on /virtual-keys, REPLACE it with /?page=...
258
- const onVirtualKeys = pathname ?. startsWith ( "/virtual-keys" ) ;
259
- pushToRootWithPage ( page , onVirtualKeys ) ;
276
+ const onVirtualKeys = pathname ?. startsWith ( withBase ( "/virtual-keys" ) ) ;
277
+ pushToRootWithPage ( page , ! ! onVirtualKeys ) ;
260
278
} else {
261
279
pushToRootWithPage ( page ) ;
262
280
}
0 commit comments