Below is my middleware code where I am trying to log to firebase, it doesn’t seem to be getting called. Folks, any inputs?
import { NextResponse } from 'next/server';
import axios from 'axios';
import Constants from '@/utilities/Constants'
export async function middleware(req) {
// Get country code from Cloudflare header (or default to 'Unknown')
const countryCode = req.headers.get('cf-ipcountry') || 'Unknown';
// Create a new response and attach the country code
const response = NextResponse.next();
response.headers.set('X-Country-Code', countryCode);
console.log("Middleware Detected Country:", countryCode); // Won't log in terminal, check browser console or network tab
try {
const data = { countryCode: countryCode, url: "middleware", date: new Date().toString() };
const response = await axios.post(Constants.FIREBASE_ERROR_LOGS_URL, data);
console.log('Firebase log successful-middleware:', response.data);
} catch (error) {
console.error('Error logging to Firebase:', error);
}
return response;
}
// Apply middleware to all routes
export const config = {
matcher: '/:path*',
};