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

Bug Report: loadUrl in ChainedBackend Calls Twice #1830

Open
rahulsharmaf81 opened this issue Jan 30, 2025 · 1 comment
Open

Bug Report: loadUrl in ChainedBackend Calls Twice #1830

rahulsharmaf81 opened this issue Jan 30, 2025 · 1 comment

Comments

@rahulsharmaf81
Copy link

=> 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",

Image Image
@adrai
Copy link
Member

adrai commented Jan 30, 2025

I'm not able to reproduce your issue.
Please provide a minimal reproducible example repository, and not just unformatted code snippets.

For example, try to use this example and update it like this:

Image

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

No branches or pull requests

2 participants