Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: remove geo #4509

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 35 additions & 36 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { geolocation } from '@vercel/functions'
import { getLogger } from 'lib/logger'
import { NextRequest, NextResponse } from 'next/server'
import { fetchProjectIdForHandle } from 'pages/api/juicebox/project/[projectHandle]'
Expand All @@ -16,31 +15,31 @@ const GEOFENCED_PROJECT_IDS: {
// { path: '/v2/p/64', projectId: 64, blockedCountries: ['AU'] }, // example
]

function geofenceCheck(projectId: number, request: NextRequest) {
const url = request.nextUrl

const country = geolocation(request).country
logger.info('🌎 Geofence check', {
country,
projectId,
geo: geolocation(request),
})

if (
country &&
GEOFENCED_PROJECT_IDS.find(
p => p.projectId === projectId,
)?.blockedCountries.includes(country)
) {
logger.info('Geofenced project', {
originalPathname: request.nextUrl.pathname,
newPathname: '/404',
})
url.pathname = '/404'

return url
}
}
// function geofenceCheck(projectId: number, request: NextRequest) {
// const url = request.nextUrl

// const country = geolocation(request).country
// logger.info('🌎 Geofence check', {
// country,
// projectId,
// geo: geolocation(request),
// })

// if (
// country &&
// GEOFENCED_PROJECT_IDS.find(
// p => p.projectId === projectId,
// )?.blockedCountries.includes(country)
// ) {
// logger.info('Geofenced project', {
// originalPathname: request.nextUrl.pathname,
// newPathname: '/404',
// })
// url.pathname = '/404'

// return url
// }
// }

export async function middleware(request: NextRequest) {
logger.info('middleware request', { pathname: request.nextUrl.pathname })
Expand All @@ -49,13 +48,13 @@ export async function middleware(request: NextRequest) {
return
}

const projectId = parseInt(request.nextUrl.pathname.split('/').reverse()[0])
const newUrl = geofenceCheck(projectId, request)
if (newUrl) {
return NextResponse.rewrite(newUrl)
}
// const projectId = parseInt(request.nextUrl.pathname.split('/').reverse()[0])
// const newUrl = geofenceCheck(projectId, request)
// if (newUrl) {
// return NextResponse.rewrite(newUrl)
// }

return
// return
}

// If request is for a handle id, add the search param with `isHandle`.
Expand Down Expand Up @@ -91,10 +90,10 @@ export async function middleware(request: NextRequest) {
return NextResponse.rewrite(url)
}

const geofenceRedirect = geofenceCheck(projectId, request)
if (geofenceRedirect) {
return NextResponse.rewrite(geofenceRedirect)
}
// const geofenceRedirect = geofenceCheck(projectId, request)
// if (geofenceRedirect) {
// return NextResponse.rewrite(geofenceRedirect)
// }

url.pathname = `/v2/p/${projectId}${trailingPath ? `/${trailingPath}` : ''}`

Expand Down
Loading