-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
93 lines (79 loc) · 2.81 KB
/
Copy pathmiddleware.ts
File metadata and controls
93 lines (79 loc) · 2.81 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { getAllLocationData } from "@/lib/location-utils";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
const PUBLIC_FILE = /\.(.*)$/;
// Cache location data to avoid repeated computation
let locationSlugs: Set<string> | null = null;
function getLocationSlugs(): Set<string> {
if (!locationSlugs) {
const locationData = getAllLocationData();
locationSlugs = new Set();
locationData.forEach(({ stateSlug, cities }) => {
locationSlugs!.add(`lokasi/${stateSlug}`);
cities.forEach((city) => {
const citySlug = city
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^a-z0-9\-]/g, "")
.replace(/-+/g, "-")
.replace(/^-|-$/g, "");
locationSlugs!.add(`lokasi/${stateSlug}/${citySlug}`);
});
});
}
return locationSlugs;
}
export function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const hostname = request.headers.get("host") || "";
// Redirect www to non-www
if (hostname.startsWith("www.")) {
const newUrl = new URL(request.url);
newUrl.hostname = hostname.replace("www.", "");
return NextResponse.redirect(newUrl, {
status: 301, // Permanent redirect
});
}
// Enhanced URL pattern matching for redirects
// Handles both /undefined/ and /dialysis-center/ legacy paths
if (
path.match(/^\/undefined\/(.+)$/) ||
path.match(/^\/dialysis-center\/(.+)$/)
) {
const slug = path.replace(/^\/undefined\/|^\/dialysis-center\//, "");
return NextResponse.redirect(new URL(`/${slug}`, request.url), {
status: 301, // Permanent redirect
});
}
// Route conflict resolution: Check if path matches location routes
// This ensures location routes take precedence over center slugs
const pathWithoutLeadingSlash = path.substring(1);
if (pathWithoutLeadingSlash) {
const locationSlugs = getLocationSlugs();
// If it's a valid location route, let it proceed
if (locationSlugs.has(pathWithoutLeadingSlash)) {
return NextResponse.next();
}
}
// Check if the path starts with /_next or is a public file
if (path.startsWith("/_next") || PUBLIC_FILE.test(path)) {
const response = NextResponse.next();
// Add X-Robots-Tag header
response.headers.set("X-Robots-Tag", "noindex, nofollow");
return response;
}
return NextResponse.next();
}
// Configure matcher for all paths except static files
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 folder files
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico|js|css|woff|woff2|ttf|otf)$).*)",
],
};