Skip to content

Commit

Permalink
Update middleware.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
et0and authored Oct 17, 2024
1 parent 8269aa2 commit 5698aa4
Showing 1 changed file with 0 additions and 56 deletions.
56 changes: 0 additions & 56 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { shouldFilterRequest } from "./utils/filterRequests";

const recordedPaths = new Set<string>();

export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;

// Don't record views for API routes, static files, or info pages
if (
!path.startsWith("/api") &&
!path.startsWith("/_next") &&
!path.startsWith("/info") &&
!path.match(
/\.(jpg|jpeg|png|gif|svg|ico|css|js|woff|woff2|ttf|eot|mp4|stl|glb|webmanifest|webp|pdf|svg|asc)$/,
)
) {
const filtered = shouldFilterRequest(request);
const key = `${path}-${(Date.now() / 1000) | 0}`; // Create a key with the path and current minute

if (!recordedPaths.has(key)) {
recordedPaths.add(key);

// Call our API route to record the page view
try {
const response = await fetch(
`${request.nextUrl.origin}/api/record-view`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ path, filtered }),
},
);

if (!response.ok) {
console.error("Failed to record page view");
}
} catch (error) {
console.error("Error recording page view:", error);
}

// Clean up old entries after 1 minute
setTimeout(() => {
recordedPaths.delete(key);
}, 60000);
}
}

return NextResponse.next();
}

export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};

0 comments on commit 5698aa4

Please sign in to comment.