-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix: Page meta not change after language switch and potential memory issues on localization #12217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
7 Skipped Deployments
|
|
||
export const LanguageContext = createContext<ContextApi | undefined>(undefined) | ||
|
||
const cache = new Map<string, string>() | ||
const cache = new LRU<string, string>({ maxSize: 250 }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to lru for avoiding memory issues
const cacheKey = `${lang}:${ver}:${key}-${JSON.stringify(data)}` | ||
if (cache.has(cacheKey)) { | ||
|
||
const cacheKey = data ? `${lang}:${ver}:${key}-${JSON.stringify(data)}` : undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to use cache where there is no data
bundle: Record<string, string> | ||
ver: number | ||
isFetching: boolean | ||
}>({ | ||
}>(() => ({ | ||
language: lang, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added to state to change language and bundle at the same time, to avoid render mismatchs
@@ -0,0 +1,79 @@ | |||
export interface LRUOptions { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copied directly from utils package to not have dep on it
Root cause is, bundle and locale not changing at the same time and if locale used as cache key (https://github.com/pancakeswap/pancake-frontend/blob/develop/apps/web/src/config/constants/meta.ts#L113) it will end up calling obsolete translation function which results old results
To reproduce
Go to swap
Change language
See title remains on the old language
PR-Codex overview
This PR introduces a new
LRU
(Least Recently Used) caching mechanism to optimize language bundle management in a localization context. It enhances theuseLocaleBundle
hook andLanguageProvider
component by implementing caching for improved performance.Detailed summary
LRU
class andLRUOptions
interface inlru.ts
for caching.useLocaleBundle
to includelanguage
in state and useLRU
for caching.LanguageProvider
to replaceMap
withLRU
for cache management.LanguageProvider
.