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
37 lines
1.5 KiB
Plaintext
37 lines
1.5 KiB
Plaintext
import { usePathname } from 'next/navigation';
|
|
import { useMemo } from 'react';
|
|
import { useLocale } from 'use-intl';
|
|
import { hasPathnamePrefixed, unprefixPathname, getLocalePrefix, getLocaleAsPrefix } from '../../shared/utils.js';
|
|
|
|
function useBasePathname(config) {
|
|
// The types aren't entirely correct here. Outside of Next.js
|
|
// `useParams` can be called, but the return type is `null`.
|
|
|
|
// Notes on `useNextPathname`:
|
|
// - Types aren't entirely correct. Outside of Next.js the
|
|
// hook will return `null` (e.g. unit tests)
|
|
// - A base path is stripped from the result
|
|
// - Rewrites *are* taken into account (i.e. the pathname
|
|
// that the user sees in the browser is returned)
|
|
const pathname = usePathname();
|
|
const locale = useLocale();
|
|
return useMemo(() => {
|
|
if (!pathname) return pathname;
|
|
let unlocalizedPathname = pathname;
|
|
const prefix = getLocalePrefix(locale, config.localePrefix);
|
|
const isPathnamePrefixed = hasPathnamePrefixed(prefix, pathname);
|
|
if (isPathnamePrefixed) {
|
|
unlocalizedPathname = unprefixPathname(pathname, prefix);
|
|
} else if (config.localePrefix.mode !== 'never' && config.localePrefix.prefixes) {
|
|
// Workaround for https://github.com/vercel/next.js/issues/73085
|
|
const localeAsPrefix = getLocaleAsPrefix(locale);
|
|
if (hasPathnamePrefixed(localeAsPrefix, pathname)) {
|
|
unlocalizedPathname = unprefixPathname(pathname, localeAsPrefix);
|
|
}
|
|
}
|
|
return unlocalizedPathname;
|
|
}, [config.localePrefix, locale, pathname]);
|
|
}
|
|
|
|
export { useBasePathname as default };
|