perf(startup): instant splash, cached home and non-blocking auth
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped

Renders a static branded splash immediately, restores the last user/API session from disk, hydrates home/library queries from localStorage, shows a home skeleton while the first request runs, drops blocking auth network checks, and redirects a streamless /player to home.
This commit is contained in:
1xDed committed 2026-11-28 17:08:08 +03:00
1 parent b1d9b40def
commit 205b655d00
12 files changed
+424 -158

No files matched your search

+41 -20
View File
@@ -10,6 +10,10 @@ import {
useApiInContext,
} from "./utils/store/api";
import { CentralProvider, useCentralStore } from "./utils/store/central";
import {
persistQueryCache,
restoreQueryCache,
} from "./utils/queryCachePersistence";
import { delUser } from "./utils/storage/user";
const queryClient = new QueryClient({
@@ -47,7 +51,10 @@ const router = createRouter({
// Only animate real page changes; search-param updates (typing in the
// search field) must not trigger a transition on every keystroke.
defaultViewTransition: {
types: ({ pathChanged }) => (pathChanged ? ["page"] : false),
types: ({ pathChanged }) =>
pathChanged && document.visibilityState === "visible"
? ["page"]
: false,
},
scrollRestoration: true,
scrollRestorationBehavior: "auto",
@@ -70,23 +77,24 @@ function ProviderWrapper() {
s.currentUser,
s.fetchCurrentUser,
]);
// Validate/refresh the saved session in the background. The UI reads the
// minimal user snapshot from disk immediately, so this never blocks paint.
useEffect(() => {
if (api?.accessToken && !user?.Id) {
fetchCurrentUser(api).catch(async (error) => {
console.error("Could not restore the saved session", error);
const status = (error as { response?: { status?: number } })?.response
?.status;
// Only an actual authorization failure should log the user out.
if (status !== 401 && status !== 403) return;
await delUser();
createApi(api.basePath, undefined);
router.navigate({
to: "/login/manual",
search: { redirect: window.location.pathname },
});
if (!api?.accessToken) return;
fetchCurrentUser(api).catch(async (error) => {
console.error("Could not restore the saved session", error);
const status = (error as { response?: { status?: number } })?.response
?.status;
// Only an actual authorization failure should log the user out.
if (status !== 401 && status !== 403) return;
await delUser();
createApi(api.basePath, undefined);
router.navigate({
to: "/login/manual",
search: { redirect: window.location.pathname },
});
}
}, [api, user, fetchCurrentUser, createApi]);
});
}, [api?.accessToken, fetchCurrentUser, createApi]);
useEffect(() => {
if (api) {
console.log(`API is set - Route ${window.location.pathname}`);
@@ -134,12 +142,27 @@ const init = async () => {
}, 200);
});
const initialApi = await initializeApi();
const { api: initialApi, user: initialUser } = await initializeApi();
const owner =
initialApi?.basePath && initialUser?.Id
? `${initialApi.basePath}|${initialUser.Id}`
: undefined;
if (owner) {
restoreQueryCache(queryClient, owner);
persistQueryCache(queryClient, () => owner);
}
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<CentralProvider>
<CentralProvider
initialUser={
initialUser
? { Id: initialUser.Id, Name: initialUser.Name }
: null
}
>
<ApiProvider initialApi={initialApi}>
<ProviderWrapper />
</ApiProvider>
@@ -150,5 +173,3 @@ const init = async () => {
};
init();
document.querySelector(".app-loading")!.classList.toggle("hidden");