Skip to content

fix: handle CORS preflight requests for /api/metrics#2

Merged
simonCatBot merged 6 commits intomasterfrom
fix/cors-preflight
Apr 11, 2026
Merged

fix: handle CORS preflight requests for /api/metrics#2
simonCatBot merged 6 commits intomasterfrom
fix/cors-preflight

Conversation

@simonCatBot
Copy link
Copy Markdown
Owner

Summary

Fixed the browser error: NetworkError when attempting to fetch resource that 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

  1. Added an OPTIONS handler to app/api/metrics/route.ts that returns proper CORS headers
  2. Added Access-Control-Allow-Origin: *, Access-Control-Allow-Methods: GET, OPTIONS, and Access-Control-Allow-Headers: Content-Type headers to the GET response
  3. Also added CORS headers to the error response

Testing

  • All 62 tests pass ✓
  • Build succeeds ✓

Logs

Before (browser console):

[browser] Failed to fetch metrics: TypeError: NetworkError when attempting to fetch resource.

Server logs showed 200 responses but browser still got errors due to failed preflight.

Developer 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.
@simonCatBot simonCatBot merged commit 1be9a66 into master Apr 11, 2026
2 checks passed
@simonCatBot simonCatBot deleted the fix/cors-preflight branch April 11, 2026 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant