Files
ember-market-frontend/middleware.ts
g a07ca55a1e
All checks were successful
Build Frontend / build (push) Successful in 1m14s
Improve dashboard prefetching and analytics charts
Removed dashboard prefetching from the login page to avoid unnecessary middleware redirects for unauthenticated users. Added delayed prefetching of dashboard routes after initial load for better navigation performance. Updated AdminAnalytics to use AreaChart instead of BarChart for daily metrics, improving visual clarity. Enhanced middleware to allow prefetch requests through without redirecting to login, supporting better caching and navigation.
2026-01-13 05:49:14 +00:00

32 lines
936 B
TypeScript

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (pathname.startsWith('/dashboard')) {
const authToken = request.cookies.get('Authorization')?.value;
const isPrefetch =
request.headers.get('Next-Router-Prefetch') === '1' ||
request.headers.get('purpose') === 'prefetch';
if (!authToken) {
if (isPrefetch) {
// Let prefetch requests through so caching works
return NextResponse.next();
}
const loginUrl = new URL('/auth/login', request.url);
loginUrl.searchParams.set('redirectUrl', pathname);
return NextResponse.redirect(loginUrl);
}
}
return NextResponse.next();
}
export const config = {
matcher: ['/dashboard/:path*'],
};