Skip to content

Commit

Permalink
Send moved permanently redirects for /api/v0/
Browse files Browse the repository at this point in the history
  • Loading branch information
sipec committed Oct 14, 2024
1 parent 5a80021 commit 337f729
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions web/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { NextResponse, type NextRequest } from 'next/server'
import { PROD_CONFIG } from 'common/envs/prod'

export async function middleware(req: NextRequest) {
const path = req.nextUrl.pathname.replace('/api/', '')

if (pathsToSkip.includes(path)) {
return NextResponse.next()
}

return new Response('Permanent Redirect', {
status: 308,
headers: {
location: getProxiedRequestUrl(req, path),
},
})
}

export const config = {
matcher: ['/api/v0/:path*'],
}

const pathsToSkip = ['v0/deployment-id', 'v0/revalidate']

function getProxiedRequestUrl(req: NextRequest, path: string) {
const baseUrl = getApiUrl(path)
const [_prefix, qs] = req.url!.split('?', 2)
if (qs) {
return baseUrl + '?' + qs
} else {
return baseUrl
}
}

// copied from common/src/utils/api. TODO the right thing
function getApiUrl(path: string) {
if (process.env.NEXT_PUBLIC_API_URL) {
return `http://${process.env.NEXT_PUBLIC_API_URL}/${path}`
} else {
const { apiEndpoint } = PROD_CONFIG
return `https://${apiEndpoint}/${path}`
}
}

0 comments on commit 337f729

Please sign in to comment.