Skip to content

Commit 04df5d5

Browse files
committed
consider referrer cookies
1 parent 5072aa1 commit 04df5d5

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

middleware.js

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const SN_REFERRER_NONCE = 'sn_referrer_nonce'
1313
const SN_REFEREE_LANDING = 'sn_referee_landing'
1414

1515
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']
1717

1818
// fetch custom domain mappings from our API, caching it for 5 minutes
1919
const getDomainMappingsCache = cachedFetcher(async function fetchDomainMappings () {
@@ -67,54 +67,28 @@ export async function customDomainMiddleware (request, referrerResp) {
6767
// remove the territory prefix from the path
6868
const cleanPath = pathname.replace(`/~${domainInfo.subName}`, '') || '/'
6969
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)
7172
}
7273

7374
// if coming from main domain, handle auth automatically
7475
if (referer && referer === mainDomain) {
7576
const authResp = customDomainAuthMiddleware(request, url)
7677
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)
9179
}
9280
}
9381

9482
const internalUrl = new URL(url)
95-
9683
// rewrite to the territory path if we're at the root
9784
if (pathname === '/' || TERRITORY_PATHS.some(p => pathname.startsWith(p))) {
9885
internalUrl.pathname = `/~${domainInfo.subName}${pathname === '/' ? '' : pathname}`
9986
}
10087
console.log('Rewrite to:', internalUrl.pathname)
101-
10288
// 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)
11892
}
11993

12094
// TODO: dirty of previous iterations, refactor
@@ -176,6 +150,22 @@ function getContentReferrer (request, url) {
176150
}
177151
}
178152

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+
179169
// we store the referrers in cookies for a future signup event
180170
// we pass the referrers in the request headers so we can use them in referral rewards for logged in stackers
181171
function referrerMiddleware (request) {

0 commit comments

Comments
 (0)