Gate analytics and cache public API requests

This commit is contained in:
2569718930@qq.com
2026-04-06 13:58:11 +08:00
parent 9be12ad1d7
commit 08d7308486
14 changed files with 114 additions and 309 deletions
@@ -5,8 +5,14 @@ import {
} from "@/lib/backend-auth";
const API_BASE = process.env.POLYWEATHER_API_BASE_URL;
const ANALYTICS_ENABLED =
process.env.NEXT_PUBLIC_POLYWEATHER_APP_ANALYTICS === "true";
export async function POST(req: NextRequest) {
if (!ANALYTICS_ENABLED) {
return new NextResponse(null, { status: 204 });
}
if (!API_BASE) {
return NextResponse.json(
{ error: "POLYWEATHER_API_BASE_URL is not configured" },
+8 -3
View File
@@ -18,10 +18,15 @@ export async function GET(req: NextRequest) {
}
try {
const auth = await buildBackendRequestHeaders(req);
const res = await fetch(`${API_BASE}/api/cities`, {
const auth = await buildBackendRequestHeaders(req, {
includeSupabaseIdentity: false,
});
const fetchOptions = {
headers: auth.headers,
cache: "no-store",
next: { revalidate: 300 },
} as const;
const res = await fetch(`${API_BASE}/api/cities`, {
...fetchOptions,
});
if (!res.ok) {
const raw = await res.text();
+3 -1
View File
@@ -23,7 +23,9 @@ export async function GET(
const url = `${API_BASE}/api/city/${encodeURIComponent(name)}?force_refresh=${forceRefresh}`;
try {
const auth = await buildBackendRequestHeaders(req);
const auth = await buildBackendRequestHeaders(req, {
includeSupabaseIdentity: false,
});
const res = await fetch(url, {
headers: auth.headers,
cache: "no-store",
+14 -3
View File
@@ -25,10 +25,21 @@ export async function GET(
const url = `${API_BASE}/api/city/${encodeURIComponent(name)}/summary?force_refresh=${forceRefresh}`;
try {
const auth = await buildBackendRequestHeaders(req);
const auth = await buildBackendRequestHeaders(req, {
includeSupabaseIdentity: false,
});
const fetchOptions =
bypassCache
? {
headers: auth.headers,
cache: "no-store" as const,
}
: {
headers: auth.headers,
next: { revalidate: 20 },
};
const res = await fetch(url, {
headers: auth.headers,
cache: "no-store",
...fetchOptions,
});
if (!res.ok) {
const raw = await res.text();
+3 -1
View File
@@ -15,7 +15,9 @@ export async function GET(req: NextRequest) {
}
try {
const auth = await buildBackendRequestHeaders(req);
const auth = await buildBackendRequestHeaders(req, {
includeSupabaseIdentity: false,
});
const res = await fetch(`${API_BASE}/healthz`, {
headers: auth.headers,
cache: "no-store",
+5 -2
View File
@@ -24,9 +24,12 @@ export async function GET(
try {
const auth = await buildBackendRequestHeaders(req);
const res = await fetch(url, {
const fetchOptions = {
headers: auth.headers,
cache: "no-store",
next: { revalidate: 60 },
} as const;
const res = await fetch(url, {
...fetchOptions,
});
if (!res.ok) {
const raw = await res.text();
+17
View File
@@ -5,6 +5,9 @@ import {
recordVitalsSample,
} from "@/lib/vitals-store";
const WEB_VITALS_ENABLED =
process.env.NEXT_PUBLIC_POLYWEATHER_WEB_VITALS === "true";
type VitalsPayload = {
id?: string;
metric?: string;
@@ -15,6 +18,10 @@ type VitalsPayload = {
};
export async function POST(request: Request) {
if (!WEB_VITALS_ENABLED) {
return new NextResponse(null, { status: 204 });
}
try {
const payload = (await request.json()) as VitalsPayload;
const metric = normalizeMetricName(payload.metric);
@@ -56,6 +63,16 @@ export async function POST(request: Request) {
}
export async function GET(request: Request) {
if (!WEB_VITALS_ENABLED) {
return NextResponse.json({
ok: true,
disabled: true,
generatedAt: Date.now(),
sampleCount: 0,
routes: {},
});
}
const { searchParams } = new URL(request.url);
const targetRoute = String(searchParams.get("route") || "").trim();
const summary = getVitalsSummary();
+1 -5
View File
@@ -1,5 +1,4 @@
import type { Metadata } from "next";
import { Analytics } from "@vercel/analytics/react";
import "./globals.css";
export const metadata: Metadata = {
@@ -31,10 +30,7 @@ export default function RootLayout({
rel="stylesheet"
/>
</head>
<body className="min-h-screen font-sans antialiased">
{children}
<Analytics />
</body>
<body className="min-h-screen font-sans antialiased">{children}</body>
</html>
);
}