@@ -13,7 +13,7 @@ const SN_REFERRER_NONCE = 'sn_referrer_nonce'
13
13
const SN_REFEREE_LANDING = 'sn_referee_landing'
14
14
15
15
const TERRITORY_PATHS = [ '/~' , '/recent' , '/random' , '/top' , '/post' , '/edit' ]
16
- const NO_REWRITE_PATHS = [ '/api' , '/_next' , '/_error' , '/404' , '/500' , '/offline' , '/static' , '/items' ]
16
+ const NO_REWRITE_PATHS = [ '/api' , '/_next' , '/_error' , '/404' , '/500' , '/offline' , '/static' ]
17
17
18
18
// fetch custom domain mappings from our API, caching it for 5 minutes
19
19
const getDomainMappingsCache = cachedFetcher ( async function fetchDomainMappings ( ) {
@@ -67,54 +67,28 @@ export async function customDomainMiddleware (request, referrerResp) {
67
67
// remove the territory prefix from the path
68
68
const cleanPath = pathname . replace ( `/~${ domainInfo . subName } ` , '' ) || '/'
69
69
console . log ( 'Redirecting to clean path:' , cleanPath )
70
- return NextResponse . redirect ( new URL ( cleanPath + url . search , url . origin ) )
70
+ const redirectResp = NextResponse . redirect ( new URL ( cleanPath + url . search , url . origin ) )
71
+ return applyReferrerCookies ( redirectResp , referrerResp )
71
72
}
72
73
73
74
// if coming from main domain, handle auth automatically
74
75
if ( referer && referer === mainDomain ) {
75
76
const authResp = customDomainAuthMiddleware ( request , url )
76
77
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
- )
89
- }
90
- return authResp
78
+ return applyReferrerCookies ( authResp , referrerResp )
91
79
}
92
80
}
93
81
94
82
const internalUrl = new URL ( url )
95
-
96
83
// rewrite to the territory path if we're at the root
97
84
if ( pathname === '/' || TERRITORY_PATHS . some ( p => pathname . startsWith ( p ) ) ) {
98
85
internalUrl . pathname = `/~${ domainInfo . subName } ${ pathname === '/' ? '' : pathname } `
99
86
}
100
87
console . log ( 'Rewrite to:' , internalUrl . pathname )
101
-
102
88
// rewrite to the territory path
103
- const redirectResp = NextResponse . rewrite ( internalUrl )
104
- // TODO: preserve referrer cookies in a DRY way
105
- for ( const cookie of referrerResp . cookies . getAll ( ) ) {
106
- redirectResp . cookies . set (
107
- cookie . name ,
108
- cookie . value ,
109
- {
110
- maxAge : cookie . maxAge ,
111
- expires : cookie . expires ,
112
- path : cookie . path
113
- }
114
- )
115
- }
116
-
117
- return redirectResp
89
+ const resp = NextResponse . rewrite ( internalUrl )
90
+ // copy referrer cookies to the rewritten response
91
+ return applyReferrerCookies ( resp , referrerResp )
118
92
}
119
93
120
94
// TODO: dirty of previous iterations, refactor
@@ -176,6 +150,22 @@ function getContentReferrer (request, url) {
176
150
}
177
151
}
178
152
153
+ function applyReferrerCookies ( response , referrer ) {
154
+ for ( const cookie of referrer . cookies . getAll ( ) ) {
155
+ response . cookies . set (
156
+ cookie . name ,
157
+ cookie . value ,
158
+ {
159
+ maxAge : cookie . maxAge ,
160
+ expires : cookie . expires ,
161
+ path : cookie . path
162
+ }
163
+ )
164
+ }
165
+ console . log ( 'response.cookies' , response . cookies )
166
+ return response
167
+ }
168
+
179
169
// we store the referrers in cookies for a future signup event
180
170
// we pass the referrers in the request headers so we can use them in referral rewards for logged in stackers
181
171
function referrerMiddleware ( request ) {
0 commit comments