fix(auth): keep app mounted across Supabase token refresh - #45
Draft
posthog-eu[bot] wants to merge 1 commit into
Draft
fix(auth): keep app mounted across Supabase token refresh#45posthog-eu[bot] wants to merge 1 commit into
posthog-eu[bot] wants to merge 1 commit into
Conversation
A token refresh (or a tab refocus) calls invalidate('supabase:auth'),
which re-runs the root load. The load built a new browser client and
reassigned the `supabase.client` $state singleton on every run. Because
the user state was `$derived(new UserState(supabase.client))`, that swap
rebuilt an empty UserState, so `isComplete` turned false and the whole
app hid behind LoadingSplash for several seconds.
Three layers now break that chain:
- +layout.ts reuses the existing browser client instead of rebuilding it.
- UserState is a real singleton; setClient() rebinds a new client and
keeps the already loaded user data.
- The (app) layout gates the splash on a first-load latch, so a brief
drop in isComplete no longer blanks the app.
Also fix hooks.client.ts importing `dev` from the non-existent $app/env
module (the value lives in $app/environment).
Generated-By: PostHog Desktop
Task-Id: 4bf014fe-cf36-45a4-95a0-498a8bfd4f70
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Problem
invalidate('supabase:auth'), which re-runs the root load. The load rebuilt the browser client and reassigned thesupabase.client$statesingleton on every run.$derived(new UserState(supabase.client)), so that swap threw away the loaded user and built an empty one —isCompleteturned false and(app)/+layout.svelteswapped the entire app forLoadingSplashuntil four Supabase round trips finished.Changes
+layout.tsreuses the existing browser client across load re-runs instead of building a new one each time.$derived(new UserState(...))with a singleton and asetClient()setter that rebinds a new client while keeping the loaded user data.(app)/+layout.sveltegatesLoadingSplashon a first-load flag, so a brief drop inisCompleteduring a refresh no longer blanks the app.The three layers are independent, so any one of them alone stops the flash; together they make the state resilient to a client swap.
Also fixed
hooks.client.tsimporteddevfrom$app/env, which is not a SvelteKit module. The value comes from$app/environment(already imported on the next line).Not included
@lucide/svelte+lucide-sveltedependencies (dev-onlyReferenceError: TriangleAlert is not definedon the landing page) are left out. That fix is a dependency migration across ~30 files, unrelated to the token-refresh symptom, and belongs in its own PR.Testing
svelte-checkpasses on the changed files (remaining errors are pre-existing and come from unset local$env/static/publicvariables).Why
Reported by a user who lost the app to the splash while reading a recipe; the event timeline shows same-URL
$pageleave/$pageviewpairs across the reported window.Created with PostHog Desktop from this inbox report.