Gate analytics and cache public API requests
This commit is contained in:
@@ -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" },
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 +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>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user