Skip to content

Fix YouTube history pagination stuck in a loop#113

Closed
micahmo wants to merge 2 commits intoaizhimou:mainfrom
micahmo:fix-bug-109
Closed

Fix YouTube history pagination stuck in a loop#113
micahmo wants to merge 2 commits intoaizhimou:mainfrom
micahmo:fix-bug-109

Conversation

@micahmo
Copy link

@micahmo micahmo commented Mar 4, 2026

⚠️ Generated by Copilot

Please choose the "Hide whitespace" option when reviewing.


Fix "Load more episodes from YouTube" getting stuck in a loop (issue #109)

"Load more episodes from YouTube" would repeatedly fetch the same page, either adding duplicate episodes or looping forever instead of advancing to older content.

Root cause

The "Load more episodes" button only appears for Channel subscriptions (not Playlists). The next YouTube API page was computed in ChannelService as ceil(savedEpisodeCount / 50) + 1. Two failure modes:

  1. All fetched episodes already exist in DBsaveEpisodes() skips duplicates, totalCount doesn't change, next click computes the same targetPage → infinite loop fetching the same page.
  2. Content filters drop items from a page — the DB count no longer maps correctly to pages fetched. E.g. page 2 yields 3 passing items → DB has 50+3=53 total → ceil(53/50) + 1 = page 2 again.

Additionally, the return value included ALL episodes from the YouTube API page (including ones already in the DB), causing the frontend to show "Loaded 6 historical episodes" and append duplicate entries to the list.

Fix: persist history_page_index on the Channel

Instead of recomputing from DB count, store which YouTube API page was last fetched and always advance it by 1 — regardless of how many items matched or were new.

When a page contains only episodes already in the DB (e.g. due to the migration seed value slightly under-counting pages consumed during init), the method automatically skips forward to the next page (up to 5 consecutive skips) so the user doesn't have to click "Load more" through already-known pages.

File Change
V36__Add_channel_history_page_index.sql Adds history_page_index INTEGER NOT NULL DEFAULT 0 to channel table; seeds existing channels via ceil(episode_count / 50) so first "Load more" targets the correct next page
Channel.java Adds historyPageIndex field
ChannelService.fetchChannelHistory Replaces DB-count formula with historyPageIndex + 1; persists incremented value on every call (hit or miss); auto-skips pages with all duplicates; returns only truly-new episodes via filterNewEpisodes()

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@micahmo micahmo marked this pull request as draft March 4, 2026 17:36
@micahmo micahmo marked this pull request as ready for review March 4, 2026 18:58
@micahmo
Copy link
Author

micahmo commented Mar 11, 2026

Was this issue solved in b54eebb by switching to cursor-based pagination?

@aizhimou
Copy link
Owner

I did not end up taking the PR code directly, because I chose to solve this by upgrading the metadata storage model rather than applying a narrower patch on top of the previous behavior. That said, your investigation and PR were genuinely helpful and helped point us in the right direction, so thank you for that.

@aizhimou aizhimou closed this Mar 11, 2026
@micahmo micahmo deleted the fix-bug-109 branch March 11, 2026 01:31
@aizhimou
Copy link
Owner

Was this issue solved in b54eebb by switching to cursor-based pagination?

Yes, I think it does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants