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
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:
1 parent
b1d9b40def
commit
205b655d00
12 files changed
+424
-158
No files matched your search
+26
-83
@@ -1,14 +1,13 @@
|
||||
import { getDefaultServer, getServer } from "@/utils/storage/servers";
|
||||
import { delUser, getUser } from "@/utils/storage/user";
|
||||
import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api";
|
||||
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
|
||||
import { getUser } from "@/utils/storage/user";
|
||||
import { createFileRoute, redirect } from "@tanstack/react-router";
|
||||
|
||||
const isAuthError = (error: unknown) => {
|
||||
const status = (error as { response?: { status?: number } })?.response?.status;
|
||||
return status === 401 || status === 403;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 =
|
||||
@@ -18,86 +17,30 @@ export const Route = createFileRoute("/_api")({
|
||||
|
||||
if (!context.api) {
|
||||
const currentServerId = await getDefaultServer();
|
||||
if (currentServerId) {
|
||||
const currentServer = await getServer(currentServerId);
|
||||
if (currentServer?.address) {
|
||||
const apiTemp = context.jellyfinSDK.createApi(
|
||||
currentServer?.address,
|
||||
undefined,
|
||||
);
|
||||
try {
|
||||
await getSystemApi(apiTemp).getPingSystem();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw redirect({
|
||||
to: "/error/$code",
|
||||
params: {
|
||||
code: "101",
|
||||
},
|
||||
});
|
||||
}
|
||||
const userOnDisk = await getUser();
|
||||
if (userOnDisk) {
|
||||
if (location.pathname !== "/login/manual") {
|
||||
const apiTemp = context.jellyfinSDK.createApi(
|
||||
currentServer?.address,
|
||||
userOnDisk.AccessToken,
|
||||
);
|
||||
try {
|
||||
await getUserApi(apiTemp).getCurrentUser();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
if (isAuthError(error)) {
|
||||
await delUser();
|
||||
throw redirect({
|
||||
to: "/login/manual",
|
||||
search: {
|
||||
redirect: location.href,
|
||||
},
|
||||
});
|
||||
}
|
||||
// Transient network failure: keep the saved session
|
||||
// instead of logging the user out.
|
||||
}
|
||||
}
|
||||
context.createApi(currentServer?.address, userOnDisk.AccessToken);
|
||||
} else {
|
||||
context.createApi(currentServer?.address, undefined); // Creates Api
|
||||
if (!isAuthPage) {
|
||||
throw redirect({
|
||||
to: "/login/manual",
|
||||
search: { redirect: location.href },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!currentServerId) {
|
||||
if (!isAuthPage) throw redirect({ to: "/setup/server/add" });
|
||||
return;
|
||||
}
|
||||
} else if (context.api) {
|
||||
// The API was created from a token stored on disk. The token can be
|
||||
// revoked (logged out everywhere / server reset); verify it and send
|
||||
// the user back to the login page instead of leaving a broken page.
|
||||
if (!context.api.accessToken && !isAuthPage) {
|
||||
|
||||
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 },
|
||||
});
|
||||
}
|
||||
if (context.api.accessToken && !isAuthPage) {
|
||||
try {
|
||||
await getUserApi(context.api).getCurrentUser();
|
||||
} catch (error) {
|
||||
console.error("Could not verify the stored access token", error);
|
||||
if (isAuthError(error)) {
|
||||
await delUser();
|
||||
context.createApi(context.api.basePath, undefined);
|
||||
throw redirect({
|
||||
to: "/login/manual",
|
||||
search: { redirect: location.href },
|
||||
});
|
||||
}
|
||||
// Network hiccup: do not throw the user out of the app.
|
||||
}
|
||||
}
|
||||
} else if (!context.api.accessToken && !isAuthPage) {
|
||||
throw redirect({
|
||||
to: "/login/manual",
|
||||
search: { redirect: location.href },
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user