fix: simplify middleware, remove broken fetch tracking
This commit is contained in:
parent
a200fbfc7d
commit
ce66e72006
|
|
@ -4,12 +4,8 @@ import type { NextRequest } from 'next/server';
|
||||||
// Routes only accessible when not authenticated
|
// Routes only accessible when not authenticated
|
||||||
const authRoutes = ['/login', '/register'];
|
const authRoutes = ['/login', '/register'];
|
||||||
|
|
||||||
// Routes à ne pas tracker
|
|
||||||
const excludedPaths = ['/api/track', '/api/metrics', '/_next', '/favicon.ico'];
|
|
||||||
|
|
||||||
export function middleware(request: NextRequest) {
|
export function middleware(request: NextRequest) {
|
||||||
const { pathname } = request.nextUrl;
|
const { pathname } = request.nextUrl;
|
||||||
const startTime = Date.now();
|
|
||||||
|
|
||||||
// Get token from cookies or headers
|
// Get token from cookies or headers
|
||||||
const token = request.cookies.get('auth_token')?.value ||
|
const token = request.cookies.get('auth_token')?.value ||
|
||||||
|
|
@ -18,47 +14,12 @@ export function middleware(request: NextRequest) {
|
||||||
// Check if route is auth route
|
// Check if route is auth route
|
||||||
const isAuthRoute = authRoutes.some(route => pathname.startsWith(route));
|
const isAuthRoute = authRoutes.some(route => pathname.startsWith(route));
|
||||||
|
|
||||||
// Préparer la réponse
|
|
||||||
let response: NextResponse;
|
|
||||||
|
|
||||||
// If accessing auth routes with token in cookies, redirect to home
|
// If accessing auth routes with token in cookies, redirect to home
|
||||||
if (isAuthRoute && token) {
|
if (isAuthRoute && token) {
|
||||||
response = NextResponse.redirect(new URL('/', request.url));
|
return NextResponse.redirect(new URL('/', request.url));
|
||||||
} else {
|
|
||||||
response = NextResponse.next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tracker les métriques HTTP (fire and forget)
|
return NextResponse.next();
|
||||||
const shouldTrack = !excludedPaths.some(path => pathname.startsWith(path));
|
|
||||||
|
|
||||||
if (shouldTrack) {
|
|
||||||
const durationMs = Date.now() - startTime;
|
|
||||||
const statusCode = response.status || 200;
|
|
||||||
|
|
||||||
// Envoyer les métriques de manière asynchrone (non bloquant)
|
|
||||||
const trackUrl = new URL('/api/track', request.url);
|
|
||||||
|
|
||||||
// Utiliser waitUntil pour ne pas bloquer la réponse
|
|
||||||
// Note: waitUntil n'est pas disponible dans tous les environnements
|
|
||||||
try {
|
|
||||||
fetch(trackUrl.toString(), {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
method: request.method,
|
|
||||||
path: pathname,
|
|
||||||
statusCode,
|
|
||||||
durationMs,
|
|
||||||
}),
|
|
||||||
}).catch(() => {
|
|
||||||
// Ignorer les erreurs de tracking
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// Ignorer les erreurs de tracking
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user