mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-09-17 11:02:06 +00:00
perf(startup): defer offline sync and backup status until the grid is interactive
Both fired during hydration and competed with rendering the grid, and the layout waited for the user store before it could show a signed-in shell even though the server already knew who the user was. Seed the user from the layout data, and queue the offline sync and backup refresh behind the grid's interactive mark; an explicit refresh, a reconnect or an edit still runs straight away. Backup refreshes now share one in-flight request per generation, and a change of account cancels the pending startup refresh and clears the stale status.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
// The only app stylesheet: Vite bundles, minifies and content-hashes it so it is cached for good.
|
||||
// static/output.css is built separately for the credential-free offline.html page only.
|
||||
import '../app.css';
|
||||
import { afterCriticalPageWork } from '$lib/utils/criticalPageWork';
|
||||
import { onDestroy, onMount } from 'svelte';
|
||||
import { user } from '$lib/stores/user.js';
|
||||
import { type User } from '@supabase/auth-js';
|
||||
@@ -27,18 +28,21 @@
|
||||
let { supabase } = data;
|
||||
$: ({ supabase } = data);
|
||||
|
||||
let localUser = null as User | null;
|
||||
let localUser: User | null = data.user ?? null;
|
||||
let userStoreReady = false;
|
||||
const unsubscribe = user.subscribe((value) => {
|
||||
localUser = value;
|
||||
if (userStoreReady) localUser = value;
|
||||
});
|
||||
onDestroy(unsubscribe);
|
||||
|
||||
let authSubscription: { unsubscribe: () => void } | null = null;
|
||||
let cancelBackupStartup: (() => void) | null = null;
|
||||
let stopOfflineSync: (() => void) | null = null;
|
||||
let isOnline = true;
|
||||
let signOutError = '';
|
||||
|
||||
onMount(() => {
|
||||
userStoreReady = true;
|
||||
isOnline = navigator.onLine;
|
||||
const updateOnlineState = () => {
|
||||
isOnline = navigator.onLine;
|
||||
@@ -60,7 +64,9 @@
|
||||
void getUser()
|
||||
.then(async () => {
|
||||
if (localUser) {
|
||||
void refreshBackupStatus();
|
||||
cancelBackupStartup = afterCriticalPageWork(() => {
|
||||
if (localUser) void refreshBackupStatus();
|
||||
});
|
||||
await claimOfflineData(localUser.id);
|
||||
}
|
||||
stopOfflineSync = startOfflineSync(() => localUser?.id ?? null);
|
||||
@@ -77,6 +83,8 @@
|
||||
// SIGNED_IN also fires when a tab regains focus, so only a change of account fetches a new
|
||||
// offline copy. Page loads and token refreshes reuse the saved one while it is fresh.
|
||||
if (event === 'SIGNED_IN' && session.user.id !== previousUserId) {
|
||||
cancelBackupStartup?.();
|
||||
clearBackupStatus();
|
||||
void claimOfflineData(session.user.id)
|
||||
.then(requestOfflineSync)
|
||||
.catch((error) => console.error('Unable to claim offline data', error));
|
||||
@@ -96,6 +104,7 @@
|
||||
return () => {
|
||||
authSubscription?.unsubscribe();
|
||||
stopOfflineSync?.();
|
||||
cancelBackupStartup?.();
|
||||
window.removeEventListener('online', updateOnlineState);
|
||||
window.removeEventListener('offline', updateOnlineState);
|
||||
for (const name of ['click', 'submit', 'input', 'change', 'keydown']) {
|
||||
|
||||
Reference in New Issue
Block a user