@@ -15,50 +15,63 @@ export const config = {
1515 '/dashboard/:path*' ,
1616 '/dashboard/admin/:path*' ,
1717 '/install' ,
18- '/[ username] ' ,
18+ '/: username' ,
1919 ] ,
2020}
2121
2222export async function middleware ( req : NextRequestWithAuth ) {
23+ const pathname = req . nextUrl . pathname
24+
2325 // Redirect literal [username] path to login page
24- if ( req . nextUrl . pathname === '/[username]' ) {
26+ if ( pathname === '/[username]' ) {
2527 return NextResponse . redirect ( new URL ( '/login' , req . url ) )
2628 }
2729
28- if ( req . nextUrl . pathname === '/install' ) {
30+ if ( pathname === '/install' ) {
2931 return NextResponse . redirect ( new URL ( '/api/install' , req . url ) )
3032 }
3133
32- if ( req . nextUrl . pathname === '/dashboard/troubleshoot' ) {
34+ if ( pathname === '/dashboard/troubleshoot' ) {
3335 return NextResponse . redirect ( new URL ( '/dashboard/help' , req . url ) )
3436 }
3537
36- if ( ! process . env . EDGE_CONFIG ) {
37- req . nextUrl . pathname = '/missing-edge-config'
38- return NextResponse . rewrite ( req . nextUrl )
39- }
38+ const shouldCheckMaintenance =
39+ pathname === '/' ||
40+ pathname === '/login' ||
41+ pathname === '/verify' ||
42+ pathname === '/install' ||
43+ pathname === '/[username]' ||
44+ pathname . startsWith ( '/overlay' )
4045
41- try {
42- // Check whether the maintenance page should be shown
43- const isInMaintenanceMode = await get < boolean > ( 'isInMaintenanceMode' )
46+ if ( shouldCheckMaintenance ) {
47+ if ( ! process . env . EDGE_CONFIG ) {
48+ req . nextUrl . pathname = '/missing-edge-config'
49+ return NextResponse . rewrite ( req . nextUrl )
50+ }
4451
45- // If is in maintenance mode, point the url pathname to the maintenance page
46- if ( isInMaintenanceMode && process . env . VERCEL_ENV === 'production' ) {
47- // Check if the path starts with 'overlay' and return an empty page
48- if ( req . nextUrl . pathname . startsWith ( '/overlay' ) ) {
49- return new NextResponse ( null , { status : 200 } )
50- }
52+ try {
53+ // Check whether the maintenance page should be shown
54+ const isInMaintenanceMode = await get < boolean > ( 'isInMaintenanceMode' )
5155
52- req . nextUrl . pathname = '/maintenance'
53- return NextResponse . rewrite ( req . nextUrl )
56+ // If is in maintenance mode, point the url pathname to the maintenance page
57+ if ( isInMaintenanceMode && process . env . VERCEL_ENV === 'production' ) {
58+ // Check if the path starts with 'overlay' and return an empty page
59+ if ( pathname . startsWith ( '/overlay' ) ) {
60+ return new NextResponse ( null , { status : 200 } )
61+ }
62+
63+ req . nextUrl . pathname = '/maintenance'
64+ return NextResponse . rewrite ( req . nextUrl )
65+ }
66+ } catch ( error ) {
67+ captureException ( error )
68+ // show the default page if EDGE_CONFIG env var is missing,
69+ // but log the error to the console
70+ console . error ( error )
5471 }
55- } catch ( error ) {
56- captureException ( error )
57- // show the default page if EDGE_CONFIG env var is missing,
58- // but log the error to the console
59- console . error ( error )
6072 }
61- if ( req . nextUrl . pathname . startsWith ( '/dashboard' ) || req . nextUrl . pathname . endsWith ( '/overlay' ) ) {
73+
74+ if ( pathname . startsWith ( '/dashboard' ) || pathname . endsWith ( '/overlay' ) ) {
6275 // Use the token in the request directly to check role
6376 // withAuth already adds the token and user to the request
6477 const onlyChatterRole = await withAuth ( req , {
0 commit comments