fix: infinite scroll now fetches fresh videos instead of returning cached ones

This commit is contained in:
Khoa Vo 2025-12-20 16:10:45 +07:00
parent a87b97e742
commit 489a5069b5
3 changed files with 37 additions and 13 deletions

View file

@ -156,11 +156,21 @@ async def get_feed(request: FeedRequest = None):
@router.get("") @router.get("")
async def get_feed_simple(fast: bool = False): async def get_feed_simple(fast: bool = False, skip_cache: bool = False):
"""Simple GET endpoint to fetch feed using stored credentials.""" """Simple GET endpoint to fetch feed using stored credentials.
Args:
fast: If True, only get initial batch (0 scrolls). If False, scroll 5 times.
skip_cache: If True, always fetch fresh videos (for infinite scroll).
"""
try: try:
# Fast mode = 0 scrolls (just initial batch), Normal = 5 scrolls # Fast mode = 0 scrolls (just initial batch), Normal = 5 scrolls
scroll_count = 0 if fast else 5 scroll_count = 0 if fast else 5
# When skipping cache for infinite scroll, do more scrolling to get different videos
if skip_cache:
scroll_count = 8 # More scrolling to get fresh content
videos = await PlaywrightManager.intercept_feed(scroll_count=scroll_count) videos = await PlaywrightManager.intercept_feed(scroll_count=scroll_count)
return videos return videos
except Exception as e: except Exception as e:

View file

@ -374,7 +374,8 @@ export const Feed: React.FC = () => {
setIsFetching(true); setIsFetching(true);
try { try {
const newVideos = await feedLoader.loadFeedWithOptimization(false); // Pass skipCache=true to force fetching fresh videos from backend
const newVideos = await feedLoader.loadFeedWithOptimization(false, undefined, true);
setVideos(prev => { setVideos(prev => {
const existingIds = new Set(prev.map(v => v.id)); const existingIds = new Set(prev.map(v => v.id));

View file

@ -20,26 +20,35 @@ class FeedLoader {
async loadFeedWithOptimization( async loadFeedWithOptimization(
fast: boolean = false, fast: boolean = false,
onProgress?: (videos: Video[]) => void onProgress?: (videos: Video[]) => void,
skipCache: boolean = false
): Promise<Video[]> { ): Promise<Video[]> {
const startTime = performance.now(); const startTime = performance.now();
try { try {
if (fast) { if (fast && !skipCache) {
const videos = await this.loadWithCache('feed-fast'); const videos = await this.loadWithCache('feed-fast');
onProgress?.(videos); onProgress?.(videos);
return videos; return videos;
} }
const cacheKey = 'feed-full'; const cacheKey = 'feed-full';
const cached = this.getCached(cacheKey);
if (cached) { // Skip cache check when explicitly requested (for infinite scroll)
onProgress?.(cached); if (!skipCache) {
return cached; const cached = this.getCached(cacheKey);
if (cached) {
onProgress?.(cached);
return cached;
}
} }
const videos = await this.fetchFeed(); const videos = await this.fetchFeed(skipCache);
this.setCached(cacheKey, videos);
// Only cache if not skipping (initial load)
if (!skipCache) {
this.setCached(cacheKey, videos);
}
onProgress?.(videos); onProgress?.(videos);
@ -53,8 +62,12 @@ class FeedLoader {
} }
} }
private async fetchFeed(): Promise<Video[]> { private async fetchFeed(skipCache: boolean = false): Promise<Video[]> {
const response = await axios.get(`${API_BASE_URL}/feed`); // Add skip_cache parameter to force backend to fetch fresh videos
const url = skipCache
? `${API_BASE_URL}/feed?skip_cache=true`
: `${API_BASE_URL}/feed`;
const response = await axios.get(url);
if (!Array.isArray(response.data)) { if (!Array.isArray(response.data)) {
return []; return [];