fix(desktop): keep server CORS headers so preflights cache - #47560
Merged
Conversation
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.
Every Desktop API call was preceded by a fresh CORS preflight. In a 310 s netlog from a Desktop debug export, 1519 requests to the local server were 759 real calls + 759
OPTIONS, and Chromium's preflight cache loggedhit-and-fail591 times andhit-and-passnever. Those preflights spent 20.9 s on the critical path (mean 27.5 ms each, 79 over 50 ms) and doubled the server's request load during every session-tab mount.Why the cache never hit
wireRendererHeadersranaddRendererHeaderson every response and it didupsertHeader(headers, "Access-Control-Allow-Headers", ["*"]). Per the Fetch spec*never matchesAuthorization, and Chromium'sPreflightCache::CheckIfRequestCanSkipPreflightvalidates cached entries strictly, so the entry was discarded on every lookup while the fresh preflight (checked leniently) kept passing and re-caching*.Change
packages/desktop/src/main/windows/headers.ts(pure, testable; noelectronimport):Access-Control-Allow-Headersis kept. The OpenCode server echoes the requested header list withmax-age: 86400, so Chromium now caches each (origin, URL, method) preflight for its 2 h cap.authorizationexplicitly so those cache too, and gets aMax-Age(Chromium's default without one is 5 s).onBeforeSendHeaderslistener that addedAccess-Control-Allow-Origin: *to request headers. It is a response header, so it did nothing, but a blocking listener costs a main-process round trip per request.Expected effect on the trace above: ~759 preflights → one per distinct URL, and the client's 4-slot request queue (#47441) stops spending half its slots on
OPTIONS.