Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
If 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 localized URL rewrites on the client side. It automatically detects if the current URL should be corrected to a "pretty" localized 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 in 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 if the current pathname matches a canonical route that has a prettier localized alias for the current locale.
- URL Correction: If a better alias is found (and it's different 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 localized version (/fr/politique-de-confidentialite). - Performance: Updates the address bar without triggering unwanted router side-effects or component re-mounts.