1
1
import { NextResponse , URLPattern } from 'next/server'
2
+ import { cachedFetcher } from '@/lib/fetch'
2
3
const referrerPattern = new URLPattern ( { pathname : ':pathname(*)/r/:referrer([\\w_]+)' } )
3
4
const itemPattern = new URLPattern ( { pathname : '/items/:id(\\d+){/:other(\\w+)}?' } )
4
5
const profilePattern = new URLPattern ( { pathname : '/:name([\\w_]+){/:type(\\w+)}?' } )
@@ -14,15 +15,29 @@ const SN_REFEREE_LANDING = 'sn_referee_landing'
14
15
const TERRITORY_PATHS = [ '/~' , '/recent' , '/random' , '/top' , '/post' , '/edit' ]
15
16
const NO_REWRITE_PATHS = [ '/api' , '/_next' , '/_error' , '/404' , '/500' , '/offline' , '/static' , '/items' ]
16
17
17
- function getDomainMapping ( ) {
18
- // placeholder for cachedFetcher
19
- return {
20
- 'forum.pizza.com' : { subName : 'pizza' }
21
- // placeholder for other domains
18
+ // fetch custom domain mappings from our API, caching it for 5 minutes
19
+ const getDomainMappingsCache = cachedFetcher ( async function fetchDomainMappings ( ) {
20
+ const url = `${ process . env . NEXT_PUBLIC_URL } /api/domains/map`
21
+ try {
22
+ const response = await fetch ( url )
23
+ if ( ! response . ok ) {
24
+ console . error ( `Cannot fetch domain mappings: ${ response . status } ${ response . statusText } ` )
25
+ return null
26
+ }
27
+
28
+ const data = await response . json ( )
29
+ return Object . keys ( data ) . length > 0 ? data : null
30
+ } catch ( error ) {
31
+ console . error ( 'Cannot fetch domain mappings:' , error )
32
+ return null
22
33
}
23
- }
34
+ } , {
35
+ cacheExpiry : 300000 , // 5 minutes cache
36
+ forceRefreshThreshold : 600000 , // 10 minutes before force refresh
37
+ keyGenerator : ( ) => 'domain_mappings'
38
+ } )
24
39
25
- export function customDomainMiddleware ( request , referrerResp ) {
40
+ export async function customDomainMiddleware ( request , referrerResp ) {
26
41
const host = request . headers . get ( 'host' )
27
42
const referer = request . headers . get ( 'referer' )
28
43
const url = request . nextUrl . clone ( )
@@ -33,8 +48,8 @@ export function customDomainMiddleware (request, referrerResp) {
33
48
34
49
console . log ( 'referer' , referer )
35
50
36
- const domainMapping = getDomainMapping ( ) // placeholder
37
- const domainInfo = domainMapping [ host . toLowerCase ( ) ]
51
+ const domainMapping = await getDomainMappingsCache ( )
52
+ const domainInfo = domainMapping ?. [ host . toLowerCase ( ) ]
38
53
if ( ! domainInfo ) {
39
54
return NextResponse . redirect ( new URL ( pathname , mainDomain ) )
40
55
}
@@ -250,7 +265,7 @@ export function applySecurityHeaders (resp) {
250
265
return resp
251
266
}
252
267
253
- export function middleware ( request ) {
268
+ export async function middleware ( request ) {
254
269
const host = request . headers . get ( 'host' )
255
270
const isCustomDomain = host !== process . env . NEXT_PUBLIC_URL . replace ( / ^ h t t p s ? : \/ \/ / , '' )
256
271
@@ -259,7 +274,7 @@ export function middleware (request) {
259
274
260
275
// If we're on a custom domain, handle that next
261
276
if ( isCustomDomain ) {
262
- const customDomainResp = customDomainMiddleware ( request , referrerResp )
277
+ const customDomainResp = await customDomainMiddleware ( request , referrerResp )
263
278
return applySecurityHeaders ( customDomainResp )
264
279
}
265
280
0 commit comments