-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
54 lines (43 loc) · 1.54 KB
/
middleware.ts
File metadata and controls
54 lines (43 loc) · 1.54 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// Extract language from URL path (e.g., /uk-ua -> uk, /en-us -> en)
function getLanguageFromPath(pathname: string): string {
const localeMatch = pathname.match(/^\/([a-z]{2})-[a-z]{2}/)
return localeMatch ? localeMatch[1] : 'en'
}
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname
// Redirect specific URLs to homepage
const redirectUrls = [
'/tag/etf',
'/en-us/the-role-of-technology-for-stocks-in-shaping-the-u-s-stock-market',
'/en-us/tag/savings',
'/en-us/home'
]
if (redirectUrls.includes(pathname)) {
return NextResponse.redirect(new URL('/', request.url))
}
// Clone the response
const response = NextResponse.next()
// Allow indexing by search engines
// Remove noindex directives and allow caching
response.headers.set('X-Robots-Tag', 'index, follow')
// Set reasonable cache control (allow caching)
response.headers.set('Cache-Control', 'public, max-age=3600, must-revalidate')
// Determine language from URL prefix and add to header
const lang = getLanguageFromPath(pathname)
response.headers.set('x-lang', lang)
return response
}
// Apply middleware to all routes
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)
*/
'/((?!_next/static|_next/image|favicon.ico).*)',
],
}