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
@@ -0,0 +1,106 @@
|
||||
import type { QueryClient } from "@tanstack/react-query";
|
||||
|
||||
const STORAGE_KEY = "blink:query-cache:v1";
|
||||
const MAX_AGE_MS = 1000 * 60 * 60 * 24 * 7;
|
||||
const MAX_ENTRIES = 60;
|
||||
|
||||
type PersistedEntry = {
|
||||
key: readonly unknown[];
|
||||
data: unknown;
|
||||
updatedAt: number;
|
||||
};
|
||||
|
||||
type PersistedCache = {
|
||||
owner: string;
|
||||
entries: PersistedEntry[];
|
||||
};
|
||||
|
||||
const isPersistableKey = (key: readonly unknown[]) =>
|
||||
typeof key[0] === "string" && (key[0] === "home" || key[0] === "libraries");
|
||||
|
||||
/**
|
||||
* Synchronously hydrate the most useful home/library queries from
|
||||
* localStorage. React Query then renders cached content immediately and
|
||||
* refetches in the background.
|
||||
*/
|
||||
export const restoreQueryCache = (client: QueryClient, owner: string) => {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
if (!raw) return;
|
||||
const cached = JSON.parse(raw) as PersistedCache;
|
||||
if (cached.owner !== owner || !Array.isArray(cached.entries)) return;
|
||||
const now = Date.now();
|
||||
for (const entry of cached.entries) {
|
||||
if (!Array.isArray(entry.key) || !isPersistableKey(entry.key)) continue;
|
||||
if (!entry.updatedAt || now - entry.updatedAt > MAX_AGE_MS) continue;
|
||||
client.setQueryData(entry.key, entry.data, {
|
||||
updatedAt: entry.updatedAt,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Could not restore the query cache", error);
|
||||
}
|
||||
};
|
||||
|
||||
/** Persist only home/library query data; throttled and silently forgiving. */
|
||||
export const persistQueryCache = (
|
||||
client: QueryClient,
|
||||
getOwner: () => string | undefined,
|
||||
) => {
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
const save = () => {
|
||||
const owner = getOwner();
|
||||
if (!owner) return;
|
||||
try {
|
||||
const entries = client
|
||||
.getQueryCache()
|
||||
.getAll()
|
||||
.filter(
|
||||
(query) =>
|
||||
query.state.status === "success" &&
|
||||
query.state.data !== undefined &&
|
||||
isPersistableKey(query.queryKey),
|
||||
)
|
||||
.sort((a, b) => b.state.dataUpdatedAt - a.state.dataUpdatedAt)
|
||||
.slice(0, MAX_ENTRIES)
|
||||
.map((query) => ({
|
||||
key: query.queryKey,
|
||||
data: query.state.data,
|
||||
updatedAt: query.state.dataUpdatedAt,
|
||||
}));
|
||||
const payload: PersistedCache = { owner, entries };
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(payload));
|
||||
} catch (error) {
|
||||
// A full storage quota must never break navigation.
|
||||
console.warn("Could not persist the query cache", error);
|
||||
try {
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
} catch {
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const schedule = () => {
|
||||
window.clearTimeout(timer);
|
||||
timer = window.setTimeout(save, 400);
|
||||
};
|
||||
|
||||
const unsubscribe = client.getQueryCache().subscribe(schedule);
|
||||
window.addEventListener("beforeunload", save);
|
||||
|
||||
return () => {
|
||||
window.clearTimeout(timer);
|
||||
unsubscribe();
|
||||
window.removeEventListener("beforeunload", save);
|
||||
};
|
||||
};
|
||||
|
||||
export const clearPersistedQueryCache = () => {
|
||||
try {
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
} catch {
|
||||
// Ignore.
|
||||
}
|
||||
};
|
||||
@@ -40,6 +40,7 @@ const getUser = async () => {
|
||||
*/
|
||||
const delUser = async () => {
|
||||
sessionStorage.removeItem("accessToken");
|
||||
localStorage.removeItem("blink:query-cache:v1");
|
||||
await user.clear();
|
||||
await user.save();
|
||||
};
|
||||
|
||||
+24
-16
@@ -46,22 +46,30 @@ export const jellyfin = new Jellyfin({
|
||||
},
|
||||
});
|
||||
|
||||
export const initializeApi = async () => {
|
||||
const currentServerId = await getDefaultServer();
|
||||
if (currentServerId) {
|
||||
const currentServer = await getServer(currentServerId);
|
||||
if (currentServer?.address) {
|
||||
const userOnDisk = await getUser();
|
||||
if (userOnDisk) {
|
||||
return jellyfin.createApi(
|
||||
currentServer.address,
|
||||
userOnDisk.AccessToken,
|
||||
);
|
||||
}
|
||||
return jellyfin.createApi(currentServer.address);
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
export type InitialSession = {
|
||||
api?: Api;
|
||||
user?: { Name: string; AccessToken: string; Id: string } | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Restore the API and a minimal user snapshot from local storage in one pass.
|
||||
* The full UserDto is fetched in the background, so the UI can render without
|
||||
* waiting for the server.
|
||||
*/
|
||||
export const initializeApi = async (): Promise<InitialSession> => {
|
||||
const [currentServerId, userOnDisk] = await Promise.all([
|
||||
getDefaultServer(),
|
||||
getUser(),
|
||||
]);
|
||||
if (!currentServerId) return { user: userOnDisk };
|
||||
|
||||
const currentServer = await getServer(currentServerId);
|
||||
if (!currentServer?.address) return { user: userOnDisk };
|
||||
|
||||
return {
|
||||
api: jellyfin.createApi(currentServer.address, userOnDisk?.AccessToken),
|
||||
user: userOnDisk,
|
||||
};
|
||||
};
|
||||
|
||||
export const ApiContext = createContext<StoreApi<ApiStore>>(undefined!);
|
||||
|
||||
@@ -51,7 +51,13 @@ export const setAppReady = () => {
|
||||
|
||||
const CentralContext = createContext<StoreApi<CentralStore>>(null!);
|
||||
|
||||
export const CentralProvider = ({ children }: { children: ReactNode }) => {
|
||||
export const CentralProvider = ({
|
||||
children,
|
||||
initialUser,
|
||||
}: {
|
||||
children: ReactNode;
|
||||
initialUser?: UserDto | null;
|
||||
}) => {
|
||||
const [store] = useState(() =>
|
||||
createStore<CentralStore>()((set) => ({
|
||||
defaultServerOnDisk: async () => (await getDefaultServer()) ?? null,
|
||||
@@ -63,7 +69,7 @@ export const CentralProvider = ({ children }: { children: ReactNode }) => {
|
||||
allServersOnDisk: async () => await getAllServers(),
|
||||
userOnDisk: async () => (await getUser()) ?? null,
|
||||
clientVersion: version,
|
||||
currentUser: null,
|
||||
currentUser: initialUser ?? null,
|
||||
fetchCurrentUser: async (api) => {
|
||||
if (api?.accessToken) {
|
||||
const user = (await getUserApi(api).getCurrentUser()).data;
|
||||
|
||||
Reference in new issue
Block a user