-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Bug Report: loadUrl in ChainedBackend Calls Twice #1830
Comments
I'm not able to reproduce your issue. For example, try to use this example and update it like this: ![]() |
Hi @adrai unfortunately we cant share project file please find below structured snippet. i18n where const dynamicBackend = { const i18nextOptions = { Please let us know what we are doing wrong here. we see two apicalls in network. Thanks. |
Your snippets do not help to investigate... |
Also, please provide your i18next debug output. |
=> Use ChainedBackend with multiple backends configured.
=> Initialize i18n with use(initReactI18next).use(ChainedBackend).init({...}).
*Observe that loadUrl(url, callback) is triggered twice for the same resource.
Expected Behavior
loadUrl should be called only once per resource.
Actual Behavior
loadUrl gets called twice for the same localization file.
This results in multiple API calls to fetch the same translations, causing redundant network requests.
code - const [i18completed, setI18completed] = useState(false);
const [initialized, setInitialized] = useState(false);
const backendConfig = getBackendConfig(selectedSetupUrl, selectedEnv, getFromSession);
import Fetch from 'i18next-fetch-backend';
import { getDynamicLoadPath, i18nextOptions } from '../../i18nextOptions';
import { getDomainFromUrl } from '@/utils/getDomainFromUrl';
const getBackendConfig = (
selectedSetupUrl: string,
selectedEnv: string,
getFromSession: { locale: string },
) => {
const locale = getFromSession?.locale || 'en_US'; // Default to 'en_US' if no locale is provided
const domain = getDomainFromUrl(selectedSetupUrl); // Get the domain from the URL
// First backend (dynamic based on the URL and environment)
const dynamicBackend = {
loadPath: getDynamicLoadPath(domain, locale, selectedEnv),
crossDomain: true,
};
// Second backend: i18nextOptions (already defined in your i18nextBackend.js)
const staticBackend = i18nextOptions;
return {
backends: [Fetch, Fetch], // Use two Fetch instances or any other backends you prefer
backendOptions: [dynamicBackend, staticBackend], // The dynamic config and static i18nextOptions
};
};
export default getBackendConfig;
if (!i18completed && !initialized) {
setInitialized(true);
i18n
.use(initReactI18next)
.use(ChainedBackend)
.init({
backend: {
backends: backendConfig.backends, // Multiple backends
backendOptions: backendConfig.backendOptions,
},
fallbackLng: 'en_US',
lng: getFromSession?.locale || 'en_US',
preload: ['en_US'],
debug: true,
react: {
useSuspense: false,
},
detection: {
order: [
'querystring',
'cookie',
'localStorage',
'navigator',
'htmlTag',
'path',
'subdomain',
],
caches: ['localStorage', 'cookie'],
},
interpolation: {
escapeValue: false,
},
})
.then(() => {
setI18completed(true);
});
}
version - "i18next": "^23.11.5",
The text was updated successfully, but these errors were encountered: