Repository navigation
Remove Front Door route cache and make /healthz no-store - #1937
Conversation
The Front Door route used IgnoreQueryString, so the first requester's query string was stored in StaticHost's cached slashless-directory 301 and replayed to every later visitor. Use UseQueryString so each query string is cached separately. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 90ef1a20-cd65-43f3-97cd-6fa484d8b0a4
There was a problem hiding this comment.
🟢 Approval recommended
The focused change addresses the cache-key mismatch; deployment still requires the documented endpoint purge.
No open issues 🎉
What changed in this PR
Updates Front Door caching so slashless-directory redirects do not replay another visitor’s query string.
Changes:
- Uses
UseQueryStringto cache responses separately by query string. - Adds a comment explaining why redirects require this behavior.
| File | Description |
|---|---|
| src/apphost/Aspire.Dev.AppHost/Extensions/DeploymentExtensions.cs | Includes query strings in Front Door cache keys. |
Review effort: Balanced
|
Review discussion on whether the Front Door route cache is needed at all, in addition to the Background
What the live origin does todayProbed against live aspire.dev (Kestrel, not behind AFD, brotli already applied at the origin):
Per the Front Door caching docs, with caching enabled Front Door honors origin directives (including What happens when content changes
What the route-wide cache buys us
Suggestion
Not verified: this was not tested against a real Front Door profile, since Dev/Test aren't resolvable and none was provisioned. The "honors |
The route-wide Front Door cache stores redirects that carry no Cache-Control for 1-3 days. A wrong or stale Location, or a replayed query string, then sticks until someone purges the endpoint. The origin already sends no-cache on pages and immutable on /_astro/*, and already serves brotli, so the cache adds little. Drop route.CacheConfiguration so every request reaches the origin, as suggested in review. This also removes the UseQueryString setting from the previous commit, which is no longer needed. - /healthz returns Cache-Control: no-store, so a health probe is never answered from a stored copy. - Turn on App Service Always On, so the site is not unloaded after about 20 minutes idle and the next request does not pay a cold start. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 90ef1a20-cd65-43f3-97cd-6fa484d8b0a4
|
Thanks, Eric. Addressed in 5a5ea7e:
Not done here: the optional second route that caches only Still untested until it deploys: ETag/304 through Front Door, and whether redirects already cached at the edge are still served. The post-deploy checks are in the description. |
A scratch App Service with the same shape (Premium v3 x2, elastic scale, minimum 2 instances, pre-warm 1, Always On off) answered its first request after about 21 hours idle in 277 ms server time, so the always-ready instances already keep the site loaded. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 90ef1a20-cd65-43f3-97cd-6fa484d8b0a4
#1942) ## Summary Replaces the Server-Sent Events (SSE) live-status stream with a WebSocket stream, because Azure Front Door (AFD) does not relay SSE. ### Problem The first-party doc says it outright: "Front Door doesn't support Server-Sent Events (SSE)" (https://learn.microsoft.com/en-us/azure/frontdoor/standard-premium/websocket). We confirmed it empirically for `GET /api/live/stream/`: | Path | Result | | --- | --- | | Origin (App Service) | 200 `text/event-stream`, `event: state` at 0.6 s, `:hb` comment every 15 s, `Cache-Control: no-store`, `X-Accel-Buffering: no` | | Through AFD (`https://aspire.dev`) | 0 headers and 0 bytes, then 504 gateway timeout at ~220 s | | Browser through AFD | `new EventSource('/api/live/stream/')` never fires `open` | The client seeded once from `GET /api/live/` and then relied only on `EventSource`, so the live badge went stale in production. ### What changed - Server (`src/statichost/StaticHost`) - New `GET /api/live/ws` endpoint (`app.UseWebSockets()`, KeepAliveInterval 30 s). It reuses the existing `LiveStatusBroadcaster` / Redis fan-out; only the transport changed. - JSON frames: `{"type":"state","data":{...}}` (same data as the old `state` event) and `{"type":"hb"}` every 15 s. - Server closes after 80-100% of `StreamMaxLifetimeSeconds` (default 600 s, key name unchanged) with 1000, and with 1001 on shutdown. Client disconnects and cancellation are handled. - Non-WebSocket requests to `/api/live/ws` get 400. - The SSE endpoint `/api/live/stream` is removed (now 404). `/api/live` snapshot is unchanged. Telemetry exclusion and the AppHost dashboard URL now point at `/api/live/ws`. - Client (`live-status.ts`) - `EventSource` replaced by `WebSocket` (`ws:`/`wss:` from `location`, path `/api/live/ws`, no trailing slash). - Still seeds from `/api/live/`; exponential backoff with jitter; visibility/online handling kept; DOM/CSS unchanged. - Polling fallback: `/api/live/` is polled every 30 s whenever the socket is not open (and the tab is visible), so the badge cannot go stale if the socket is blocked. A 45 s stale-socket watchdog closes and reconnects a socket that goes silent. - Vite dev proxy for `/api/live` gets `ws: true`. - AppHost: `website.SiteConfig.IsWebSocketsEnabled = true`. It was NOT set in the public AppHost, and WebSockets are off by default on App Service, so this is required for the endpoint to work. Please review this setting. - Tests/docs: SSE tests replaced by `LiveWebSocketTests`; vitest and Playwright specs updated; `Live/README.md` updated. ### AFD constraints considered - Caching must be disabled on WebSocket routes or AFD drops the Upgrade header. The AFD route has no cache config after #1937, so this is fine. - Idle timeout is 5 min: the 15 s heartbeat (plus 30 s protocol keep-alive) keeps it alive. - Connections are closed after at most 2 h and may drop on maintenance: the client reconnects with backoff and the polling fallback covers gaps. - Capacity: 3,000 concurrent connections per AFD profile. The ~10 min jittered server lifetime makes clients cycle. - WAF only inspects the handshake. AFD health probes use HTTP and are unaffected. ### Deploy note The internal Azure DevOps `deploy` branch pulls from public `main`, so it needs to be synced for this to ship (including the App Service WebSockets setting). ## Third-party links and affiliations None. The only new link is the first-party Microsoft Learn AFD WebSocket doc. ## Validation - `dotnet build tests\StaticHost.Tests\StaticHost.Tests.csproj` - passed (0 errors; only the existing NU1902 MessagePack warning) - `dotnet test tests\StaticHost.Tests\StaticHost.Tests.csproj --no-build --filter "FullyQualifiedName!~Live.Redis"` - passed, 337/337 (includes new WebSocket tests: initial state, updates, heartbeat, non-WS 400, max-lifetime close, shutdown close, client close/abort) - `pnpm exec vitest run --config vitest.config.ts tests/unit/live-status.vitest.test.ts tests/unit/astro-vite-config.vitest.test.ts` (in `src/frontend`) - passed, 22/22 - Local round trip: ran StaticHost on `http://127.0.0.1:5099` against a throwaway Redis container; a Node `ws` client received `{"type":"state",...}` at 0.2 s and `{"type":"hb"}` at 15.2 s and 30.2 s, then a clean 1000 close; plain `GET /api/live/ws` returned 400 and `/api/live/stream` returned 404. ### Not verified - Through Front Door: production AFD fronts the currently deployed site, which has no WS endpoint until this ships. - `Live.Redis*` integration tests (need Aspire + Docker containers) were not run. - Playwright e2e specs were updated but not run. ### Post-deploy checklist - [ ] App Service `webSocketsEnabled` is true after deploy - [ ] Script opens `wss://aspire.dev/api/live/ws` and shows `state` + `hb` messages for 60+ s, including past 220 s (and ideally past 5 min) - [ ] Live badge updates in a browser on `https://aspire.dev` with the network tab showing a 101 upgrade --------- Co-authored-by: David Pine <[email protected]> Co-authored-by: Copilot App <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]> Copilot-Session: 90ef1a20-cd65-43f3-97cd-6fa484d8b0a4
Summary
Removes the Front Door route cache (
route.CacheConfiguration) so every request reaches StaticHost, following Eric's review comment. This replaces the earlierUseQueryStringchange in this PR.Why: Front Door caches StaticHost's 301 redirects, which carry no
Cache-Control, for 1-3 days. A staleLocationor a replayed query string then sticks until someone purges the endpoint. The origin already setsno-cacheon pages andimmutableon/_astro/*, and serves brotli, so the edge cache adds little.Also in this PR:
/healthzreturnsCache-Control: no-store, so a health probe is never answered from a stored copy.Trade-offs, none measured yet on a deployed Front Door:
/_astro/*is no longer cached at the edge.If measurements justify it, a second route that caches only
/_astro/*can restore edge caching for fingerprinted assets.The generated bicep on the internal
deploybranch is refreshed in the follow-up internal PR.Third-party links and affiliations
None
Validation
dotnet test tests\StaticHost.Tests\StaticHost.Tests.csproj --filter "FullyQualifiedName!~Live.Redis" --nologo -v q: passed, 332 tests. The Redis integration tests need Docker and were excluded.GET /healthzreturned 200 withCache-Control: no-store, andGET /keptno-cacheand its ETag.aspire publishon a throwaway worktree of the internaldeploybranch with an earlier revision of these changes: the Front Door route lostcacheConfiguration. The only other drift wasalwaysOn: trueon the website bicep, which this PR no longer sets.pnpm build. No frontend files changed.cacheConfiguration;/healthzreturnsno-store(expectX-Cache: CONFIG_NOCACHE); slashless URLs redirect correctly on every edge; a conditional GET returns 304; assets are still brotli.