-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
50 lines (43 loc) · 1.48 KB
/
middleware.ts
File metadata and controls
50 lines (43 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// /middleware.ts
import { type NextRequest, NextResponse } from "next/server";
import { updateSession } from "@/utils/supabase/middleware";
import { createClient } from '@/utils/supabase/server';
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Handle case-insensitive project URLs
if (pathname.startsWith('/projects/')) {
const slug = pathname.split('/projects/')[1];
// If slug contains uppercase letters
if (/[A-Z]/.test(slug)) {
const lowercaseSlug = slug.toLowerCase();
// Check if lowercase version exists in database
const supabase = createClient();
const { data } = await supabase
.from('listings')
.select('slug')
.eq('slug', lowercaseSlug)
.single();
if (data) {
// Redirect to lowercase version if it exists
return NextResponse.redirect(
new URL(`/projects/${lowercaseSlug}`, request.url)
);
}
}
}
// Continue with existing session management
return await updateSession(request);
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - images - .svg, .png, .jpg, .jpeg, .gif, .webp
* Feel free to modify this pattern to include more paths.
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};