import { NextRequest, NextResponse } from "next/server"; import { verifyToken } from…
- User
import { NextRequest, NextResponse } from "next/server"; import { verifyToken } from "@/src/helper/verifyToken"; import { publicRoutes } from "@/src/constants/middleware";
export default async function middleware(request: NextRequest) { try { const pathname = request.nextUrl.pathname; console.log("Middleware executing for path:", pathname); //for bypass the middleware for routes // if (pathname) return NextResponse.next(); if (publicRoutes(pathname)) { console.log("Path is public, continuing..."); return NextResponse.next(); }
const token = request.headers.get("authorization")?.split(" ")[1] || ""; console.log("Token extracted:", token);
if (!token) { console.log("No token found, redirecting to login..."); return NextResponse.redirect(new URL("/login", request.url)); }
const decodedToken = verifyToken(token); if (!decodedToken) { console.log("Invalid token, redirecting to login..."); return NextResponse.redirect(new URL("/login", request.url)); }
console.log("Token verified, allowing access to protected route"); return NextResponse.next(); } catch (error) { console.error("Error in middleware:", error); return NextResponse.redirect(new URL("/login", request.url)); } }
export const config = { matcher: [ /*
- Match all request paths except for the ones starting with:
-
- api (API routes)
-
- _next/static (static files)
-
- _next/image (image optimization files)
-
- favicon.ico (favicon file)
-
- assets in the public folder / "/((?!api|_next/static|_next/image|favicon.ico|public).)", ], }; @workspace styles of my app is corrupted cause of matcher
- Assistant
Your matcher configuration in the middleware is likely interfering with the serving of some static assets, including styles, due to how it's set. This matcher is supposed to exclude paths for API routes and static files but may still need adjustments to ensure all vital static assets are served correctly.
The issue lies here:
matcher: [ "/((?!api|_next/static|_next/image|favicon.ico|public).)", ],To resolve the issue with your styles not loading, you should make sure that you're excluding all the paths where your static assets, including CSS, JavaScript, and other resources, might reside. We can modify the matcher pattern to make sure it covers typical paths that host static resources:
export const config = { matcher: [ /* * Match all request paths except for the ones starting with: * - api (API routes) * - _next/static (static files) * - _next/image (image optimization files) * - favicon.ico (favicon file) * - public (assets in the public folder) * - static (common for static assets) * - css, js (common file types for static resources) */ "/((?!api|_next/static|_next/image|favicon.ico|public|static|css|js).*)", ], };
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.