Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Init doc"v9.5.519/09/2026
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
onRequest Astro Middleware Documentation
The onRequest middleware from astro-intlayer/middleware resolves the locale of each incoming HTTP request and populates Astro.locals.intlayer.
When you register the intlayer() integration in astro.config.mjs, this middleware is injected automatically. You only need to import it directly if you are manually composing Astro middleware using sequence(...).
Usage
Copy the code to the clipboard
Description
The middleware performs the following:
- Locale Detection:
- URL: Analyses the URL path prefix or
?locale=search parameter (unlessrouting.modeis set tono-prefix). - Cookies / Headers: Checks persisted locale cookies or custom header values.
- Accept-Language: Falls back to the browser's preferred language negotiation.
- For prerendered pages (
context.isPrerendered), the locale is extracted strictly from the URL to prevent Astro build warnings.
- URL: Analyses the URL path prefix or
- Context Population: Populates
Astro.locals.intlayerwith:locale: The resolved locale.defaultLocale: The default fallback locale.availableLocales: The array of configured locales.
- AsyncLocalStorage Scope: Wraps the downstream request processing inside an
AsyncLocalStoragescope, allowinguseIntlayer(),useDictionary(), anduseLocale()to access request state without passing arguments.
IntlayerLocals Type
Copy the code to the clipboard
