This commit is contained in:
payacom
2026-10-06 17:07:08 +03:30
parent d6386b779f
commit f364f989ed
50 changed files with 2071 additions and 1656 deletions

View File

@@ -2,7 +2,16 @@ import { Post } from "@/types/types";
import { buildStorageUrl } from "@/components/main/BaseUrl";
import { resolvePostMediaType } from "@/lib/reelsMediaType";
const MAX_WARM_VIDEOS = 8;
const MAX_WARM_VIDEOS = 3;
export function isConstrainedNetwork(): boolean {
if (typeof navigator === "undefined") return false;
const connection = (navigator as Navigator & {
connection?: { saveData?: boolean; effectiveType?: string };
}).connection;
if (!connection) return false;
return connection.saveData === true || ["slow-2g", "2g", "3g"].includes(connection.effectiveType ?? "");
}
const warmVideos = new Map<string, HTMLVideoElement>();
const prefetchedLinks = new Set<string>();
@@ -23,7 +32,7 @@ function evictOldestWarmVideo() {
}
function warmVideoElement(url: string) {
if (typeof window === "undefined" || !url || warmVideos.has(url)) return;
if (typeof window === "undefined" || !url || warmVideos.has(url) || isConstrainedNetwork()) return;
const video = document.createElement("video");
video.preload = "auto";
@@ -69,7 +78,7 @@ export function prefetchPostVideo(
post: Pick<Post, "files" | "type" | "_id"> | null | undefined,
priority: VideoPrefetchPriority = "high"
) {
prefetchVideoUrl(getPostVideoSrc(post ?? undefined), priority);
prefetchVideoUrl(post ? getPostVideoSrc(post) : undefined, priority);
}
export function prefetchPostVideos(