Skip to content

Strip token query parameter from SW navigation requests#526

Merged
maraf merged 2 commits into
mainfrom
maraf/fix-sw-token-query
May 6, 2026
Merged

Strip token query parameter from SW navigation requests#526
maraf merged 2 commits into
mainfrom
maraf/fix-sw-token-query

Conversation

@maraf

@maraf maraf commented May 6, 2026

Copy link
Copy Markdown
Member

Production reported a recurring service worker error when users land on the app via a token-bearing URL:

The FetchEvent for "https://recollections.app/?token=demo" resulted in a network error response: the promise was rejected.

Why

onFetch in service-worker.published.js forwarded the raw navigation request (including ?token=...) both to cache.match and to the fetch() network fallback. When the cache miss happened and the network fetch then failed for any reason, the promise returned to respondWith() rejected, surfacing the error above to users.

What changed

Added stripTokenFromNavigation(), called from onFetch for mode === 'navigate' requests. It rebuilds the Request without the token query parameter before the cache lookup and the network fallback. Non-navigation requests are passed through untouched.

The SPA still reads window.location.search after boot (UserState.OnInitializedAsync), so the token remains available for the LoginWithTokenAsync API exchange. Only the SW-side fetch sees the cleaned URL.

Notes

  • Only the published service worker needed the change; the dev service-worker.js does not call respondWith() on regular fetches.
  • No equivalent issue is filed; this is a follow-up to a production console report.

The published service worker's onFetch handler forwarded the raw
navigation request (including '?token=...') to cache.match and to the
network fallback. When that fallback failed, the promise returned to
respondWith() rejected and Chrome surfaced:

  The FetchEvent for "https://recollections.app/?token=demo" resulted
  in a network error response: the promise was rejected.

Strip the 'token' query parameter from navigation requests before the
cache/network fetch. The SPA still reads window.location.search after
boot, so the token remains available for the API auth exchange.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a production service worker fetch error triggered when users open the app with a token-bearing navigation URL (e.g., /?token=...). It updates the published service worker to sanitize navigation requests by removing the token query parameter before cache lookup and network fallback, reducing the likelihood of respondWith() surfacing rejected fetch promises to end users.

Changes:

  • Added stripTokenFromNavigation() to remove the token query parameter from navigation requests.
  • Updated onFetch to use the sanitized request for cache matching and network fallback.

Comment thread src/Recollections.Blazor.UI/wwwroot/service-worker.published.js
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@maraf maraf marked this pull request as ready for review May 6, 2026 10:01
@maraf maraf added this to the blazor-v0.20.3 milestone May 6, 2026
@maraf maraf added bug Something isn't working blazor area-layout labels May 6, 2026
@maraf maraf merged commit 1b76d12 into main May 6, 2026
1 check passed
@maraf maraf deleted the maraf/fix-sw-token-query branch May 6, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-layout blazor bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants