Next.js Discord

Discord Forum

Debugging proxy.ts

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
What's the "ideal" way to debug proxy.ts? I have the file at the root of the app (same level as app), but as far as I've been able to determine it's doing literally nothing. I'm running Next 16.1.1 and the content of the file is as follows:

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function proxy(request: NextRequest) {
    const host = request.headers.get('host') ?? request.nextUrl.host;
    const domains = host.split('.');

    if (domains.length !== 3) return;

    const subdomain = domains[0];
    const rewriteTo = new URL(
        `/${subdomain}/${request.nextUrl.pathname}`.replaceAll('//', '/'),
        request.url,
    );

    new URL(request.url).searchParams
        .entries()
        .forEach(([key, value]) => rewriteTo.searchParams.set(key, value));

    return NextResponse.rewrite(rewriteTo);
}

export const config = {
    matcher: [
        // Exclude API routes, static files, image optimizations, and .png files
        "/((?!api|_next/static|_next/image|.*\\.png$).*)",
    ],
};
Answered by Sun bear
Never mind, needed to be tsx
View full answer

4 Replies

Sun bearOP
Never mind, needed to be tsx
Answer
Greater Shearwater
I didn't know the proxy file could be saved as .tsx.
@Australian Freshwater Crocodile hm.. I don't see the part that requires it to be tsx instead of ts.
Sun bearOP
it does... if you've modified pageExtensions and aren't including .ts I only had .tsx. And I currently have it saved as .tsx and it's working fine