Vercel/proxy.ts
Strtus 66fa66d3d3 Skip auth middleware for public proxy routes
Route public paths through proxy without invoking auth so category pages no longer fail with runtime auth errors on EdgeOne.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-19 01:11:42 +08:00

66 lines
1.9 KiB
TypeScript

import { auth } from './src/auth/server';
import { NextRequest, NextResponse } from 'next/server';
import type { NextApiRequest, NextApiResponse } from 'next';
import {
PATH_ADMIN,
PATH_ADMIN_PHOTOS,
PATH_OG,
PATH_OG_SAMPLE,
PREFIX_PHOTO,
PREFIX_TAG,
isPathProtected,
} from './src/app/path';
export function proxy(req: NextRequest, res:NextResponse) {
const pathname = req.nextUrl.pathname;
if (pathname === PATH_ADMIN) {
return NextResponse.redirect(new URL(PATH_ADMIN_PHOTOS, req.url));
} else if (pathname === PATH_OG) {
return NextResponse.redirect(new URL(PATH_OG_SAMPLE, req.url));
} else if (/^\/photos\/(.)+$/.test(pathname)) {
// Accept /photos/* paths, but serve /p/*
const matches = pathname.match(/^\/photos\/(.+)$/);
return NextResponse.rewrite(new URL(
`${PREFIX_PHOTO}/${matches?.[1]}`,
req.url,
));
} else if (/^\/t\/(.)+$/.test(pathname)) {
// Accept /t/* paths, but serve /tag/*
const matches = pathname.match(/^\/t\/(.+)$/);
return NextResponse.rewrite(new URL(
`${PREFIX_TAG}/${matches?.[1]}`,
req.url,
));
}
// Avoid invoking auth middleware on public routes so
// downstream auth runtime differences don't 500 category pages.
if (!isPathProtected(pathname)) {
return NextResponse.next();
}
return auth(
req as unknown as NextApiRequest,
res as unknown as NextApiResponse,
);
}
export const config = {
// Excludes:
// - /api + /api/auth*
// - /_next/static*
// - /_next/image*
// - /favicon.ico + /favicons/*
// - /grid
// - /full
// - /about
// - / (root)
// - /home-image
// - /template-image
// - /template-image-tight
// - /template-url
// eslint-disable-next-line max-len
matcher: ['/((?!api(?:/|$)|_next/static|_next/image|favicon.ico$|favicons/|grid$|full$|about$|home-image$|template-image$|template-image-tight$|template-url$|$).*)'],
};