Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
The content of this page was translated using an AI.
See the last version of the original content in EnglishIf you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy doc Markdown to clipboard
useRewriteURL Hook
The useRewriteURL hook is designed to manage localised URL rewrites on the client-side. It automatically detects if the current URL should be corrected to a "pretty" localised version based on the user's locale and the rewrite rules defined in intlayer.config.ts.
Unlike standard navigation, this hook uses window.history.replaceState to update the URL in the address bar without triggering a full page reload or a router navigation cycle.
Usage
Simply call the hook from a client-side component.
How it works
- Detection: The hook monitors the current
window.location.pathnameand the user'slocale. - Matching: It uses the internal Intlayer engine to check whether the current pathname matches a canonical route that has a prettier localised alias for the current locale.
- URL Correction: If a better alias is found (and it differs from the current path), the hook calls
window.history.replaceStateto update the browser URL while preserving the same canonical content and state.
Why use it?
- SEO: Ensures that users always land on the single, authoritative pretty URL for a given language.
- Consistency: Prevents inconsistencies where a user might manually type a canonical path (like
/fr/privacy-notice) instead of the localised version (/fr/politique-de-confidentialite). - Performance: Updates the address bar without triggering unwanted router side effects or component remounts.