react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
42 lines (39 loc) • 1.57 kB
JavaScript
'use client';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
const require_client_useLocale = require('./useLocale.cjs');
let _intlayer_config_built = require("@intlayer/config/built");
let react = require("react");
let _intlayer_core_localization = require("@intlayer/core/localization");
//#region src/client/useRewriteURL.ts
/**
* Client-side hook to manage URL rewrites without triggering a router navigation.
* It uses `window.history.replaceState` to update the URL in the address bar.
*
* This hook is useful to "prettify" the URL when the user lands on a canonical path
* that has a localized alias defined in `intlayer.config.ts`.
*
* @example
* ```tsx
* import { useRewriteURL } from 'react-intlayer';
*
* const MyComponent = () => {
* useRewriteURL();
*
* return <div>My Component</div>;
* };
* ```
*/
const useRewriteURL = () => {
const { locale } = require_client_useLocale.useLocale();
const rewrite = _intlayer_config_built.routing?.rewrite;
(0, react.useEffect)(() => {
if (typeof window === "undefined" || !rewrite) return;
const pathname = window.location.pathname;
const targetPath = (0, _intlayer_core_localization.getRewritePath)(pathname, locale, rewrite);
if (targetPath && targetPath !== pathname) window.history.replaceState(window.history.state, "", targetPath + window.location.search + window.location.hash);
}, [locale, rewrite]);
};
//#endregion
exports.useRewriteURL = useRewriteURL;
//# sourceMappingURL=useRewriteURL.cjs.map