@@ -11,20 +11,14 @@ const SN_REFERRER_NONCE = 'sn_referrer_nonce'
11
11
// key for referred pages
12
12
const SN_REFEREE_LANDING = 'sn_referee_landing'
13
13
14
- const TERRITORY_PATHS = [
15
- '/' ,
16
- '/~' ,
17
- '/recent' ,
18
- '/random' ,
19
- '/top' ,
20
- '/items'
21
- ]
14
+ const TERRITORY_PATHS = [ '/~' , '/recent' , '/random' , '/top' , '/post' , '/edit' ]
15
+ const NO_REWRITE_PATHS = [ '/api' , '/_next' , '/_error' , '/404' , '/500' , '/offline' , '/static' , '/items' ]
22
16
23
17
function getDomainMapping ( ) {
24
18
// placeholder for cachedFetcher
25
19
return {
26
20
'forum.pizza.com' : { subName : 'pizza' }
27
- // placeholder
21
+ // placeholder for other domains
28
22
}
29
23
}
30
24
@@ -33,7 +27,9 @@ export function customDomainMiddleware (request, referrerResp) {
33
27
const referer = request . headers . get ( 'referer' )
34
28
const url = request . nextUrl . clone ( )
35
29
const pathname = url . pathname
36
- const mainDomain = process . env . NEXT_PUBLIC_URL
30
+ const mainDomain = process . env . NEXT_PUBLIC_URL + '/'
31
+ console . log ( 'host' , host )
32
+ console . log ( 'mainDomain' , mainDomain )
37
33
38
34
console . log ( 'referer' , referer )
39
35
@@ -43,31 +39,25 @@ export function customDomainMiddleware (request, referrerResp) {
43
39
return NextResponse . redirect ( new URL ( pathname , mainDomain ) )
44
40
}
45
41
46
- // For territory paths, handle them directly on the custom domain
47
- if ( TERRITORY_PATHS . includes ( pathname ) ) {
48
- // Internally rewrite the request to the territory path without changing the URL
49
- const internalUrl = new URL ( url )
42
+ if ( NO_REWRITE_PATHS . some ( p => pathname . startsWith ( p ) ) || pathname . includes ( '.' ) ) {
43
+ return NextResponse . next ( )
44
+ }
50
45
51
- // If we're at the root path, internally rewrite to the territory path
52
- if ( pathname === '/' || pathname === '/~' ) {
53
- internalUrl . pathname = `/~${ domainInfo . subName } `
54
- console . log ( 'Internal rewrite to:' , internalUrl . pathname )
46
+ console . log ( 'pathname' , pathname )
47
+ console . log ( 'query' , url . searchParams )
55
48
56
- // NextResponse.rewrite() keeps the URL the same for the user
57
- // but internally fetches from the rewritten path
58
- return NextResponse . rewrite ( internalUrl )
59
- }
60
-
61
- // For other territory paths like /recent, /top, etc.
62
- // We need to rewrite them to the territory-specific versions
63
- if ( pathname === '/recent' || pathname === '/top' || pathname === '/random' || pathname === '/items' ) {
64
- internalUrl . pathname = `/~${ domainInfo . subName } ${ pathname } `
65
- console . log ( 'Internal rewrite to:' , internalUrl . pathname )
66
- return NextResponse . rewrite ( internalUrl )
67
- }
49
+ // if the url contains the territory path, remove it
50
+ if ( pathname . startsWith ( `/~${ domainInfo . subName } ` ) ) {
51
+ // remove the territory prefix from the path
52
+ const cleanPath = pathname . replace ( `/~${ domainInfo . subName } ` , '' ) || '/'
53
+ console . log ( 'Redirecting to clean path:' , cleanPath )
54
+ return NextResponse . redirect ( new URL ( cleanPath + url . search , url . origin ) )
55
+ }
68
56
69
- // Handle auth if needed
70
- if ( ! referer || referer !== mainDomain ) {
57
+ // if territory path, retain custom domain
58
+ if ( pathname === '/' || TERRITORY_PATHS . some ( p => pathname . startsWith ( p ) ) ) {
59
+ // if coming from main domain, handle auth automatically
60
+ if ( referer && referer === mainDomain ) {
71
61
const authResp = customDomainAuthMiddleware ( request , url )
72
62
if ( authResp && authResp . status !== 200 ) {
73
63
// copy referrer cookies to auth redirect
@@ -77,7 +67,15 @@ export function customDomainMiddleware (request, referrerResp) {
77
67
return authResp
78
68
}
79
69
}
80
- return referrerResp
70
+
71
+ const internalUrl = new URL ( url )
72
+
73
+ // rewrite to the territory path if we're at the root
74
+ internalUrl . pathname = `/~${ domainInfo . subName } ${ pathname === '/' ? '' : pathname } `
75
+ console . log ( 'Rewrite to:' , internalUrl . pathname )
76
+
77
+ // rewrite to the territory path
78
+ return NextResponse . rewrite ( internalUrl )
81
79
}
82
80
83
81
// redirect to main domain for non-territory paths
@@ -93,7 +91,7 @@ export function customDomainMiddleware (request, referrerResp) {
93
91
}
94
92
95
93
// TODO: dirty of previous iterations, refactor
96
- // Not safe, tokens are visible in the URL
94
+ // UNSAFE UNSAFE UNSAFE tokens are visible in the URL
97
95
export function customDomainAuthMiddleware ( request , url ) {
98
96
const pathname = url . pathname
99
97
const host = request . headers . get ( 'host' )
@@ -114,7 +112,6 @@ export function customDomainAuthMiddleware (request, url) {
114
112
const response = NextResponse . next ( )
115
113
116
114
if ( ! hasSession && isCustomDomain ) {
117
- // Use the original request's host and protocol for the redirect URL
118
115
// TODO: original request url points to localhost, this is a workaround atm
119
116
const protocol = secure ? 'https' : 'http'
120
117
const originalDomain = `${ protocol } ://${ host } `
@@ -206,22 +203,7 @@ function referrerMiddleware (request) {
206
203
return response
207
204
}
208
205
209
- export function middleware ( request ) {
210
- const host = request . headers . get ( 'host' )
211
- const isCustomDomain = host !== process . env . NEXT_PUBLIC_URL . replace ( / ^ h t t p s ? : \/ \/ / , '' )
212
-
213
- // First run referrer middleware to capture referrer data
214
- const referrerResp = referrerMiddleware ( request )
215
-
216
- // If we're on a custom domain, handle that next
217
- if ( isCustomDomain ) {
218
- return customDomainMiddleware ( request , referrerResp )
219
- }
220
-
221
- const resp = referrerResp
222
-
223
- // TODO: This doesn't run for custom domains, need to support it
224
-
206
+ export function applySecurityHeaders ( resp ) {
225
207
const isDev = process . env . NODE_ENV === 'development'
226
208
227
209
const nonce = Buffer . from ( crypto . randomUUID ( ) ) . toString ( 'base64' )
@@ -268,6 +250,22 @@ export function middleware (request) {
268
250
return resp
269
251
}
270
252
253
+ export function middleware ( request ) {
254
+ const host = request . headers . get ( 'host' )
255
+ const isCustomDomain = host !== process . env . NEXT_PUBLIC_URL . replace ( / ^ h t t p s ? : \/ \/ / , '' )
256
+
257
+ // First run referrer middleware to capture referrer data
258
+ const referrerResp = referrerMiddleware ( request )
259
+
260
+ // If we're on a custom domain, handle that next
261
+ if ( isCustomDomain ) {
262
+ const customDomainResp = customDomainMiddleware ( request , referrerResp )
263
+ return applySecurityHeaders ( customDomainResp )
264
+ }
265
+
266
+ return applySecurityHeaders ( referrerResp )
267
+ }
268
+
271
269
export const config = {
272
270
matcher : [
273
271
// NextJS recommends to not add the CSP header to prefetches and static assets
0 commit comments