Skip to content
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

Memoized cache should be busted when new resources are added #17

Closed
offirgolan opened this issue Mar 18, 2020 · 3 comments
Closed

Memoized cache should be busted when new resources are added #17

offirgolan opened this issue Mar 18, 2020 · 3 comments

Comments

@offirgolan
Copy link
Contributor

@offirgolan offirgolan commented Mar 18, 2020

I ran into this issue when setting up react-i18next with gatsby when hot reloading the translations weren't working. This was a major troll not gonna lie lol.

i18next.addResourceBundle(locale, 'translation', { foo: 'bar' }, false, true);
i18next.t('foo') // => 'bar'
i18next.addResourceBundle(locale, 'translation', { foo: 'baz' }, false, true);
i18next.t('foo') // => 'bar'
"dependencies": {
    "i18next": "^19.3.2",
    "i18next-icu": "^1.2.1",
    "react-i18next": "^11.3.3"
  },

My current work around is to turn off memoize in the dev environment but I feel like this should be something handled by this library or provide a way to manually clear the cache.

@jamuhl

This comment has been minimized.

Copy link
Member

@jamuhl jamuhl commented Mar 18, 2020

setting i18next.services.i18nFormat.mem = {} should do the trick. Not sure how we should handle this...we do not really know if this runs in development or production.

@offirgolan

This comment has been minimized.

Copy link
Contributor Author

@offirgolan offirgolan commented Mar 18, 2020

Wouldn't you want to bust the cache regardless of the environment if any resources are changed? Can you do something like

i18next.store.on('added removed', () => {
  i18next.services.i18nFormat.mem = {}
});

Doing this in my codebase seems very fragile since mem is a private field. If anything, could you provide a public and documented API to clear the cache?

i18next.services.i18nFormat.clearCache()

@jamuhl

This comment has been minimized.

Copy link
Member

@jamuhl jamuhl commented Mar 18, 2020

Feel free to add a PR which adds either a clearCache function or options to set bindings for store and i18next event (defaulting to none) which clears the cache on events defined passed as options like we eg. do in react-i18next: https://github.com/i18next/react-i18next/blob/master/src/context.js#L4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants
You can’t perform that action at this time.