[Performance] Per-endpoint SWR cache TTL + immediate mutation invalidation#743
Merged
RUKAYAT-CODER merged 5 commits intoJun 29, 2026
Merged
Conversation
|
@Spagero763 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
Thank you for contributing to the project. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[Performance] Per-endpoint SWR cache TTL + immediate mutation invalidation
Closes #597
Problem
The SWR cache used a single global TTL, so volatile data (subscription status,
balance, active quiz session) shared a freshness window with static catalog data.
A user who upgraded could see the old "Free" status for the full TTL.
Changes
src/config/apiCacheConfig.tsENDPOINT_TTL_MAP: Record<string, EndpointTtl>plusresolveEndpointTtl(url):/auth/me,/subscriptions,/payments): 30 s TTL,with
staleTtl === ttlso nothing older than 30 s is ever served./courses,/categories): 5 min TTL./apiprefixes, query strings,api:cache-keyprefixes, and nested paths (longest-prefix match).
/subscriptionsand/paymentsrules toMUTATION_INVALIDATION_MAPso awrite to either immediately invalidates the related GET caches (including
/auth/me, which embeds subscription status).src/services/api/cache.ts— addedinvalidatePattern(urlPattern)(publicalias of the existing
invalidateByPattern), the method named in the issue.src/services/api/index.ts—apiService.getnow resolves the per-endpointTTL via
resolveEndpointTtl(url)before falling back to the global default, andre-exports
invalidatePattern/resolveEndpointTtl/ENDPOINT_TTL_MAP.src/__tests__/issues/fix_597_cache_ttl.test.ts— 8 unit tests (all passing).The axios response interceptor already iterates
MUTATION_INVALIDATION_MAPandcalls
invalidateByPatternafter successful mutations, so adding thesubscriptions/payments rules wires up the immediate invalidation end-to-end.
Acceptance criteria
/subscriptionscache expires after 30 seconds maximum (ttl = staleTtl = 30s)./coursescache valid for 5 minutes./subscriptionsimmediately invalidates the/subscriptionsGET cache.Verification
jest src/__tests__/issues/fix_597_cache_ttl.test.ts→ 8/8 passing.eslint --max-warnings=0is clean on all changed files.tsc --noEmitadds no new errors from the changed files.Note: bundled lint fix (required to commit)
The repo's
eslint.config.jshad'no-console': ['error', { allow: [] }], which thepinned ESLint 9.39.4 rejects (
allowmust have ≥1 item). This crashedexpo lintand the husky pre-commit hook on a clean checkout, so no commit could pass the
hook. I fixed it to the equivalent valid form
'no-console': 'error'(same intent:disallow all
console.*) in a separate, clearly-labeled commit so the lint/hook runagain. Happy to split this into its own PR if preferred.