-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/version 6 0 4 #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Feat/version 6 0 4 #141
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
58648fd
feat: implement Speed Insight component for real-time PageSpeed analysis
ColdByDefault 1c855d4
refactor: improve layout and styling of ScoreRing and SpeedInsight co…
ColdByDefault 72319c2
feat: implement LocaleAutoDetect component for improved language dete…
ColdByDefault 7e7a2f9
feat: enhance CookiesBanner with internationalization support and imp…
ColdByDefault 883c2f0
feat: add force refresh capability and cooldown management to SpeedIn…
ColdByDefault File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /** | ||
| * @author ColdByDefault | ||
| * @copyright 2026 ColdByDefault. All Rights Reserved. | ||
| */ | ||
|
|
||
| import { NextResponse } from "next/server"; | ||
| import { sanitizeErrorMessage } from "@/lib/security"; | ||
| import type { | ||
| RawPageSpeedResponse, | ||
| SpeedInsightResult, | ||
| SpeedInsightScore, | ||
| SpeedInsightApiResponse, | ||
| } from "@/types/configs/speed-insight"; | ||
|
|
||
| const PAGESPEED_API_URL = | ||
| "https://www.googleapis.com/pagespeedonline/v5/runPagespeed"; | ||
| const TARGET_URL = | ||
| process.env.PAGESPEED_TARGET_URL || "https://coldbydefault.com"; | ||
| const API_KEY = process.env.GOOGLE_PAGESPEED_API_KEY; | ||
|
|
||
| /** Map a 0–1 score to a Tailwind color class */ | ||
| function getScoreColor(score: number): string { | ||
| if (score >= 90) return "text-green-500"; | ||
| if (score >= 50) return "text-yellow-500"; | ||
| return "text-red-500"; | ||
| } | ||
|
|
||
| /** Parse the raw Google API response into our clean type */ | ||
| function parseResult( | ||
| raw: RawPageSpeedResponse, | ||
| strategy: "mobile" | "desktop", | ||
| ): SpeedInsightResult { | ||
| const cats = raw.lighthouseResult.categories; | ||
|
|
||
| const categories: SpeedInsightScore[] = Object.values(cats).map((cat) => { | ||
| const pct = Math.round((cat.score ?? 0) * 100); | ||
| return { | ||
| label: cat.title, | ||
| score: pct, | ||
| color: getScoreColor(pct), | ||
| }; | ||
| }); | ||
|
|
||
| return { | ||
| url: raw.id, | ||
| strategy, | ||
| categories, | ||
| fetchedAt: raw.lighthouseResult.fetchTime, | ||
| }; | ||
| } | ||
|
|
||
| /** Fetch PageSpeed data for a given strategy */ | ||
| async function fetchPageSpeed( | ||
| strategy: "mobile" | "desktop", | ||
| forceRefresh = false, | ||
| ): Promise<SpeedInsightResult> { | ||
| const params = new URLSearchParams({ | ||
| url: TARGET_URL, | ||
| strategy, | ||
| category: "performance", | ||
| }); | ||
|
|
||
| // Add all categories | ||
| ["accessibility", "best-practices", "seo"].forEach((cat) => | ||
| params.append("category", cat), | ||
| ); | ||
|
|
||
| if (API_KEY) { | ||
| params.set("key", API_KEY); | ||
| } | ||
|
|
||
| const response = await fetch(`${PAGESPEED_API_URL}?${params.toString()}`, { | ||
| headers: { | ||
| Referer: TARGET_URL, | ||
| }, | ||
| ...(forceRefresh | ||
| ? { cache: "no-store" as const } | ||
| : { next: { revalidate: 3600 } }), // Cache for 1 hour unless force refresh | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
| throw new Error( | ||
| `PageSpeed API error (${strategy}): ${response.status} – ${errorText}`, | ||
| ); | ||
| } | ||
|
|
||
| const data = (await response.json()) as RawPageSpeedResponse; | ||
| return parseResult(data, strategy); | ||
| } | ||
|
|
||
| export async function GET(request: Request): Promise<NextResponse> { | ||
| const { searchParams } = new URL(request.url); | ||
| const forceRefresh = searchParams.has("refresh"); | ||
|
|
||
| try { | ||
| const [desktop, mobile] = await Promise.all([ | ||
| fetchPageSpeed("desktop", forceRefresh), | ||
| fetchPageSpeed("mobile", forceRefresh), | ||
| ]); | ||
|
|
||
| const body: SpeedInsightApiResponse = { desktop, mobile }; | ||
|
|
||
| return NextResponse.json(body, { | ||
| headers: { | ||
| "Cache-Control": "public, s-maxage=3600, stale-while-revalidate=7200", | ||
| "X-Content-Type-Options": "nosniff", | ||
| "X-Frame-Options": "DENY", | ||
| "X-XSS-Protection": "1; mode=block", | ||
| "Referrer-Policy": "strict-origin-when-cross-origin", | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| console.error("PageSpeed API Error:", error); | ||
|
|
||
| return NextResponse.json( | ||
| { | ||
| error: "Failed to fetch PageSpeed data", | ||
| message: sanitizeErrorMessage(error), | ||
| desktop: null, | ||
| mobile: null, | ||
| } satisfies SpeedInsightApiResponse & { message: string }, | ||
| { | ||
| status: 500, | ||
| headers: { | ||
| "X-Content-Type-Options": "nosniff", | ||
| "X-Frame-Options": "DENY", | ||
| }, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The speed-insight API route lacks rate limiting, which could allow abuse of the Google PageSpeed API. Other API routes in this codebase (e.g., app/api/github/route.ts, app/api/about/route.ts) implement RateLimiter to protect against excessive requests. Consider adding rate limiting to prevent abuse and protect the Google PageSpeed API quota, especially since API calls are expensive and the endpoint is publicly accessible.