Reuse the open PWA window when an emailed link launches the app - #6
Merged
Conversation
The sign-in mail links back at our own origin — api/auth/magic builds the URL from origin() — so on a device where the app handles its own links, clicking it is an in-scope launch of p0dcasters. The manifest named no launch_handler, which leaves the browser on `auto`, and `auto` answers every launch with a new app window. For a magic link that is the worst shape the bug takes. The session cookie is set in whichever window opened the link, so you end up signed in inside a window you did not open while the one you were already using sits there signed out, looking like the link failed. `navigate-existing` hands the launch to the window used most recently and navigates it, so the link lands where you are already looking and the cookie is set in the window you were using. `auto` follows it for a browser that does not know the first value, and for when nothing is open yet. No service worker change is needed here: public/sw.js precaches nothing and never caches the manifest, so there is no stale copy for a version bump to evict. Safari and Firefox ignore launch_handler and keep their current behaviour. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013FR5zDT4DGJAmwDb6oVZ6n
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.
The same fix just shipped for rssamplifier.com (profullstack/rssamplifier.com#154), applied here.
The problem
Clicking the sign-in link in an email opens the installed PWA in a new window every time instead of reusing the one already open.
Why
src/app/api/auth/magic/route.tsbuilds the magic link from the site's ownorigin(), so every click is an in-scope launch ofp0dcasters.src/app/manifest.tsnamed nolaunch_handler, which leaves the browser onauto— andautoopens a new app window per launch.For a magic link this is the worst shape the bug takes: the session cookie is set in whichever window opened the link, so you end up signed in inside a window you did not open, while the window you were already using sits there signed out, looking like the link failed.
The fix
navigate-existinghands the launch to the most recently used app window and navigates it, so the link lands where you are already looking and the cookie is set in the window you were using.autotrails it for browsers that do not know the first value, and for when no window is open yet.MetadataRoute.Manifestin Next 15.5 types this natively (client_mode: ClientModeEnum | ClientModeEnum[], with both values in the enum), so no cast or escape hatch is needed.What did not need changing
Unlike rssamplifier, no service worker bump.
public/sw.jshere precaches nothing and never caches the manifest, so there is no stale copy for a version sweep to evict.Compatibility
Chromium 110+ honours it. Safari and Firefox ignore
launch_handlerand keep their current behaviour. iOS home-screen installs open email links in Safari rather than the app either way.Testing
npx tsc --noEmitpasses (exit 0)pnpm buildsucceeds.next/server/app/manifest.webmanifest.bodycontains"launch_handler":{"client_mode":["navigate-existing","auto"]}🤖 Generated with Claude Code
https://claude.ai/code/session_013FR5zDT4DGJAmwDb6oVZ6n