57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import type { NextRequest } from 'next/server'
|
|
import { NextResponse } from 'next/server'
|
|
|
|
const APEX_HOST = 'greenlenspro.com'
|
|
const WWW_HOST = `www.${APEX_HOST}`
|
|
const ENGLISH_PATHS = new Set([
|
|
'/best-plant-identification-app',
|
|
'/plant-identifier-app',
|
|
'/plant-scanner',
|
|
'/flower-scanner',
|
|
'/houseplant-identifier',
|
|
'/succulent-identifier',
|
|
'/identify-plant-photo',
|
|
'/plant-disease-identifier',
|
|
'/plant-health-app',
|
|
'/plant-care-app',
|
|
])
|
|
|
|
function getLangForPath(pathname: string) {
|
|
if (pathname === '/es' || pathname.startsWith('/es/')) return 'es'
|
|
if (pathname === '/vs/google-lens') return 'de'
|
|
if (ENGLISH_PATHS.has(pathname) || pathname.startsWith('/vs/')) return 'en'
|
|
return 'de'
|
|
}
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const host = request.headers.get('host')
|
|
const forwardedProto = request.headers.get('x-forwarded-proto')
|
|
const requestHeaders = new Headers(request.headers)
|
|
requestHeaders.set('x-greenlens-lang', getLangForPath(request.nextUrl.pathname))
|
|
|
|
if (!host || (host !== APEX_HOST && host !== WWW_HOST)) {
|
|
return NextResponse.next({ request: { headers: requestHeaders } })
|
|
}
|
|
|
|
const url = request.nextUrl.clone()
|
|
let shouldRedirect = false
|
|
|
|
if (host === WWW_HOST) {
|
|
url.host = APEX_HOST
|
|
shouldRedirect = true
|
|
}
|
|
|
|
if (forwardedProto === 'http' || url.protocol === 'http:') {
|
|
url.protocol = 'https:'
|
|
shouldRedirect = true
|
|
}
|
|
|
|
return shouldRedirect
|
|
? NextResponse.redirect(url, 308)
|
|
: NextResponse.next({ request: { headers: requestHeaders } })
|
|
}
|
|
|
|
export const config = {
|
|
matcher: '/:path*',
|
|
}
|