Files
Blink/src/routes/_api.tsx
T
1xDed 205b655d00
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
perf(startup): instant splash, cached home and non-blocking auth
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.
2026-11-28 17:08:08 +03:00

47 lines
1.5 KiB
TypeScript

import { getDefaultServer, getServer } from "@/utils/storage/servers";
import { getUser } from "@/utils/storage/user";
import { createFileRoute, redirect } from "@tanstack/react-router";
/**
* Session bootstrap is deliberately local-only: reading the Tauri store is
* quick, while network verification would block the first render. The token
* is validated in the background by ProviderWrapper, which redirects to the
* login page on a real 401/403 without punishing transient network errors.
*/
export const Route = createFileRoute("/_api")({
beforeLoad: async ({ context, location }) => {
const isAuthPage =
location.pathname.startsWith("/login") ||
location.pathname.startsWith("/setup") ||
location.pathname.startsWith("/error");
if (!context.api) {
const currentServerId = await getDefaultServer();
if (!currentServerId) {
if (!isAuthPage) throw redirect({ to: "/setup/server/add" });
return;
}
const currentServer = await getServer(currentServerId);
if (!currentServer?.address) {
if (!isAuthPage) throw redirect({ to: "/setup/server/add" });
return;
}
const userOnDisk = await getUser();
context.createApi(currentServer.address, userOnDisk?.AccessToken);
if (!userOnDisk?.AccessToken && !isAuthPage) {
throw redirect({
to: "/login/manual",
search: { redirect: location.href },
});
}
} else if (!context.api.accessToken && !isAuthPage) {
throw redirect({
to: "/login/manual",
search: { redirect: location.href },
});
}
},
});