From dfba6bde1efe5b227a9a99bcdab13b9499bc89e7 Mon Sep 17 00:00:00 2001 From: Thomas Ghysels Date: Fri, 26 Aug 2022 14:44:09 +0200 Subject: [PATCH] feat: add option to clear cache in @bothrs/translations --- packages/translations/README.md | 23 +++++++++++++++++++++++ packages/translations/src/index.ts | 3 ++- packages/translations/src/types.ts | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/translations/README.md b/packages/translations/README.md index 4112f74..10fdea5 100644 --- a/packages/translations/README.md +++ b/packages/translations/README.md @@ -34,6 +34,7 @@ export function AirtableExample() { | -------------- | ------------------------------------------------------- | | expirationTime | Time between translation refreshes in ms | | loadPath | The endpoint from where the translations will be loaded | +| storagePrefix | Prefix for the location where the cache is persisted | ### Airtable table structure @@ -69,7 +70,29 @@ export function GenericExample() { } ``` +## Refresh translations + +If translations change commonly, consider updating the `storagePrefix` to make sure all users are using the latest translations. This prefix acts as a cache-busting mechanism. + +```tsx +import { useTranslations } from '@bothrs/translations' +import i18next from 'i18next' + +export function GenericExample() { + const ready = useTranslations({ + // Increment manually + storagePrefix: 'i18n_3_', + // Refresh on every app update + storagePrefix: 'i18n_' + Constants.manifest?.version + '_', + }) + return
{ready ? i18next.t('Ready') : 'Loading'}
+} +``` + +## All options + | Name | Explanation | | -------------- | ------------------------------------------------------- | | expirationTime | Time between translation refreshes in ms | | loadPath | The endpoint from where the translations will be loaded | +| storagePrefix | Prefix for the location where the cache is persisted | diff --git a/packages/translations/src/index.ts b/packages/translations/src/index.ts index 454ffec..31ed8ec 100644 --- a/packages/translations/src/index.ts +++ b/packages/translations/src/index.ts @@ -36,6 +36,7 @@ export function useTranslations( } export function initTranslations({ + storagePrefix = 'i18n_', expirationTime, fetchOptions, ...options @@ -54,7 +55,7 @@ export function initTranslations({ backend: { backends: [StorageBackend, MultiloadAdapter], backendOptions: [ - { prefix: 'i18n_', expirationTime }, + { prefix: storagePrefix, expirationTime }, { backend: Fetch, backendOption: fetchOptions }, ], }, diff --git a/packages/translations/src/types.ts b/packages/translations/src/types.ts index ba2defb..eb9fc1e 100644 --- a/packages/translations/src/types.ts +++ b/packages/translations/src/types.ts @@ -37,6 +37,8 @@ export interface TranslationRow { } export interface TranslationInitParameters extends InitOptions { + /** allows to bust the cache in a declarative way, warning: previous cached versions are kept in storage forever */ + storagePrefix?: string /** expirationTime time between between revalidation intervals, defaults to 1 week */ expirationTime?: number /** Configuration for 'i18next-fetch-backend' */