fix(startup): reduce initial loading and unblock app restoration

This commit is contained in:
t8y2 committed 2026-09-25 09:03:15 +08:00
1 parent 55c07f5852
commit 48d66d04d0
23 files changed
+745 -123

No files matched your search

+59 -81
View File
@@ -1,15 +1,31 @@
<script setup lang="ts">
import { defineAsyncComponent, onMounted, ref, watch } from "vue";
import { defineAsyncComponent, onMounted, onUnmounted, ref, shallowRef, watch } from "vue";
import { useI18n } from "vue-i18n";
import LoginPage from "@/components/auth/LoginPage.vue";
import SecurityMigrationWizard from "@/components/migration/SecurityMigrationWizard.vue";
import StartupLoading from "@/components/layout/StartupLoading.vue";
import { useMigrationStore } from "@/stores/migrationStore";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { apiUrl, webPath } from "@/lib/common/webPath";
import { webPath } from "@/lib/common/webPath";
import { loadSavedLocale } from "@/i18n";
import { checkStartupAuthentication, type StartupAuthentication } from "@/lib/startup/startupAuthentication";
import { markStartupPhase } from "@/lib/startup/startupTiming";
import { retryStartupAfterPreloadFailure } from "@/lib/startup/startupPreloadRecovery";
// App setup installs listeners and instantiates business stores, so even its import
// is deferred until authentication and the security migration have completed.
const App = defineAsyncComponent(() => import("./App.vue"));
const startupAsyncOptions = {
loadingComponent: StartupLoading,
errorComponent: StartupLoading,
delay: 0,
suspensible: false,
onError(error: Error, _retry: () => void, fail: () => void) {
retryStartupAfterPreloadFailure(error);
fail();
},
};
const App = defineAsyncComponent({ ...startupAsyncOptions, loader: () => import("./App.vue") });
const LoginPage = defineAsyncComponent({ ...startupAsyncOptions, loader: () => import("@/components/auth/LoginPage.vue") });
const SecurityMigrationWizard = defineAsyncComponent({ ...startupAsyncOptions, loader: () => import("@/components/migration/SecurityMigrationWizard.vue") });
const props = defineProps<{ localeReady?: Promise<void> }>();
const { t } = useI18n();
const migration = useMigrationStore();
const { blocking } = migration;
@@ -17,33 +33,42 @@ const checkingAuth = ref(true);
const loginRequired = ref(false);
const setupRequired = ref(false);
const authFailed = ref(false);
const enteringApp = ref(false);
let launchTransitionTimer: number | undefined;
const startupAuthentication = shallowRef<StartupAuthentication>();
const checkingLocale = ref(Boolean(props.localeReady));
const localeFailed = ref(false);
let initialLocaleReady = props.localeReady;
let authRequest: AbortController | undefined;
watch(
() => migration.state.entered,
(entered) => {
if (entered) {
enteringApp.value = true;
}
},
);
watch(migration.blocking, (blocked) => {
if (!blocked) markStartupPhase("migration-ready");
});
function appReady() {
window.clearTimeout(launchTransitionTimer);
launchTransitionTimer = window.setTimeout(() => {
enteringApp.value = false;
}, 650);
async function initializeLocale() {
if (!initialLocaleReady && !localeFailed.value) return;
checkingLocale.value = true;
localeFailed.value = false;
try {
await (initialLocaleReady ?? loadSavedLocale());
markStartupPhase("locale-ready");
window.dispatchEvent(new Event("dbx:startup-ready"));
} catch {
localeFailed.value = true;
} finally {
initialLocaleReady = undefined;
checkingLocale.value = false;
}
}
async function initialize() {
authRequest?.abort();
const request = new AbortController();
authRequest = request;
checkingAuth.value = true;
authFailed.value = false;
try {
if (!isTauriRuntime()) {
const response = await fetch(apiUrl("/api/auth/check"), { credentials: "same-origin" });
if (!response.ok) throw new Error("AUTH_CHECK_FAILED");
const result = await response.json();
if (typeof result.required !== "boolean" || typeof result.authenticated !== "boolean") throw new Error("AUTH_CHECK_FAILED");
const result = await checkStartupAuthentication(request.signal);
if (request.signal.aborted) return;
startupAuthentication.value = result;
setupRequired.value = result.setup_required === true;
loginRequired.value = setupRequired.value || (result.required && !result.authenticated);
if (loginRequired.value) {
@@ -51,74 +76,27 @@ async function initialize() {
return;
}
}
markStartupPhase("auth-ready");
await migration.initialize();
} catch {
authFailed.value = true;
if (!request.signal.aborted) authFailed.value = true;
} finally {
checkingAuth.value = false;
if (!request.signal.aborted) checkingAuth.value = false;
}
}
async function authenticated() {
history.replaceState(null, "", webPath("/"));
await initialize();
}
onMounted(initialize);
onMounted(() => {
void initializeLocale();
void initialize();
});
onUnmounted(() => authRequest?.abort());
</script>
<template>
<div v-if="checkingAuth || authFailed" class="fixed inset-0 flex flex-col items-center justify-center gap-4 bg-background text-foreground" role="status">
<p>{{ t(authFailed ? "migration.authFailed" : "migration.checking") }}</p>
<button v-if="authFailed" class="rounded border px-4 py-2" @click="initialize">{{ t("migration.retry") }}</button>
</div>
<StartupLoading v-if="checkingLocale || localeFailed || checkingAuth || authFailed" :label="authFailed ? t('migration.authFailed') : !checkingLocale && !localeFailed ? t('migration.checking') : undefined" :error="localeFailed || authFailed" :retry="localeFailed ? initializeLocale : initialize" />
<LoginPage v-else-if="loginRequired" :setup-mode="setupRequired" @authenticated="authenticated" />
<SecurityMigrationWizard v-else-if="blocking" :store="migration" />
<div v-else class="relative min-h-screen">
<Suspense @resolve="appReady">
<App />
</Suspense>
<Transition name="startup-fade">
<div v-if="enteringApp" class="fixed inset-0 z-[1100] flex items-center justify-center bg-background text-foreground" role="status" aria-live="polite">
<div class="flex flex-col items-center gap-5 text-center">
<span class="flex h-14 w-14 items-center justify-center rounded-full bg-primary/10 text-2xl text-primary" aria-hidden="true">✓</span>
<div>
<p class="text-lg font-semibold">{{ t("migration.successTitle") }}</p>
<p class="mt-2 text-sm text-muted-foreground">{{ t("migration.launching") }}</p>
</div>
<span class="h-1.5 w-24 overflow-hidden rounded-full bg-primary/15" aria-hidden="true"><span class="startup-progress block h-full w-1/2 rounded-full bg-primary" /></span>
</div>
</div>
</Transition>
</div>
<App v-else :startup-authentication="startupAuthentication" />
</template>
<style scoped>
.startup-fade-enter-active,
.startup-fade-leave-active {
transition: opacity 280ms ease;
}
.startup-fade-enter-from,
.startup-fade-leave-to {
opacity: 0;
}
.startup-progress {
animation: startup-progress 1.2s ease-in-out infinite;
}
@keyframes startup-progress {
from {
transform: translateX(-100%);
}
to {
transform: translateX(200%);
}
}
@media (prefers-reduced-motion: reduce) {
.startup-progress {
animation: none;
transform: translateX(50%);
}
}
</style>