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

Refactor server rewrites to accept function API URL as parameter #3632

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
13 changes: 8 additions & 5 deletions server/rewrites.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'

const rewriteRules = () => [
const rewriteRules = (functionsApiUrl: string) => [
{
source: '/api/triggers/:path*',
destination: `${process.env.FUNCTIONS_API_URL}/api/triggers/:path*`,
destination: `${functionsApiUrl}/api/triggers/:path*`,
},
{
source: '/api/triggers',
destination: `${process.env.FUNCTIONS_API_URL}/api/triggers`,
destination: `${functionsApiUrl}/api/triggers`,
},
{
source: '/api/migrations',
destination: `${process.env.FUNCTIONS_API_URL}/api/migrations`,
destination: `${functionsApiUrl}/api/migrations`,
},
// ... other rules
]
Expand Down Expand Up @@ -46,7 +46,10 @@ export function handleRewrite(request: NextRequest): NextResponse | null {
if (!request.nextUrl.pathname.startsWith('/api')) {
return null
}
const rules = rewriteRules()
const functionApiUrlCookie = request.cookies.get('functionApiUrl')

const functionApiUrl = functionApiUrlCookie?.value ?? process.env.FUNCTIONS_API_URL ?? ''
const rules = rewriteRules(functionApiUrl)
for (const rule of rules) {
const { regex, names } = parsePattern(rule.source)
const matches = regex.exec(request.nextUrl.pathname)
Expand Down
Loading