Skip to content

Bound the Twitch login lookup cache to 60 seconds - #86

Merged
t3dotgg merged 2 commits into
mainfrom
t3code/refresh-twitch-user-lookup
Sep 25, 2026
Merged

t3dotgg merged 2 commits into
mainfrom
t3code/refresh-twitch-user-lookup

Conversation

@t3dotgg

@t3dotgg t3dotgg commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Twitch logins can change or go to a different account. The login-to-ID lookup caches with revalidate: Infinity, so it can return an old account ID.

On main this rarely happens. Next puts request headers in the fetch cache key, and that includes the Authorization token. The channel page gets a new app token on each request, so the lookup never hits the cache. The OBS embed uses the creator's token, which changes about every 4 hours.

#81 keeps one app token per server instance until it expires (about 2 months). After #81 lands, the Infinity cache hits for all of that time.

This PR sets the lookup cache to 60 seconds. After 60 seconds, Next gives the cached ID one more time and gets a new one in the background.

Also:

  • A failed Twitch request now throws Could not look up Twitch user: <status>. Main also threw, but the errors were unclear (Unauthorized, a TypeError, or a JSON parse error).
  • The login is URL-encoded.
  • A missing account still returns null.

Tests: pnpm test (34 passed), pnpm typecheck, and pnpm lint.

#81 also edits getTwitchUserId. Keep revalidate: 60 when you resolve the conflict.

Created with GPT-6 Astra in Codex. Takeover fixes by Claude Opus 5.5 in Claude Code.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Twitch account lookups now correctly handle special characters in usernames and return no result when an account isn’t found.
    • Lookups refresh every 60 seconds, and failed requests report their HTTP status.

@vercel

vercel Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
markerthing Ready Ready Preview Sep 25, 2026 4:07am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: f7e6e203-c84c-4ab0-8900-b76fad43a2b2

📥 Commits

Reviewing files that changed from the base of the PR and between 688e1e4 and 553676f.

📒 Files selected for processing (2)
  • src/utils/twitch-server.ts
  • src/utils/twitch-user.test.ts

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


Walkthrough

getTwitchUserId now encodes Twitch login parameters, refreshes cached lookups every 60 seconds, throws on non-OK responses, and returns null when no user ID is present. Tests cover encoded logins, missing accounts, and a 401 response.

Changes

Twitch user lookup

Layer / File(s) Summary
Lookup behavior and validation
src/utils/twitch-server.ts, src/utils/twitch-user.test.ts
The lookup encodes the login with URLSearchParams, uses a 60-second cache duration, and throws an error with the HTTP status for non-OK responses. It returns null when no user ID is present. Tests cover URL encoding, missing accounts, and a 401 response.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 55367

The lookup changes are mergeable after normal checks. Missing-account VOD handling remains a pre-existing issue.

Architecture Summary

Architecture risk: 🔵 Low · up to 55367

The change affects 1 system.

Changed systems: src

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — src (service) was modified; 2 changed files map to changed impact.

Before / after behavior

  • observed — Modified behavior in src/utils/twitch-server.ts: getTwitchUserId now encodes the login through URLSearchParams, caches results for 60 seconds rather than indefinitely, and throws for any non-OK HTTP response with its status. It replaces the "Unauthorized" JSON check and direct response cast with optional access to the first user ID, returning null when absent.
  • observed — Modified behavior in src/utils/twitch-user.test.ts: Adds imports, mocks the Clerk server module, and unstubs globals after each test.
  • observed — Modified behavior in src/utils/twitch-user.test.ts: Adds a test expecting getTwitchUserId to return the matching ID and encode & in the login when constructing the Twitch URL.
  • observed — Modified behavior in src/utils/twitch-user.test.ts: Adds a test expecting a login with no matching Twitch account to resolve to null.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the cache-duration change in the Twitch login lookup, which is a primary objective of the pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Changes Twitch user lookup cache from infinite to 60 seconds.

The PR appears safe to merge.

Reviews (2) · Last reviewed commit: "Name the Twitch failure test for what it..."

Comment thread src/utils/twitch-user.test.ts
Main already threw on Twitch failures, so the old name described a
behavior change that did not happen.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@t3dotgg t3dotgg changed the title Refresh Twitch channel IDs after username changes Bound the Twitch login lookup cache to 60 seconds Sep 25, 2026
@t3dotgg

t3dotgg commented Sep 25, 2026

Copy link
Copy Markdown
Member Author

Note

🤖 Claude Opus 5.5 responding on behalf of Theo

@greptileai review

The new commit 553676f only renames one test. Please review the latest head.

@t3dotgg
t3dotgg merged commit 3e5c215 into main Sep 25, 2026
6 checks passed

This branch was successfully deployed

1 active deployment
Preview — 553676f2 Deployed Sep 25, 2026 by vercel[bot]
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.

1 participant