Files
myeasycms-v2/packages/i18n/src/i18n.client.ts
Giancarlo Buomprisco f0baf4f348 Fix cookie sameSite attribute (#443)
* fix: set i18n cookie SameSite=lax to survive cross-domain redirects

The i18n language cookie was being reset to default after Supabase
email verification redirects because the cookie was using the default
SameSite=strict setting which doesn't survive cross-domain navigation.

Changes:
- Set cookieOptions.sameSite to 'lax' to allow cookie to persist through
  cross-domain redirects (like Supabase auth flows)
- Set secure flag dynamically based on HTTPS protocol
- Set cookie path to '/' for site-wide availability
- Set cookie expiration to 1 year
- Change detection order to prioritize cookie over htmlTag

Bumps version to 2.23.4

* fix: set theme cookie SameSite=lax to survive cross-domain redirects

Apply the same fix as the i18n cookie to the theme cookie. The theme
preference cookie was also missing SameSite=lax attribute, causing it
to potentially be lost during cross-domain authentication flows.

Changes:
- Add SameSite=lax to theme cookie
- Add Secure flag conditionally based on HTTPS protocol
- Fix typo: setCookeTheme -> setCookieTheme

* fix: correct SameSite casing and add security to sidebar cookie

- Fix SameSite value to use proper capitalization (Lax instead of lax)
  for better browser compatibility
- Add SameSite=Lax and conditional Secure flag to sidebar state cookie
- Add typeof window check for defensive programming

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-11 15:10:49 +01:00

88 lines
2.5 KiB
TypeScript

import i18next, { type InitOptions, i18n } from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import resourcesToBackend from 'i18next-resources-to-backend';
import { initReactI18next } from 'react-i18next';
// Keep track of the number of iterations
let iteration = 0;
// Maximum number of iterations
const MAX_ITERATIONS = 20;
/**
* Initialize the i18n instance on the client.
* @param settings - the i18n settings
* @param resolver - a function that resolves the i18n resources
*/
export async function initializeI18nClient(
settings: InitOptions,
resolver: (lang: string, namespace: string) => Promise<object>,
): Promise<i18n> {
const loadedLanguages: string[] = [];
const loadedNamespaces: string[] = [];
await i18next
.use(
resourcesToBackend(async (language, namespace, callback) => {
const data = await resolver(language, namespace);
if (!loadedLanguages.includes(language)) {
loadedLanguages.push(language);
}
if (!loadedNamespaces.includes(namespace)) {
loadedNamespaces.push(namespace);
}
return callback(null, data);
}),
)
.use(LanguageDetector)
.use(initReactI18next)
.init(
{
...settings,
detection: {
order: ['cookie', 'htmlTag', 'navigator'],
caches: ['cookie'],
lookupCookie: 'lang',
cookieMinutes: 60 * 24 * 365, // 1 year
cookieOptions: {
sameSite: 'lax',
secure: typeof window !== 'undefined' && window.location.protocol === 'https:',
path: '/',
},
},
interpolation: {
escapeValue: false,
},
},
(err) => {
if (err) {
console.error('Error initializing i18n client', err);
}
},
);
// to avoid infinite loops, we return the i18next instance after a certain number of iterations
// even if the languages and namespaces are not loaded
if (iteration >= MAX_ITERATIONS) {
console.debug(`Max iterations reached: ${MAX_ITERATIONS}`);
return i18next;
}
// keep component from rendering if no languages or namespaces are loaded
if (loadedLanguages.length === 0 || loadedNamespaces.length === 0) {
iteration++;
console.debug(
`Keeping component from rendering if no languages or namespaces are loaded. Iteration: ${iteration}. Will stop after ${MAX_ITERATIONS} iterations.`,
);
throw new Error('No languages or namespaces loaded');
}
return i18next;
}