-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send moved permanently redirects for /api/v0/
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
} | ||
} |