rybbit/client/src/middleware.ts
Bill Yang 97c29dafac
Fix callbacks (#107)
* Fix callbacks

* fix layout

* Remove callbacks

* Disable redirects

* fix caddy

* test disable basepath

* wip

* test caddy fix

* remove basepath

* fml

* pray

* kms

* pray

* wip

* Test
2025-05-03 16:27:46 -07:00

69 lines
1.9 KiB
TypeScript

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export async function middleware(request: NextRequest) {
const url = request.nextUrl.clone();
const path = url.pathname;
// Skip redirects for static files (favicon.ico, images, etc.)
if (path.includes(".")) {
return NextResponse.next();
}
// Handle GitHub OAuth callback redirect
if (path === "/auth/callback/github" || path === "/auth/callback/google") {
const redirectUrl = new URL(
`/api${path}${request.nextUrl.search}`,
request.url
);
const response = NextResponse.redirect(redirectUrl);
response.headers.set("Cache-Control", "no-store, max-age=0");
return response;
}
// Check if we're on a site route without a specific page
// This matches exactly /{siteId} with nothing after it
const siteRoutePattern = /^\/([^/]+)$/;
const match = path.match(siteRoutePattern);
if (match) {
const siteId = match[1];
// Don't redirect certain built-in routes like 'login', 'signup', etc.
const excludedRoutes = [
"login",
"signup",
"settings",
"subscribe",
"auth",
"_next",
"api",
];
if (excludedRoutes.includes(siteId)) {
return NextResponse.next();
}
// Add cache control headers to make sure the redirect isn't cached
const response = NextResponse.redirect(
new URL(`/${siteId}/main`, request.url)
);
response.headers.set("Cache-Control", "no-store, max-age=0");
return response;
}
return NextResponse.next();
}
// Only run middleware on specific paths, exclude asset paths
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public files (images, etc.)
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\..*).*)",
],
};