@@ -70,45 +70,38 @@ export async function customDomainMiddleware (request, referrerResp) {
70
70
return NextResponse . redirect ( new URL ( cleanPath + url . search , url . origin ) )
71
71
}
72
72
73
- // if territory path, retain custom domain
74
- if ( pathname === '/' || TERRITORY_PATHS . some ( p => pathname . startsWith ( p ) ) ) {
75
- // if coming from main domain, handle auth automatically
76
- if ( referer && referer === mainDomain ) {
77
- const authResp = customDomainAuthMiddleware ( request , url )
78
- if ( authResp && authResp . status !== 200 ) {
79
- // copy referrer cookies to auth redirect
80
- console . log ( 'referrerResp' , referrerResp )
81
- for ( const cookie of referrerResp . cookies . getAll ( ) ) {
82
- authResp . cookies . set (
83
- cookie . name ,
84
- cookie . value ,
85
- {
86
- maxAge : cookie . maxAge ,
87
- expires : cookie . expires ,
88
- path : cookie . path
89
- }
90
- )
91
- }
92
- return authResp
73
+ // if coming from main domain, handle auth automatically
74
+ if ( referer && referer === mainDomain ) {
75
+ const authResp = customDomainAuthMiddleware ( request , url )
76
+ if ( authResp && authResp . status !== 200 ) {
77
+ // copy referrer cookies to auth redirect
78
+ console . log ( 'referrerResp' , referrerResp )
79
+ for ( const cookie of referrerResp . cookies . getAll ( ) ) {
80
+ authResp . cookies . set (
81
+ cookie . name ,
82
+ cookie . value ,
83
+ {
84
+ maxAge : cookie . maxAge ,
85
+ expires : cookie . expires ,
86
+ path : cookie . path
87
+ }
88
+ )
93
89
}
90
+ return authResp
94
91
}
92
+ }
95
93
96
- // TODO: preserve referrer cookies in a DRY way
97
-
98
- const internalUrl = new URL ( url )
94
+ const internalUrl = new URL ( url )
99
95
100
- // rewrite to the territory path if we're at the root
96
+ // rewrite to the territory path if we're at the root
97
+ if ( pathname === '/' || TERRITORY_PATHS . some ( p => pathname . startsWith ( p ) ) ) {
101
98
internalUrl . pathname = `/~${ domainInfo . subName } ${ pathname === '/' ? '' : pathname } `
102
- console . log ( 'Rewrite to:' , internalUrl . pathname )
103
-
104
- // rewrite to the territory path
105
- return NextResponse . rewrite ( internalUrl )
106
99
}
100
+ console . log ( 'Rewrite to:' , internalUrl . pathname )
107
101
108
- // redirect to main domain for non-territory paths
109
- // create redirect response but preserve referrer cookies
110
- const redirectResp = NextResponse . redirect ( new URL ( pathname , mainDomain ) )
111
-
102
+ // rewrite to the territory path
103
+ const redirectResp = NextResponse . rewrite ( internalUrl )
104
+ // TODO: preserve referrer cookies in a DRY way
112
105
for ( const cookie of referrerResp . cookies . getAll ( ) ) {
113
106
redirectResp . cookies . set (
114
107
cookie . name ,
0 commit comments