Files
klz-cables.com/.pnpm-store/v10/files/54/57cea389478c4e9f95364515277a214f9455b9b9684539455125dd521215ca99ced0dd4f9cf2a6ee5ac367b531550303ccf93967a12fe58a99f91e8e4e81a9
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

48 lines
1.6 KiB
Plaintext

import { getBasePath } from './utils.js';
/**
* We have to keep the cookie value in sync as Next.js might
* skip a request to the server due to its router cache.
* See https://github.com/amannn/next-intl/issues/786.
*/
function syncLocaleCookie(localeCookie, pathname, locale, nextLocale) {
const isSwitchingLocale = nextLocale !== locale && nextLocale != null;
if (!localeCookie || !isSwitchingLocale ||
// Theoretical case, we always have a pathname in a real app,
// only not when running e.g. in a simulated test environment
!pathname) {
return;
}
const basePath = getBasePath(pathname);
const hasBasePath = basePath !== '';
const defaultPath = hasBasePath ? basePath : '/';
const {
name,
...rest
} = localeCookie;
if (!rest.path) {
rest.path = defaultPath;
}
let localeCookieString = `${name}=${nextLocale};`;
for (const [key, value] of Object.entries(rest)) {
// Map object properties to cookie properties.
// Interestingly, `maxAge` corresponds to `max-age`,
// while `sameSite` corresponds to `SameSite`.
// Also, keys are case-insensitive.
const targetKey = key === 'maxAge' ? 'max-age' : key;
localeCookieString += `${targetKey}`;
if (typeof value !== 'boolean') {
localeCookieString += '=' + value;
}
// A trailing ";" is allowed by browsers
localeCookieString += ';';
}
// Note that writing to `document.cookie` doesn't overwrite all
// cookies, but only the ones referenced via the name here.
document.cookie = localeCookieString;
}
export { syncLocaleCookie as default };