From 436afe121ab152ff1e0d4dc502d7ae6a85c68f6c Mon Sep 17 00:00:00 2001
From: "2569718930@qq.com" <2569718930@qq.com>
Date: Thu, 26 Mar 2026 17:04:56 +0800
Subject: [PATCH] Reduce Vercel CPU usage by narrowing auth checks
---
frontend/app/layout.tsx | 2 --
frontend/middleware.ts | 19 +++++++++++++++++++
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/frontend/app/layout.tsx b/frontend/app/layout.tsx
index 26a2a493..6026d0ec 100644
--- a/frontend/app/layout.tsx
+++ b/frontend/app/layout.tsx
@@ -1,7 +1,6 @@
import type { Metadata } from "next";
import { Analytics } from "@vercel/analytics/react";
import { SpeedInsights } from "@vercel/speed-insights/next";
-import { WebVitalsReporter } from "@/components/observability/WebVitalsReporter";
import "./globals.css";
export const metadata: Metadata = {
@@ -35,7 +34,6 @@ export default function RootLayout({
{children}
-
diff --git a/frontend/middleware.ts b/frontend/middleware.ts
index a47e4e39..7f0dde9d 100644
--- a/frontend/middleware.ts
+++ b/frontend/middleware.ts
@@ -57,6 +57,16 @@ function isPublicApi(pathname: string) {
);
}
+function shouldRefreshOptionalSupabaseSession(pathname: string) {
+ return (
+ pathname.startsWith("/account") ||
+ pathname.startsWith("/ops") ||
+ pathname.startsWith("/api/ops/") ||
+ pathname.startsWith("/api/payments/") ||
+ pathname === "/api/system/status"
+ );
+}
+
function handleLegacyTokenGate(request: NextRequest) {
const requiredToken = process.env.POLYWEATHER_DASHBOARD_ACCESS_TOKEN?.trim();
if (!requiredToken) {
@@ -144,6 +154,15 @@ async function handleSupabaseAuthGate(request: NextRequest) {
}
async function handleSupabaseOptionalSession(request: NextRequest) {
+ const { pathname } = request.nextUrl;
+ if (
+ isPublicPage(pathname) ||
+ isPublicApi(pathname) ||
+ !shouldRefreshOptionalSupabaseSession(pathname)
+ ) {
+ return NextResponse.next();
+ }
+
const response = NextResponse.next({
request: {
headers: request.headers,