fix: handle CORS preflight requests for /api/metrics#2
Merged
simonCatBot merged 6 commits intomasterfrom Apr 11, 2026
Merged
Conversation
added 6 commits
April 11, 2026 10:40
The browser was sending a CORS preflight OPTIONS request before the GET request. In standalone mode, Next.js wasn't handling the preflight, causing 'NetworkError when attempting to fetch resource'. - Add OPTIONS handler that returns proper CORS headers - Add Access-Control headers to GET response - Add Access-Control headers to error response
The Image component had width=80 and height=32 but CSS set h-8 (32px), creating a mismatch. Added inline style 'width: auto' to maintain aspect ratio and suppress the browser warning.
- Add loading='eager' to logo.svg as it's above the fold (LCP element) - Change ROCm image from w-auto to w-8 to keep width/height symmetric
React Strict Mode double-invokes effects in development, causing concurrent fetches that can result in NetworkError. This change: - Uses useRef to track AbortController - Cancels any pending request before starting a new one - Adds 5s timeout for fetch to prevent hanging - Cleans up controller on unmount
Using relative URL '/api/metrics' may cause issues in some deployment configurations. Switch to absolute URL to ensure the fetch always goes to the correct origin. Also improved error logging to capture error name and message for better debugging.
Explicitly set fetch mode to same-origin to prevent any potential CORS preflight issues. Added cache: no-store to prevent response caching issues with streaming.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixed the browser error:
NetworkError when attempting to fetch resourcethat appeared in the console when the dashboard tried to fetch metrics.Root Cause
The browser sends a CORS preflight OPTIONS request before the actual GET request. In Next.js standalone deployment mode, the preflight was not being handled, causing the fetch to fail with a NetworkError.
Fix
OPTIONShandler toapp/api/metrics/route.tsthat returns proper CORS headersAccess-Control-Allow-Origin: *,Access-Control-Allow-Methods: GET, OPTIONS, andAccess-Control-Allow-Headers: Content-Typeheaders to the GET responseTesting
Logs
Before (browser console):
Server logs showed 200 responses but browser still got errors due to failed preflight.