Files
sent-shop/middleware.ts
2026-01-19 20:21:14 +01:00

40 lines
1.3 KiB
TypeScript

import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
// Middleware samo prosljeđuje zahtjeve
export function middleware(request: NextRequest) {
// Get the pathname
const { pathname } = request.nextUrl;
// Get the locale from cookie or default to 'hr'
const locale = request.cookies.get('NEXT_LOCALE')?.value || 'hr';
// Create a new URL based on the request
const newUrl = new URL(request.nextUrl);
// Check if we're on an English page
const isEnglishPage = pathname.startsWith('/en');
// Check if we're requesting the English version
const wantsEnglish = locale === 'en';
// If user wants English but we're not on an English page, redirect to English version
if (wantsEnglish && !isEnglishPage) {
newUrl.pathname = `/en${pathname}`;
return NextResponse.redirect(newUrl);
}
// If user wants Croatian but we're on an English page, redirect to Croatian version
if (!wantsEnglish && isEnglishPage) {
newUrl.pathname = pathname.replace(/^\/en/, '') || '/';
return NextResponse.redirect(newUrl);
}
return NextResponse.next();
}
// Za svaki slučaj, definiraj matcher
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico|assets).*)',
],
};