Creation:2025-08-23Last update:2025-08-23
Reference this doc to your favorite AI assistantChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
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 EnglishEdit this doc
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
Copy doc Markdown to clipboard
useRewriteURL Hook
The useRewriteURL hook for Svelte is designed to manage localised URL rewrites on the client side. It automatically corrects the browser's URL to its "pretty" localised version based on the current locale and the configuration in intlayer.config.ts.
It updates the URL silently using window.history.replaceState, avoiding a full SvelteKit navigation.
Usage
Call the hook within a Svelte component.
How it works
- Reactive Updates: The hook subscribes to the Intlayer
localestore. - Detection: Whenever the locale changes (or on mount), it calculates if the current
window.location.pathnamehas a prettier localised alias defined in your rewrite rules. - URL Correction: If a prettier path is found, the hook calls
window.history.replaceStateto update the address bar without a full page reload or triggering SvelteKit navigation logic.
Why use it?
- SEO Best Practices: Ensures search engines index only the pretty, localised version of your URLs.
- Improved UX: Corrects manually-entered URLs to reflect your preferred naming structure.
- Silent Updates: Updates the address bar without affecting the component tree or the navigation history.