Author:
    Creation:2026-09-19Last update:2026-09-19

    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

    src/middleware.ts
    import { onRequest as intlayer } from "astro-intlayer/middleware";
    import { sequence } from "astro:middleware";
    
    export const onRequest = sequence(intlayer, async (context, next) => {
      // Access the resolved locale in your custom middleware
      const { locale } = context.locals.intlayer;
      console.log(`Handling request for locale: ${locale}`);
    
      return next();
    });
    

    Description

    The middleware performs the following:

    1. Locale Detection:
      • URL: Analyzes the URL path prefix or ?locale= search parameter (unless routing.mode is set to no-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.
    2. Context Population: Populates Astro.locals.intlayer with:
      • locale: The resolved locale.
      • defaultLocale: The default fallback locale.
      • availableLocales: The array of configured locales.
    3. AsyncLocalStorage Scope: Wraps the downstream request processing inside an AsyncLocalStorage scope, allowing useIntlayer(), useDictionary(), and useLocale() to access request state without passing arguments.

    IntlayerLocals Type

    ts
    export type IntlayerLocals = {
      locale: DeclaredLocales;
      defaultLocale: DeclaredLocales;
      availableLocales: DeclaredLocales[];
    };