-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
41 lines (34 loc) · 1.14 KB
/
Copy pathmiddleware.js
File metadata and controls
41 lines (34 loc) · 1.14 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
import { NextResponse } from 'next/server';
const locales = ['en', 'ar'];
const defaultLocale = 'en';
export function middleware(request) {
const pathname = request.nextUrl.pathname;
// Skip static files, images, and PostHog ingest paths
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/api') ||
pathname.startsWith('/ingest') || // Add this line to exclude PostHog paths
pathname.includes('.') // This catches files with extensions like .png, .jpg, .ico, etc.
) {
return NextResponse.next();
}
// Check if there is any supported locale in the pathname
const pathnameIsMissingLocale = locales.every(
(locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
);
// Redirect if there is no locale
if (pathnameIsMissingLocale) {
const locale = defaultLocale;
// e.g. incoming request is /products
// The new URL is now /en/products
return NextResponse.redirect(
new URL(`/${locale}${pathname}`, request.url)
);
}
}
export const config = {
matcher: [
// Skip all internal paths (_next) and files with extensions
'/((?!_next|ingest|api|.*\\.).*)',
],
};